Skip to content

Commit

Permalink
Fix the HTTP client decoration when no http_client service is regis…
Browse files Browse the repository at this point in the history
…tered (#792)
  • Loading branch information
ste93cry authored Dec 6, 2023
1 parent 7a0751f commit ce12448
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/DependencyInjection/Compiler/HttpClientTracingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ public function process(ContainerBuilder $container): void

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

if (null === $decoratedService) {
return;
}

$container->register(TraceableHttpClient::class, TraceableHttpClient::class)
->setArgument(0, new Reference(TraceableHttpClient::class . '.inner'))
->setArgument(1, new Reference(HubInterface::class))
->setDecoratedService($decoratedService[0], null, $decoratedService[1]);
}

/**
* @return array{string, int}
* @return array{string, int}|null
*/
private function getDecoratedService(ContainerBuilder $container): array
private function getDecoratedService(ContainerBuilder $container): ?array
{
// Starting from Symfony 6.3, the raw HTTP client that serves as adapter
// for the transport is registered as a separate service, so that the
Expand All @@ -66,6 +70,10 @@ private function getDecoratedService(ContainerBuilder $container): array
}
}

return ['http_client', 15];
if ($container->hasDefinition('http_client')) {
return ['http_client', 15];
}

return null;
}
}
16 changes: 13 additions & 3 deletions tests/DependencyInjection/Compiler/HttpClientTracingPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function processDataProvider(): \Generator
];
}

public function testProcessDoesNothingIfHttpClientServiceCannotBeFound(): void
{
$container = $this->createContainerBuilder(true, true, null);
$container->compile();

$this->assertFalse($container->hasDefinition('http_client'));
}

/**
* @dataProvider processDoesNothingIfConditionsForEnablingTracingAreMissingDataProvider
*/
Expand Down Expand Up @@ -86,7 +94,7 @@ public function processDoesNothingIfConditionsForEnablingTracingAreMissingDataPr
];
}

private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClientTracingEnabled, string $httpClientServiceId): ContainerBuilder
private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClientTracingEnabled, ?string $httpClientServiceId): ContainerBuilder
{
$container = new ContainerBuilder();
$container->addCompilerPass(new HttpClientTracingPass());
Expand All @@ -96,8 +104,10 @@ private function createContainerBuilder(bool $isTracingEnabled, bool $isHttpClie
$container->register(HubInterface::class, HubInterface::class)
->setPublic(true);

$container->register($httpClientServiceId, HttpClientInterface::class)
->setPublic(true);
if (null !== $httpClientServiceId) {
$container->register($httpClientServiceId, HttpClientInterface::class)
->setPublic(true);
}

return $container;
}
Expand Down

0 comments on commit ce12448

Please sign in to comment.