Skip to content

[RateLimiter] Add RateLimiterBuilder #60084

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

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
frameworkbundle configuration/tests
  • Loading branch information
kbond committed Apr 4, 2025
commit 9fa23d52f7e088edb97f7de429fc5185d1d00fd6
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,24 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $
})
->end()
->children()
->arrayNode('builder')
->info('Configuration for the RateLimiterBuilder service.')
->addDefaultsIfNotSet()
->children()
->scalarNode('lock_factory')
->info('The service ID of the lock factory to use with the RateLimiterBuilder.')
->defaultValue('auto')
->end()
->scalarNode('cache_pool')
->info('The cache pool to use with RateLimiterBuilder.')
->defaultValue('cache.rate_limiter')
->end()
->scalarNode('storage_service')
->info('The service ID of a custom storage implementation, this precedes any configured "cache_pool".')
->defaultNull()
->end()
->end()
->end()
->arrayNode('limiters')
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\RateLimiterBuilder;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\RateLimiterFactoryInterface;
use Symfony\Component\RateLimiter\Storage\CacheStorage;
Expand Down Expand Up @@ -3272,6 +3273,38 @@ private function registerRateLimiterConfiguration(array $config, ContainerBuilde
$container->registerAliasForArgument($limiterId, RateLimiterFactoryInterface::class, $name.'.limiter');
}
}

if (class_exists(RateLimiterBuilder::class)) {
$builderConfig = $config['builder'];

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

$builder = $container->getDefinition('limiter_builder');

if (null === $storageId = $builderConfig['storage_service'] ?? null) {
$container->register($storageId = 'limiter_builder.storage', CacheStorage::class)->addArgument(new Reference($builderConfig['cache_pool']));
}

$builder->replaceArgument(0, new Reference($storageId));

if ($builderConfig['lock_factory']) {
if (!interface_exists(LockInterface::class)) {
throw new LogicException('Rate Limiter Builder requires the Lock component to be installed. Try running "composer require symfony/lock".');
}

if (!$this->isInitializedConfigEnabled('lock')) {
throw new LogicException('Rate Limiter Builder requires the Lock component to be configured.');
}

$builder->replaceArgument(1, new Reference($builderConfig['lock_factory']));
}

$container->setAlias(RateLimiterBuilder::class, 'limiter_builder');
} else {
$container->removeDefinition('limiter_builder');
}
}

private function registerUidConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\RateLimiter\RateLimiterBuilder;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\CacheStorage;

return static function (ContainerConfigurator $container) {
$container->services()
Expand All @@ -26,5 +28,11 @@
abstract_arg('storage'),
null,
])

->set('limiter_builder', RateLimiterBuilder::class)
->args([
abstract_arg('storage'),
null,
])
;
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\RateLimiter\RateLimiterBuilder;
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;

class PhpFrameworkExtensionTest extends FrameworkExtensionTestCase
Expand Down Expand Up @@ -290,4 +291,42 @@ public function testRateLimiterIsTagged()
$this->assertSame('first', $container->getDefinition('limiter.first')->getTag('rate_limiter')[0]['name']);
$this->assertSame('second', $container->getDefinition('limiter.second')->getTag('rate_limiter')[0]['name']);
}

public function testRateLimiterBuilderDefault()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'rate_limiter' => true,
]);
});

$this->assertSame('cache.rate_limiter', (string) $container->getDefinition('limiter_builder.storage')->getArgument(0));

$builder = $container->getDefinition('limiter_builder');
$this->assertSame('limiter_builder.storage', (string) $builder->getArgument(0));
$this->assertNull($builder->getArgument(1));

$this->assertSame('limiter_builder', (string) $container->getAlias(RateLimiterBuilder::class));
}

public function testRateLimiterBuilderDefaultWithLock()
{
$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' => true,
]);
});

$builder = $container->getDefinition('limiter_builder');
$this->assertSame('lock.factory', (string) $builder->getArgument(1));
}
}