Skip to content

Commit 685e034

Browse files
committed
feature symfony#53091 [FrameworkBundle][RateLimiter] add rate_limiter tag to rate limiter services (kbond)
This PR was merged into the 7.1 branch. Discussion ---------- [FrameworkBundle][RateLimiter] add `rate_limiter` tag to rate limiter services | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix symfony#52516 | License | MIT This will allow creating tagged iterators/locators containing all registered rate limiters (indexed by their name). Commits ------- 8706f84 [RateLimiter][FrameworkBundle] add `rate_limiter` tag to rate limiter services
2 parents 1d68ce9 + 8706f84 commit 685e034

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
7.0
1112
---

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2861,7 +2861,8 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
28612861
// default configuration (when used by other DI extensions)
28622862
$limiterConfig += ['lock_factory' => 'lock.factory', 'cache_pool' => 'cache.rate_limiter'];
28632863

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

28662867
if (null !== $limiterConfig['lock_factory']) {
28672868
if (!interface_exists(LockInterface::class)) {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/PhpFrameworkExtensionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,24 @@ public function testRateLimiterLockFactory()
245245

246246
$container->getDefinition('limiter.without_lock')->getArgument(2);
247247
}
248+
249+
public function testRateLimiterIsTagged()
250+
{
251+
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
252+
$container->loadFromExtension('framework', [
253+
'annotations' => false,
254+
'http_method_override' => false,
255+
'handle_all_throwables' => true,
256+
'php_errors' => ['log' => true],
257+
'lock' => true,
258+
'rate_limiter' => [
259+
'first' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
260+
'second' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
261+
],
262+
]);
263+
});
264+
265+
$this->assertSame('first', $container->getDefinition('limiter.first')->getTag('rate_limiter')[0]['name']);
266+
$this->assertSame('second', $container->getDefinition('limiter.second')->getTag('rate_limiter')[0]['name']);
267+
}
248268
}

0 commit comments

Comments
 (0)