Skip to content

Commit a8ef44a

Browse files
committed
Merge remote-tracking branch 'origin/1.x' into update-1-to-2
2 parents 734a76e + 87c61d2 commit a8ef44a

File tree

9 files changed

+115
-21
lines changed

9 files changed

+115
-21
lines changed

.github/workflows/continuous-integration.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- dependencies: "php-http/guzzle7-adapter"
4444
php-version: "8.2"
4545
symfony-deprecations-helper: "weak"
46-
- dependencies: "php-http/guzzle7-adapter"
46+
- dependencies: "php-http/guzzle7-adapter php-http/throttle-plugin"
4747
php-version: "8.3"
4848
symfony-deprecations-helper: "weak"
4949

@@ -78,7 +78,7 @@ jobs:
7878

7979
steps:
8080
- name: "Checkout"
81-
uses: "actions/checkout@v3"
81+
uses: "actions/checkout@v4"
8282
with:
8383
fetch-depth: 2
8484

@@ -88,18 +88,18 @@ jobs:
8888
php-version: "${{ matrix.php-version }}"
8989
coverage: "pcov"
9090
ini-values: "zend.assertions=1"
91+
tools: "flex"
9192

9293
- name: "Enforce using stable dependencies"
9394
run: "composer config minimum-stability stable"
9495
if: "${{ matrix.stability == 'stable' }}"
9596

9697
- name: "Add dependencies and enable flex"
9798
run: |
98-
composer require --no-update symfony/flex ${{ matrix.dependencies }}
99-
composer config --no-plugins allow-plugins.symfony/flex true
99+
composer require --no-update ${{ matrix.dependencies }}
100100
101101
- name: "Install dependencies with Composer"
102-
uses: "ramsey/composer-install@v2"
102+
uses: "ramsey/composer-install@v3"
103103
with:
104104
dependency-versions: "${{ matrix.dependency-versions }}"
105105
composer-options: "${{ matrix.composer-options }}"

.github/workflows/php-cs-fixer.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
steps:
1717
- name: "Checkout"
18-
uses: "actions/checkout@v3"
18+
uses: "actions/checkout@v4"
1919
- name: "PHP-CS-Fixer"
2020
uses: "docker://oskarstark/php-cs-fixer-ga:2.19.0"
2121
with:

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
1919

2020
# Version 1
2121

22+
# 1.34.3 - 2024-09-01
23+
24+
- Same as 1.34.2 but tagged on the 1.x banch instead of the feature branch.
25+
26+
# 1.34.2 - 2024-09-01
27+
28+
- More cleanup of the rate-limiter configuration. The service name is the full service name of the rate limiter, e.g. `limiter.my_name` when configuring `framework.rate_limiter.my_name`.
29+
30+
# 1.34.1 - 2024-09-01
31+
32+
- The rate-limiter name in the throttle plugin configuration is required.
33+
34+
# 1.34.0 - 2024-06-17
35+
36+
- Support to configure the throttle plugin.
37+
38+
# 1.33.1 - 2024-05-27
39+
40+
- Fixed extension to depend on the DependencyInjection component rather than the HttpKernel.
41+
2242
# 1.33.0 - 2024-02-27
2343

