|
10 | 10 | use Http\Client\Common\HttpMethodsClient; |
11 | 11 | use Http\Client\Common\HttpMethodsClientInterface; |
12 | 12 | use Http\Client\Common\Plugin\AuthenticationPlugin; |
| 13 | +use Http\Client\Common\Plugin\ThrottlePlugin; |
13 | 14 | use Http\Client\Common\PluginClient; |
14 | 15 | use Http\Client\Common\PluginClientFactory; |
15 | 16 | use Http\Client\HttpAsyncClient; |
|
30 | 31 | use Symfony\Component\DependencyInjection\ChildDefinition; |
31 | 32 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
32 | 33 | use Symfony\Component\DependencyInjection\Definition; |
| 34 | +use Symfony\Component\DependencyInjection\Extension\Extension; |
33 | 35 | use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
34 | 36 | use Symfony\Component\DependencyInjection\Reference; |
35 | | -use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 37 | +use Symfony\Component\HttpKernel\Kernel; |
| 38 | +use Symfony\Component\RateLimiter\LimiterInterface; |
36 | 39 | use Twig\Environment as TwigEnvironment; |
37 | 40 |
|
38 | 41 | /** |
@@ -284,6 +287,24 @@ private function configurePluginByName($name, Definition $definition, array $con |
284 | 287 |
|
285 | 288 | break; |
286 | 289 |
|
| 290 | + case 'throttle': |
| 291 | + if (!\class_exists(ThrottlePlugin::class)) { |
| 292 | + throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".'); |
| 293 | + } |
| 294 | + |
| 295 | + $key = $config['name'] ? '.'.$config['name'] : ''; |
| 296 | + $container |
| 297 | + ->register($serviceId.$key, LimiterInterface::class) |
| 298 | + ->setFactory([new Reference('limiter.'.$config['name']), 'create']) |
| 299 | + ->addArgument($config['key']) |
| 300 | + ->setPublic(false); |
| 301 | + |
| 302 | + $definition->replaceArgument(0, new Reference($serviceId.$key)); |
| 303 | + $definition->setArgument('$tokens', $config['tokens']); |
| 304 | + $definition->setArgument('$maxTime', $config['max_time']); |
| 305 | + |
| 306 | + break; |
| 307 | + |
287 | 308 | /* client specific plugins */ |
288 | 309 |
|
289 | 310 | case 'add_host': |
|
0 commit comments