8000 [RateLimiter] Rename RateLimiter to RateLimiterFactory by Nyholm · Pull Request #38675 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[RateLimiter] Rename RateLimiter to RateLimiterFactory #38675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\CacheStorage;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
Expand Down Expand Up @@ -2280,7 +2280,7 @@ public static function registerRateLimiter(ContainerBuilder $container, string $
$limiterConfig['id'] = $name;
$limiter->replaceArgument(0, $limiterConfig);

$container->registerAliasForArgument($limiterId, RateLimiter::class, $name.'.limiter');
$container->registerAliasForArgument($limiterId, RateLimiterFactory::class, $name.'.limiter');
}

private function resolveTrustedHeaders(array $headers): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;

return static function (ContainerConfigurator $container) {
$container->services()
->set('cache.rate_limiter')
->parent('cache.app')
->tag('cache.pool')

->set('limiter', RateLimiter::class)
->set('limiter', RateLimiterFactory::class)
->abstract()
->args([
abstract_arg('config'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RateLimiter\RequestRateLimiterInterface;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Http\EventListener\LoginThrottlingListener;
use Symfony\Component\Security\Http\RateLimiter\DefaultLoginRateLimiter;

Expand Down Expand Up @@ -63,7 +63,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
throw new \LogicException('Login throttling requires symfony/security-http:^5.2.');
}

if (!class_exists(RateLimiter::class)) {
if (!class_exists(RateLimiterFactory::class)) {
throw new \LogicException('Login throttling requires symfony/rate-limiter to be installed and enabled.');
}

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/RateLimiter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ $ composer require symfony/rate-limiter

```php
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;

$limiter = new RateLimiter([
$limiter = new RateLimiterFactory([
'id' => 'login',
'strategy' => 'token_bucket', // or 'fixed_window'
'strategy' => 'token_bucket',
'limit' => 10,
'rate' => ['interval' => '15 minutes'],
], new InMemoryStorage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @experimental in 5.2
*/
final class RateLimiter
final class RateLimiterFactory
{
private $config;
private $storage;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/RateLimiter/Tests/LimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\StorageInterface;
use Symfony\Component\RateLimiter\TokenBucketLimiter;

Expand Down Expand Up @@ -61,6 +61,6 @@ public function testWrongInterval()

private function createFactory(array $options)
{
return new RateLimiter($options, $this->createMock(StorageInterface::class), $this->createMock(LockFactory::class));
return new RateLimiterFactory($options, $this->createMock(StorageInterface::class), $this->createMock(LockFactory::class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\RateLimiter\FixedWindowLimiter;
use Symfony\Component\RateLimiter\NoLimiter;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\SlidingWindowLimiter;
use Symfony\Component\RateLimiter\Stora 9E12 ge\InMemoryStorage;
use Symfony\Component\RateLimiter\TokenBucketLimiter;

class RateLimiterTest extends TestCase
class RateLimiterFactoryTest extends TestCase
{
/**
* @dataProvider validConfigProvider
*/
public function testValidConfig(string $expectedClass, array $config)
{
$factory = new RateLimiter($config, new InMemoryStorage());
$factory = new RateLimiterFactory($config, new InMemoryStorage());
$rateLimiter = $factory->create('key');
$this->assertInstanceOf($expectedClass, $rateLimiter);
}
Expand Down Expand Up @@ -66,7 +66,7 @@ public function validConfigProvider()
public function testInvalidConfig(string $exceptionClass, array $config)
{
$this->expectException($exceptionClass);
$factory = new RateLimiter($config, new InMemoryStorage());
$factory = new RateLimiterFactory($config, new InMemoryStorage());
$factory->create('key');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\HttpFoundation\RateLimiter\AbstractRequestRateLimiter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Core\Security;

/**
Expand All @@ -28,20 +28,20 @@
*/
final class DefaultLoginRateLimiter extends AbstractRequestRateLimiter
{
private $globalLimiter;
private $localLimiter;
private $globalFactory;
private $localFactory;

public function __construct(RateLimiter $globalLimiter, RateLimiter $localLimiter)
public function __construct(RateLimiterFactory $globalFactory, RateLimiterFactory $localFactory)
{
$this->globalLimiter = $globalLimiter;
$this->localLimiter = $localLimiter;
$this->globalFactory = $globalFactory;
$this->localFactory = $localFactory;
}

protected function getLimiters(Request $request): array
{
return [
$this->globalLimiter->create($request->getClientIp()),
$this->localLimiter->create($request->attributes->get(Security::LAST_USERNAME).$request->getClientIp()),
$this->globalFactory->create($request->getClientIp()),
$this->localFactory->create($request->attributes->get(Security::LAST_USERNAME).$request->getClientIp()),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\RateLimiter\RateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\TooManyLoginAttemptsAuthenticationException;
Expand All @@ -35,13 +35,13 @@ protected function setUp(): void
{
$this->requestStack = new RequestStack();

$localLimiter = new RateLimiter([
$localLimiter = new RateLimiterFactory([
'id' => 'login',
'strategy' => 'fixed_window',
'limit' => 3,
'interval' => '1 minute',
], new InMemoryStorage());
$globalLimiter = new RateLimiter([
$globalLimiter = new RateLimiterFactory([
'id' => 'login',
'strategy' => 'fixed_window',
'limit' => 6,
Expand Down
0