Skip to content

Commit 5bb8918

Browse files
committed
[HttpClient] Fix caching client docrating scoping client
1 parent 929e82e commit 5bb8918

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,19 +2943,31 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
29432943
private function registerCachingHttpClient(array $options, array $defaultOptions, string $name, ContainerBuilder $container): void
29442944
{
29452945
if (!class_exists(ChunkCacheItemNotFoundException::class)) {
2946-
throw new LogicException('Caching cannot be enabled as version 7.3+ of the HttpClient component is required.');
2946+
throw new LogicException('Caching cannot be enabled as version 7.4+ of the HttpClient component is required.');
29472947
}
29482948

29492949
$container
29502950
->register($name.'.caching', CachingHttpClient::class)
2951-
->setDecoratedService($name, null, 13) // between RetryableHttpClient (10) and ThrottlingHttpClient (15)
29522951
->setArguments([
29532952
new Reference($name.'.caching.inner'),
29542953
new Reference($options['cache_pool']),
29552954
$defaultOptions,
29562955
$options['shared'],
29572956
$options['max_ttl'],
29582957
]);
2958+
2959+
$httpClient = $container->getDefinition($name);
2960+
2961+
if (ScopingHttpClient::class === $httpClient->getClass()) {
2962+
$httpClient->setDecoratedService($name.'.caching', null, 13); // between RetryableHttpClient (10) and ThrottlingHttpClient (15)
2963+
2964+
$container->getDefinition($name.'.caching')
2965+
->replaceArgument(0, $httpClient->getArgument(0));
2966+
} else {
2967+
$container
2968+
->getDefinition($name.'.caching')
2969+
->setDecoratedService($name, null, 13); // between RetryableHttpClient (10) and ThrottlingHttpClient (15)
2970+
}
29592971
}
29602972

29612973
private function registerThrottlingHttpClient(string $rateLimiter, string $name, ContainerBuilder $container): void

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)