Skip to content

Commit b2bb053

Browse files
fancywebnicolas-grekas
authored andcommitted
[FrameworkBundle][HttpClient] Refactor http_client decoration strategy
1 parent 80b9b80 commit b2bb053

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,7 +2342,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
23422342
unset($options['retry_failed']);
23432343
$defaultUriTemplateVars = $options['vars'] ?? [];
23442344
unset($options['vars']);
2345-
$container->getDefinition('http_client')->setArguments([$options, $config['max_host_connections'] ?? 6]);
2345+
$container->getDefinition('http_client.transport')->setArguments([$options, $config['max_host_connections'] ?? 6]);
23462346

23472347
if (!$hasPsr18 = ContainerBuilder::willBeAvailable('psr/http-client', ClientInterface::class, ['symfony/framework-bundle', 'symfony/http-client'])) {
23482348
$container->removeDefinition('psr18.http_client');
@@ -2353,7 +2353,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
23532353
$container->removeDefinition(HttpClient::class);
23542354
}
23552355

2356-
if ($hasRetryFailed = $this->readConfigEnabled('http_client.retry_failed', $container, $retryOptions)) {
2356+
if ($this->readConfigEnabled('http_client.retry_failed', $container, $retryOptions)) {
23572357
$this->registerRetryableHttpClient($retryOptions, 'http_client', $container);
23582358
}
23592359

@@ -2371,15 +2371,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
23712371
throw new LogicException('Support for URI template requires symfony/http-client 6.3 or higher, try upgrading.');
23722372
}
23732373

2374-
$httpClientId = match (true) {
2375-
$hasUriTemplate => 'http_client.uri_template.inner',
2376-
$hasRetryFailed => 'http_client.retryable.inner',
2377-
$this->isInitializedConfigEnabled('profiler') => '.debug.http_client.inner',
2378-
default => 'http_client',
2379-
};
2380-
23812374
foreach ($config['scoped_clients'] as $name => $scopeConfig) {
2382-
if ('http_client' === $name) {
2375+
if ($container->has($name)) {
23832376
throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name));
23842377
}
23852378

@@ -2394,17 +2387,17 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
23942387

23952388
$container->register($name, ScopingHttpClient::class)
23962389
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
2397-
->setArguments([new Reference($httpClientId), $baseUri, $scopeConfig])
2390+
->setArguments([new Reference('http_client.transport'), $baseUri, $scopeConfig])
23982391
->addTag('http_client.client')
23992392
;
24002393
} else {
24012394
$container->register($name, ScopingHttpClient::class)
2402-
->setArguments([new Reference($httpClientId), [$scope => $scopeConfig], $scope])
2395+
->setArguments([new Reference('http_client.transport'), [$scope => $scopeConfig], $scope])
24032396
->addTag('http_client.client')
24042397
;
24052398
}
24062399

2407-
if ($this->readConfigEnabled('http_client.scoped_clients.'.$name.'retry_failed', $container, $retryOptions)) {
2400+
if ($this->readConfigEnabled('http_client.scoped_clients.'.$name.'.retry_failed', $container, $retryOptions)) {
24082401
$this->registerRetryableHttpClient($retryOptions, $name, $container);
24092402
}
24102403

@@ -2413,7 +2406,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
24132406
->register($name.'.uri_template', UriTemplateHttpClient::class)
24142407
->setDecoratedService($name, null, 7) // Between TraceableHttpClient (5) and RetryableHttpClient (10)
24152408
->setArguments([
2416-
new Reference('.inner'),
2409+
new Reference($name.'.uri_template.inner'),
24172410
new Reference('http_client.uri_template_expander', ContainerInterface::NULL_ON_INVALID_REFERENCE),
24182411
$defaultUriTemplateVars,
24192412
]);
@@ -2430,8 +2423,8 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
24302423
}
24312424

24322425
if ($responseFactoryId = $config['mock_response_factory'] ?? null) {
2433-
$container->register($httpClientId.'.mock_client', MockHttpClient::class)
2434-
->setDecoratedService($httpClientId, null, -10) // lower priority than TraceableHttpClient
2426+
$container->register('http_client.mock_client', MockHttpClient::class)
2427+
->setDecoratedService('http_client.transport', null, -10) // lower priority than TraceableHttpClient (5)
24352428
->setArguments([new Reference($responseFactoryId)]);
24362429
}
24372430
}
@@ -2464,7 +2457,7 @@ private function registerRetryableHttpClient(array $options, string $name, Conta
24642457

24652458
$container
24662459
->register($name.'.retryable', RetryableHttpClient::class)
2467-
->setDecoratedService($name, null, 10) // higher priority than TraceableHttpClient
2460+
->setDecoratedService($name, null, 10) // higher priority than TraceableHttpClient (5)
24682461
->setArguments([new Reference($name.'.retryable.inner'), $retryStrategy, $options['max_retries'], new Reference('logger')])
24692462
->addTag('monolog.logger', ['channel' => 'http_client']);
24702463
}