2444
- Support php-http/cache-plugin 2.0 and bump minimal version to 1.7 by defaulting the stream factory for cache to `httplug.psr17_stream_factory` (#448).

composer.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,24 @@
4747
"php-http/curl-client": "<2.0",
4848
"php-http/socket-client": "<2.0",
4949
"kriswallsmith/buzz": "<0.17",
50-
"php-http/react-adapter": "<3.0"
50+
"php-http/react-adapter": "<3.0",
51+
"php-http/throttle-plugin": "<1.1"
5152
},
5253
"require-dev": {
5354
"guzzlehttp/psr7": "^1.7 || ^2.0",
54-
"matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0",
55+
"matthiasnoback/symfony-config-test": "^4.3 || ^5.0",
56+
"matthiasnoback/symfony-dependency-injection-test": "^4.3.1 || ^5.0",
5557
"nyholm/nsa": "^1.1",
5658
"nyholm/psr7": "^1.2.1",
5759
"php-http/cache-plugin": "^1.7",
5860
"php-http/mock-client": "^1.2",
5961
"php-http/promise": "^1.0",
62+
"phpunit/phpunit": "^9.6",
6063
"symfony/browser-kit": "^5.4 || ^6.0 || ^7.0",
6164
"symfony/cache": "^5.4 || ^6.0 || ^7.0",
6265
"symfony/dom-crawler": "^5.4 || ^6.0 || ^7.0",
6366
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0",
6467
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
65-
"symfony/phpunit-bridge": "^7.0.3",
6668
"symfony/stopwatch": "^5.4 || ^6.0 || ^7.0",
6769
"symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0",
6870
"symfony/web-profiler-bundle": "^5.4 || ^6.0 || ^7.0",
@@ -95,7 +97,7 @@
9597
},
9698
"prefer-stable": false,
9799
"scripts": {
98-
"test": "vendor/bin/simple-phpunit",
99-
"test-ci": "vendor/bin/simple-phpunit --coverage-text --coverage-clover=build/coverage.xml"
100+
"test": "vendor/bin/phpunit",
101+
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
100102
}
101103
}

src/DependencyInjection/Configuration.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,38 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
611611
->end();
612612
// End stopwatch plugin
613613

614-
$error = $children->arrayNode('error')
614+
$children->arrayNode('error')
615615
->canBeEnabled()
616616
->addDefaultsIfNotSet()
617617
->children()
618618
->scalarNode('only_server_exception')->defaultFalse()->end()
619619
->end()
620620
->end();
621621
// End error plugin
622+
623+
$children->arrayNode('throttle')
624+
->canBeEnabled()
625+
->addDefaultsIfNotSet()
626+
->children()
627+
->scalarNode('name')
628+
->isRequired()
629+
->info('Rate limiter service name from symfony/rate-limiter configuration. E.g. for a rate limiter http_client you specify limiter.http_client here')
630+
->end()
631+
->scalarNode('key')
632+
->defaultNull()
633+
->info('Key to avoid sharing this rate limiter with other clients or other services. You can use the name of the client for example.')
634+
->end()
635+
->integerNode('tokens')
636+
->defaultValue(1)
637+
->info('How many tokens spending per request')
638+
->end()
639+
->floatNode('max_time')
640+
->defaultNull()
641+
->info('Maximum accepted waiting time in seconds')
642+
->end()
643+
->end()
644+
->end();
645+
// End throttle plugin
622646
}
623647

624648
/**

src/DependencyInjection/HttplugExtension.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Http\Client\Common\HttpMethodsClient;
1111
use Http\Client\Common\HttpMethodsClientInterface;
1212
use Http\Client\Common\Plugin\AuthenticationPlugin;
13+
use Http\Client\Common\Plugin\ThrottlePlugin;
1314
use Http\Client\Common\PluginClient;
1415
use Http\Client\Common\PluginClientFactory;
1516
use Http\Client\HttpAsyncClient;
@@ -30,9 +31,10 @@
3031
use Symfony\Component\DependencyInjection\ChildDefinition;
3132
use Symfony\Component\DependencyInjection\ContainerBuilder;
3233
use Symfony\Component\DependencyInjection\Definition;
34+
use Symfony\Component\DependencyInjection\Extension\Extension;
3335
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3436
use Symfony\Component\DependencyInjection\Reference;
35-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
37+
use Symfony\Component\RateLimiter\LimiterInterface;
3638
use Twig\Environment as TwigEnvironment;
3739

3840
/**
@@ -284,6 +286,24 @@ private function configurePluginByName($name, Definition $definition, array $con
284286

285287
break;
286288

289+
case 'throttle':
290+
if (!\class_exists(ThrottlePlugin::class)) {
291+
throw new InvalidConfigurationException('You need to require the Throttle Plugin to be able to use it: "composer require php-http/throttle-plugin".');
292+
}
293+
294+
$limiterServiceId = $serviceId.'.'.$config['name'];
295+
$container
296+
->register($limiterServiceId, LimiterInterface::class)
297+
->setFactory([new Reference($config['name']), 'create'])
298+
->addArgument($config['key'])
299+
->setPublic(false);
300+
301+
$definition->replaceArgument(0, new Reference($limiterServiceId));
302+
$definition->setArgument('$tokens', $config['tokens']);
303+
$definition->setArgument('$maxTime', $config['max_time']);
304+
305+
break;
306+
287307
/* client specific plugins */
288308

