Skip to content

Fix the HTTP client decoration when no http_client service is registered #792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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