-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6992da5
commit 7aa20cb
Showing
12 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/DependencyInjection/Compiler/HttpClientRequestIdPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DR\SymfonyRequestId\DependencyInjection\Compiler; | ||
|
||
use DR\SymfonyRequestId\DependencyInjection\SymfonyRequestIdExtension; | ||
use DR\SymfonyRequestId\Http\RequestIdAwareHttpClient; | ||
use DR\SymfonyRequestId\RequestIdStorageInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Parameter; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @codeCoverageIgnore - This is a config class | ||
*/ | ||
class HttpClientRequestIdPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* @SuppressWarnings(PHPMD.UnusedLocalVariable) | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->hasParameter(SymfonyRequestIdExtension::PARAMETER_KEY . '.http_client.enabled') === false || | ||
$container->getParameter(SymfonyRequestIdExtension::PARAMETER_KEY . '.http_client.enabled') === false | ||
) { | ||
return; | ||
} | ||
|
||
if ($container->getParameter(SymfonyRequestIdExtension::PARAMETER_KEY . '.http_client.tag_default_client') === true && | ||
$container->hasDefinition('http_client') | ||
) { | ||
$container->getDefinition('http_client') | ||
->addTag('http_client.request_id'); | ||
} | ||
|
||
$taggedServices = $container->findTaggedServiceIds('http_client.request_id'); | ||
|
||
foreach ($taggedServices as $id => $tag) { | ||
$container->register($id . '.request_id', RequestIdAwareHttpClient::class) | ||
->setArguments([ | ||
new Reference($id . '.request_id' . '.inner'), | ||
new Reference(RequestIdStorageInterface::class), | ||
new Parameter(SymfonyRequestIdExtension::PARAMETER_KEY . '.http_client.header') | ||
]) | ||
->setDecoratedService($id, null, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DR\SymfonyRequestId\Http; | ||
|
||
use DR\SymfonyRequestId\RequestIdStorageInterface; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
use Symfony\Contracts\HttpClient\ResponseStreamInterface; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
class RequestIdAwareHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $client, | ||
private readonly RequestIdStorageInterface $storage, | ||
private readonly string $requestIdHeader | ||
) { | ||
} | ||
|
||
public function request(string $method, string $url, array $options = []): ResponseInterface | ||
{ | ||
$options['headers'][$this->requestIdHeader] = $this->storage->getRequestId(); | ||
|
||
return $this->client->request($method, $url, $options); | ||
} | ||
|
||
public function stream(iterable|ResponseInterface $responses, ?float $timeout = null): ResponseStreamInterface | ||
{ | ||
return $this->client->stream($responses, $timeout); | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
if ($this->client instanceof ResetInterface) { | ||
$this->client->reset(); | ||
} | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger): void | ||
{ | ||
if ($this->client instanceof LoggerAwareInterface) { | ||
$this->client->setLogger($logger); | ||
} | ||
} | ||
|
||
public function withOptions(array $options): static | ||
{ | ||
$this->client = $this->client->withOptions($options); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DR\SymfonyRequestId\Tests\Functional\App\Service; | ||
|
||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class MockClientCallbackHelper | ||
{ | ||
/** | ||
* @param array{ | ||
* headers: string[] | ||
* } $options | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function __invoke(string $method, string $url, array $options): ResponseInterface | ||
{ | ||
$headers = []; | ||
|
||
foreach ($options['headers'] as $header) { | ||
[$key, $value] = explode(': ', $header); | ||
$headers[$key] = $value; | ||
} | ||
|
||
return new MockResponse('success', [ | ||
'response_headers' => $headers | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Functional; | ||
|
||
use DR\SymfonyRequestId\Tests\Functional\App\Service\TestRequestIdStorage; | ||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
#[CoversNothing] | ||
class HttpClientTest extends KernelTestCase | ||
{ | ||
public function testHttpClientIsDecorated(): void | ||
{ | ||
/** @var TestRequestIdStorage $storage */ | ||
$storage = static::getContainer()->get('request.id.storage'); | ||
/** @var HttpClientInterface $client */ | ||
$client = static::getContainer()->get('test.http_client'); | ||
|
||
$storage->setRequestId('123'); | ||
|
||
$response = $client->request('GET', 'https://example.com'); | ||
|
||
self::assertArrayHasKey('request-id', $response->getHeaders()); | ||
self::assertSame('123', $response->getHeaders()['request-id'][0]); | ||
} | ||
} |
Oops, something went wrong.