Skip to content

Commit

Permalink
[RateLimiter][FrameworkBundle] add rate_limiter tag to rate limiter…
Browse files Browse the repository at this point in the history
… services
  • Loading branch information
kbond committed Dec 15, 2023
1 parent 0af28a0 commit 8706f84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Move the Router `cache_dir` to `kernel.build_dir`
* Deprecate the `router.cache_dir` config option
* Add `rate_limiter` tags to rate limiter services

7.0
---
Expand Down
Expand Up @@ -2860,7 +2860,8 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
// default configuration (when used by other DI extensions)
$limiterConfig += ['lock_factory' => 'lock.factory', 'cache_pool' => 'cache.rate_limiter'];

$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'));
$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'))
->addTag('rate_limiter', ['name' => $name]);

if (null !== $limiterConfig['lock_factory']) {
if (!interface_exists(LockInterface::class)) {
Expand Down
Expand Up @@ -245,4 +245,24 @@ public function testRateLimiterLockFactory()

$container->getDefinition('limiter.without_lock')->getArgument(2);
}

public function testRateLimiterIsTagged()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'lock' => true,
'rate_limiter' => [
'first' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
'second' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
],
]);
});

$this->assertSame('first', $container->getDefinition('limiter.first')->getTag('rate_limiter')[0]['name']);
$this->assertSame('second', $container->getDefinition('limiter.second')->getTag('rate_limiter')[0]['name']);
}
}

0 comments on commit 8706f84

Please sign in to comment.