289309
case 'add_host':

src/Resources/config/plugins.xml

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<service id="httplug.plugin.stopwatch" class="Http\Client\Common\Plugin\StopwatchPlugin" public="false" abstract="true">
2929
<argument />
3030
</service>
31+
<service id="httplug.plugin.throttle" class="Http\Client\Common\Plugin\ThrottlePlugin" public="false" abstract="true">
32+
<argument />
33+
</service>
3134

3235
<!-- client specific plugin definition prototypes -->
3336

tests/Unit/DependencyInjection/ConfigurationTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
8989
'enabled' => false,
9090
'only_server_exception' => false,
9191
],
92+
'throttle' => [
93+
'enabled' => false,
94+
'key' => null,
95+
'tokens' => 1,
96+
'max_time' => null,
97+
],
9298
],
9399
'discovery' => [
94100
'client' => 'auto',
@@ -293,6 +299,12 @@ public function testSupportsAllConfigFormats(): void
293299
'enabled' => false,
294300
'only_server_exception' => false,
295301
],
302+
'throttle' => [
303+
'enabled' => false,
304+
'key' => null,
305+
'tokens' => 1,
306+
'max_time' => null,
307+
],
296308
],
297309
'discovery' => [
298310
'client' => 'auto',

tests/Unit/DependencyInjection/HttplugExtensionTest.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;
66

77
use Http\Adapter\Guzzle7\Client;
8+
use Http\Client\Common\Plugin\ThrottlePlugin;
89
use Http\Client\Plugin\Vcr\Recorder\InMemoryRecorder;
910
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
1011
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
@@ -80,7 +81,7 @@ public function testConfigLoadService(): void
8081

8182
public function testClientPlugins(): void
8283
{
83-
$this->load([
84+
$config = [
8485
'clients' => [
8586
'acme' => [
8687
'factory' => 'httplug.factory.curl',
@@ -131,6 +132,11 @@ public function testClientPlugins(): void
131132
'headers' => ['X-FOO'],
132133
],
133134
],
135+
[
136+
'query_defaults' => [
137+
'parameters' => ['locale' => 'en'],
138+
],
139+
],
134140
[
135141
'request_seekable_body' => [
136142
'use_file_buffer' => true,
@@ -139,11 +145,6 @@ public function testClientPlugins(): void
139145
[
140146
'response_seekable_body' => true,
141147
],
142-
[
143-
'query_defaults' => [
144-
'parameters' => ['locale' => 'en'],
145-
],
146-
],
147148
[
148149
'authentication' => [
149150
'my_basic' => [
@@ -166,7 +167,16 @@ public function testClientPlugins(): void
166167
],
167168
],
168169
],
169-
]);
170+
];
171+
if (class_exists(ThrottlePlugin::class)) {
172+
$config['clients']['acme']['plugins'][] = [
173+
'throttle' => [
174+
'name' => 'limiter.test',
175+
],
176+
];
177+
}
178+
179+
$this->load($config);
170180

171181
$plugins = [
172182
'httplug.client.acme.plugin.decoder',
@@ -179,13 +189,16 @@ public function testClientPlugins(): void
179189
'httplug.client.acme.plugin.header_defaults',
180190
'httplug.client.acme.plugin.header_set',
181191
'httplug.client.acme.plugin.header_remove',
192+
'httplug.client.acme.plugin.query_defaults',
182193
'httplug.client.acme.plugin.request_seekable_body',
183194
'httplug.client.acme.plugin.response_seekable_body',
184-
'httplug.client.acme.plugin.query_defaults',
185195
'httplug.client.acme.authentication.my_basic',
186196
'httplug.client.acme.plugin.cache',
187197
'httplug.client.acme.plugin.error',
188198
];
199+
if (\class_exists(ThrottlePlugin::class)) {
200+
$plugins[] = 'httplug.client.acme.plugin.throttle';
201+
}
189202
$pluginReferences = array_map(function ($id) {
190203
return new Reference($id);
191204
}, $plugins);

0 commit comments

Comments
 (0)