Resources/config/http_client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
return static function (ContainerConfigurator $container) {
2525
$container->services()
26-
->set('http_client', HttpClientInterface::class)
26+
->set('http_client.transport', HttpClientInterface::class)
2727
->factory([HttpClient::class, 'create'])
2828
->args([
2929
[], // default options
@@ -32,6 +32,10 @@
3232
->call('setLogger', [service('logger')->ignoreOnInvalid()])
3333
->tag('monolog.logger', ['channel' => 'http_client'])
3434
->tag('kernel.reset', ['method' => 'reset', 'on_invalid' => 'ignore'])
35+
36+
->set('http_client', HttpClientInterface::class)
37+
->factory('current')
38+
->args([[service('http_client.transport')]])
3539
->tag('http_client.client')
3640

3741
->alias(HttpClientInterface::class, 'http_client')

Tests/DependencyInjection/FrameworkExtensionTestCase.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
use Symfony\Component\HttpClient\MockHttpClient;
5656
use Symfony\Component\HttpClient\RetryableHttpClient;
5757
use Symfony\Component\HttpClient\ScopingHttpClient;
58-
use Symfony\Component\HttpClient\UriTemplateHttpClient;
5958
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
6059
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
6160
use Symfony\Component\Messenger\Transport\TransportFactory;
@@ -1846,14 +1845,14 @@ public function testRobotsTagListenerIsRegisteredInDebugMode()
18461845
public function testHttpClientDefaultOptions()
18471846
{
18481847
$container = $this->createContainerFromFile('http_client_default_options');
1849-
$this->assertTrue($container->hasDefinition('http_client'), '->registerHttpClientConfiguration() loads http_client.xml');
1848+
$this->assertTrue($container->hasDefinition('http_client.transport'), '->registerHttpClientConfiguration() loads http_client.xml');
18501849

18511850
$defaultOptions = [
18521851
'headers' => [],
18531852
'resolve' => [],
18541853
'extra' => [],
18551854
];
1856-
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client')->getArguments());
1855+
$this->assertSame([$defaultOptions, 4], $container->getDefinition('http_client.transport')->getArguments());
18571856

18581857
$this->assertTrue($container->hasDefinition('foo'), 'should have the "foo" service.');
18591858
$this->assertSame(ScopingHttpClient::class, $container->getDefinition('foo')->getClass());
@@ -1871,9 +1870,9 @@ public function testHttpClientOverrideDefaultOptions()
18711870
{
18721871
$container = $this->createContainerFromFile('http_client_override_default_options');
18731872

1874-
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['headers']);
1875-
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client')->getArgument(0)['extra']);
1876-
$this->assertSame(4, $container->getDefinition('http_client')->getArgument(1));
1873+
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client.transport')->getArgument(0)['headers']);
1874+
$this->assertSame(['foo' => 'bar'], $container->getDefinition('http_client.transport')->getArgument(0)['extra']);
1875+
$this->assertSame(4, $container->getDefinition('http_client.transport')->getArgument(1));
18771876
$this->assertSame('http://example.com', $container->getDefinition('foo')->getArgument(1));
18781877

18791878
$expected = [
@@ -1926,7 +1925,7 @@ public function testHttpClientFullDefaultOptions()
19261925
{
19271926
$container = $this->createContainerFromFile('http_client_full_default_options');
19281927

1929-
$defaultOptions = $container->getDefinition('http_client')->getArgument(0);
1928+
$defaultOptions = $container->getDefinition('http_client.transport')->getArgument(0);
19301929

19311930
$this->assertSame(['X-powered' => 'PHP'], $defaultOptions['headers']);
19321931
$this->assertSame(2, $defaultOptions['max_redirects']);
@@ -2004,15 +2003,15 @@ public function testHttpClientMockResponseFactory()
20042003
{
20052004
$container = $this->createContainerFromFile('http_client_mock_response_factory');
20062005

2007-
$definition = $container->getDefinition(($uriTemplateHttpClientExists = class_exists(UriTemplateHttpClient::class)) ? 'http_client.uri_template.inner.mock_client' : 'http_client.mock_client');
2006+
$definition = $container->getDefinition('http_client.mock_client');
20082007

20092008
$this->assertSame(MockHttpClient::class, $definition->getClass());
20102009
$this->assertCount(1, $definition->getArguments());
20112010

20122011
$argument = $definition->getArgument(0);
20132012

20142013
$this->assertInstanceOf(Reference::class, $argument);
2015-
$this->assertSame($uriTemplateHttpClientExists ? 'http_client.uri_template.inner' : 'http_client', current($definition->getDecoratedService()));
2014+
$this->assertSame('http_client.transport', current($definition->getDecoratedService()));
20162015
$this->assertSame('my_response_factory', (string) $argument);
20172016
}
20182017

0 commit comments

Comments
 (0)