Skip to content

Commit

Permalink
fix: EB-0 Fix decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
aallier committed Sep 24, 2024
1 parent 924a576 commit 9887224
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
14 changes: 6 additions & 8 deletions DependencyInjection/CompilerPass/HttpClientPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
21 changes: 5 additions & 16 deletions Tracing/Plugins/Symfony/HttpClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,23 @@
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)
{
}

public function request(string $method, string $url, array $options = []): ResponseInterface
{
$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);
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9887224

Please sign in to comment.