Skip to content

Commit aeb55c4

Browse files
pierredupnicolas-grekas
authored andcommitted
[HttpClient] Fix caching client decorating scoping client
1 parent 929e82e commit aeb55c4

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e
20762076
->acceptAndWrap(['string'], 'base_uri')
20772077
->validate()
20782078
->ifTrue(static fn () => !class_exists(HttpClient::class))
2079-
->then(static fn () => 'HttpClient support cannot be enabled as the component is not installed. Try running "composer require symfony/http-client".')
2079+
->then(static fn () => throw new LogicException('HttpClient support cannot be enabled as the component is not installed. Try running "composer require symfony/http-client".'))
20802080
->end()
20812081
->validate()
20822082
->ifTrue(static fn ($v) => !isset($v['scope']) && !isset($v['base_uri']))

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,39 +2877,45 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
28772877
$retryOptions = $scopeConfig['retry_failed'] ?? ['enabled' => false];
28782878
unset($scopeConfig['retry_failed']);
28792879

2880+
$transport = $name.'.transport';
2881+
$container->register($transport, HttpClientInterface::class)
2882+
->setFactory('current')
2883+
->setArguments([[new Reference('http_client.transport')]])
2884+
;
2885+
28802886
if (null === $scope) {
28812887
$baseUri = $scopeConfig['base_uri'];
28822888
unset($scopeConfig['base_uri']);
28832889

28842890
$container->register($name, ScopingHttpClient::class)
28852891
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
2886-
->setArguments([new Reference('http_client.transport'), $baseUri, $scopeConfig])
2892+
->setArguments([new Reference($transport), $baseUri, $scopeConfig])
28872893
->addTag('http_client.client')
28882894
->addTag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
28892895
;
28902896
} else {
28912897
$container->register($name, ScopingHttpClient::class)
2892-
->setArguments([new Reference('http_client.transport'), [$scope => $scopeConfig], $scope])
2898+
->setArguments([new Reference($transport), [$scope => $scopeConfig], $scope])
28932899
->addTag('http_client.client')
28942900
->addTag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
28952901
;
28962902
}
28972903

28982904
if ($this->readConfigEnabled('http_client.scoped_clients.'.$name.'.caching', $container, $cachingOptions)) {
2899-
$this->registerCachingHttpClient($cachingOptions, $scopeConfig, $name, $container);
2905+
$this->registerCachingHttpClient($cachingOptions, $scopeConfig, $transport, $container);
29002906
}
29012907

29022908
if (null !== $rateLimiter) {
2903-
$this->registerThrottlingHttpClient($rateLimiter, $name, $container);
2909+
$this->registerThrottlingHttpClient($rateLimiter, $transport, $container);
29042910
}
29052911

29062912
if ($this->readConfigEnabled('http_client.scoped_clients.'.$name.'.retry_failed', $container, $retryOptions)) {
2907-
$this->registerRetryableHttpClient($retryOptions, $name, $container);
2913+
$this->registerRetryableHttpClient($retryOptions, $transport, $container);
29082914
}
29092915

29102916
$container
29112917
->register($name.'.uri_template', UriTemplateHttpClient::class)
2912-
->setDecoratedService($name, null, 7) // Between TraceableHttpClient (5) and RetryableHttpClient (10)
2918+
->setDecoratedService($name, null, 7) // After TraceableHttpClient (5) and on top of ScopingHttpClient
29132919
->setArguments([
29142920
new Reference($name.'.uri_template.inner'),
29152921
new Reference('http_client.uri_template_expander', ContainerInterface::NULL_ON_INVALID_REFERENCE),
@@ -2943,7 +2949,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
29432949
private function registerCachingHttpClient(array $options, array $defaultOptions, string $name, ContainerBuilder $container): void
29442950
{
29452951
if (!class_exists(ChunkCacheItemNotFoundException::class)) {
2946-
throw new LogicException('Caching cannot be enabled as version 7.3+ of the HttpClient component is required.');
2952+
throw new LogicException('Caching cannot be enabled as version 7.4+ of the HttpClient component is required.');
29472953
}
29482954

29492955
$container

src/Symfony/Bundle/FrameworkBundle/Resources/config/http_client.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
->factory('current')
4949
->args([[service('http_client.transport')]])
5050
->tag('http_client.client')
51-
->tag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
5251

5352
->alias(HttpClientInterface::class, 'http_client')
5453

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,12 +2298,15 @@ public function testCachingHttpClient()
22982298
$this->assertTrue($container->hasDefinition('bar.caching'));
22992299
$definition = $container->getDefinition('bar.caching');
23002300
$this->assertSame(CachingHttpClient::class, $definition->getClass());
2301-
$this->assertSame('bar', $definition->getDecoratedService()[0]);
23022301
$arguments = $definition->getArguments();
23032302
$this->assertInstanceOf(Reference::class, $arguments[0]);
2304-
$this->assertSame('bar.caching.inner', (string) $arguments[0]);
2303+
$this->assertSame('http_client.transport', (string) $arguments[0]);
23052304
$this->assertInstanceOf(Reference::class, $arguments[1]);
23062305
$this->assertSame('baz', (string) $arguments[1]);
2306+
$scopedClient = $container->getDefinition('bar');
2307+
2308+
$this->assertSame('http_client.transport', (string) $scopedClient->getArgument(0));
2309+
$this->assertSame('bar.caching', $scopedClient->getDecoratedService()[0]);
23072310
}
23082311

23092312
public function testHttpClientRetry()

0 commit comments

Comments
 (0)