diff --git a/DependencyInjection/CompilerPass/HttpClientPass.php b/DependencyInjection/CompilerPass/HttpClientPass.php index cc1a9ac..98a7c9a 100644 --- a/DependencyInjection/CompilerPass/HttpClientPass.php +++ b/DependencyInjection/CompilerPass/HttpClientPass.php @@ -29,15 +29,13 @@ public function process(ContainerBuilder $container): void } $taggedServices = $container->findTaggedServiceIds('http_client.client'); - foreach (array_keys($taggedServices) as $id) { - $httpClientDefinition = $container->getDefinition($id); - - $decorator = new Definition(HttpClientDecorator::class); - $decorator->setDecoratedService($id, null, self::DECORATOR_PRIORITY); - $decorator->setArgument('$httpClient', $httpClientDefinition); - $decorator->setArgument('$tokenCollection', new Reference(TokenCollection::class)); - $container->setDefinition(HttpClientDecorator::class, $decorator); + foreach (array_keys($taggedServices) as $id) { + $container + ->register($id . '_decorator', HttpClientDecorator::class) + ->setDecoratedService($id, priority: self::DECORATOR_PRIORITY) + ->setArgument('$client', $container->getDefinition($id)) + ->setArgument('$tokenCollection', new Reference(TokenCollection::class)); } } } diff --git a/Tracing/Plugins/Symfony/HttpClientDecorator.php b/Tracing/Plugins/Symfony/HttpClientDecorator.php index 8cb8388..82a1ce1 100644 --- a/Tracing/Plugins/Symfony/HttpClientDecorator.php +++ b/Tracing/Plugins/Symfony/HttpClientDecorator.php @@ -5,14 +5,16 @@ namespace ETSGlobal\LogBundle\Tracing\Plugins\Symfony; use ETSGlobal\LogBundle\Tracing\TokenCollection; +use Symfony\Component\HttpClient\DecoratorTrait; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; -use Symfony\Contracts\HttpClient\ResponseStreamInterface; /** Decorates a HttpClientInterface and injects the tracing token in the request.*/ class HttpClientDecorator implements HttpClientInterface { - public function __construct(private HttpClientInterface $httpClient, private TokenCollection $tokenCollection) + use DecoratorTrait; + + public function __construct(private HttpClientInterface $client, private TokenCollection $tokenCollection) { } @@ -20,19 +22,6 @@ public function request(string $method, string $url, array $options = []): Respo { $options['headers']['X-Token-Global'] = $this->tokenCollection->getTokenValue('global'); - return $this->httpClient->request($method, $url, $options); - } - - public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface - { - return $this->httpClient->stream($responses, $timeout); - } - - public function withOptions(array $options): static - { - $clone = clone $this; - $clone->httpClient = $this->httpClient->withOptions($options); - - return $clone; + return $this->client->request($method, $url, $options); } } diff --git a/composer.json b/composer.json index 456f7e5..0edd2eb 100644 --- a/composer.json +++ b/composer.json @@ -10,13 +10,13 @@ "symfony/config": "^7.0", "symfony/console": "^7.0", "symfony/dependency-injection": "^7.0", + "symfony/http-client": "^7.0", "symfony/http-kernel": "^7.0", "symfony/monolog-bundle": "^3.10" }, "require-dev": { "etsglobal/php-static-analysis": "^3.0", - "symfony/phpunit-bridge": "^7.0", - "symfony/http-client-contracts": "^3.4" + "symfony/phpunit-bridge": "^7.0" }, "suggest": { "symfony/http-client": "Allow token forwarding through Symfony's HttpClient HTTP calls"