Skip to content

Commit ce12448

Browse files
authored
Fix the HTTP client decoration when no http_client service is registered (#792)
1 parent 7a0751f commit ce12448

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/DependencyInjection/Compiler/HttpClientTracingPass.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ public function process(ContainerBuilder $container): void
3434

3535
$decoratedService = $this->getDecoratedService($container);
3636

37+
if (null === $decoratedService) {
38+
return;
39+
}
40+
3741
$container->register(TraceableHttpClient::class, TraceableHttpClient::class)
3842
->setArgument(0, new Reference(TraceableHttpClient::class . '.inner'))
3943
->setArgument(1, new Reference(HubInterface::class))
4044
->setDecoratedService($decoratedService[0], null, $decoratedService[1]);
4145
}
4246

4347
/**
44-
* @return array{string, int}
48+
* @return array{string, int}|null
4549
*/
46-
private function getDecoratedService(ContainerBuilder $container): array
50+
private function getDecoratedService(ContainerBuilder $container): ?array
4751
{
4852
// Starting from Symfony 6.3, the raw HTTP client that serves as adapter
4953
// for the transport is registered as a separate service, so that the
@@ -66,6 +70,10 @@ private function getDecoratedService(ContainerBuilder $container): array
6670
}
6771
}
6872

69-
return ['http_client', 15];
73+
if ($container->hasDefinition('http_client')) {
74+
return ['http_client', 15];
75+
}
76+
77+
return null;
7078
}
7179
}

tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public function processDataProvider(): \Generator
5454
];
5555
}
5656

57+
public function testProcessDoesNothingIfHttpClientServiceCannotBeFound(): void
58+
{
59+
$container = $this->createContainerBuilder(true, true, null);
60+
$container->compile();
61+
62+
$this->assertFalse($container->hasDefinition('http_client'));
63+
}
64+
5765
/**
5866
* @dataProvider processDoesNothingIfConditionsForEnablingTracingAreMissingDataProvider
5967
*/
@@ -86,7 +94,7 @@ public function processDoesNothingIfConditionsForEnablingTracingAreMissingDataPr
8694
];
8795
}
8896

89-
private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClientTracingEnabled, string $httpClientServiceId): ContainerBuilder
97+
private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClientTracingEnabled, ?string $httpClientServiceId): ContainerBuilder
9098
{
9199
$container = new ContainerBuilder();
92100
$container->addCompilerPass(new HttpClientTracingPass());
@@ -96,8 +104,10 @@ private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClie
96104
$container->register(HubInterface::class, HubInterface::class)
97105
->setPublic(true);
98106

99-
$container->register($httpClientServiceId, HttpClientInterface::class)
100-
->setPublic(true);
107+
if (null !== $httpClientServiceId) {
108+
$container->register($httpClientServiceId, HttpClientInterface::class)
109+
->setPublic(true);
110+
}
101111

102112
return $container;
103113
}

0 commit comments

Comments
 (0)