E606 [FrameworkBundle][RateLimiter] default `lock_factory` to `auto` by kbond · Pull Request #60099 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CHANGELOG
* Enable service argument resolution on classes that use the `#[Route]` attribute,
the `#[AsController]` attribute is no longer required
* Deprecate setting the `framework.profiler.collect_serializer_data` config option to `false`
* Set `framework.rate_limiter.limiters.*.lock_factory` to `auto` by default

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,7 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $
->children()
->scalarNode('lock_factory')
->info('The service ID of the lock factory used by this limiter (or null to disable locking).')
->defaultValue('lock.factory')
->defaultValue('auto')
->end()
->scalarNode('cache_pool')
->info('The cache pool to use for storing the current limiter state.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3239,6 +3239,10 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'))
->addTag('rate_limiter', ['name' => $name]);

if ('auto' === $limiterConfig['lock_factory']) {
$limiterConfig['lock_factory'] = $this->isInitializedConfigEnabled('lock') ? 'lock.factory' : null;
}

if (null !== $limiterConfig['lock_factory']) {
if (!interface_exists(LockInterface::class)) {
throw new LogicException(\sprintf('Rate limiter "%s" requires the Lock component to be installed. Try running "composer require symfony/lock".', $name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testWorkflowDefaultMarkingStoreDefinition()
$this->assertNull($argumentsB['index_1'], 'workflow_b marking_store argument is null');
}

public function testRateLimiterWithLockFactory()
public function testRateLimiterLockFactoryWithLockDisabled()
{
try {
$this->createContainerFromClosure(function (ContainerBuilder $container) {
Expand All @@ -199,7 +199,7 @@ public function testRateLimiterWithLockFactory()
'php_errors' => ['log' => true],
'lock' => false,
'rate_limiter' => [
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour', 'lock_factory' => 'lock.factory'],
],
]);
});
Expand All @@ -208,7 +208,10 @@ public function testRateLimiterWithLockFactory()
} catch (LogicException $e) {
$this->assertEquals('Rate limiter "with_lock" requires the Lock component to be configured.', $e->getMessage());
}
}

public function testRateLimiterAutoLockFactoryWithLockEnabled()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'annotations' => false,
Expand All @@ -226,13 +229,35 @@ public function testRateLimiterWithLockFactory()
$this->assertEquals('lock.factory', (string) $withLock->getArgument(2));
}

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

$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessageMatches('/^The argument "2" doesn\'t exist.*\.$/');

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

public function testRateLimiterDisableLockFactory()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'lock' => true,
'php_errors' => ['log' => true],
'rate_limiter' => [
'without_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour', 'lock_factory' => null],
Expand Down
Loading
0