From c2a7b731b0d29c0f396732e306a8e6e84c11602b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20Eugon=C3=A9?= Date: Fri, 19 Apr 2024 08:37:51 +0200 Subject: [PATCH] [HttpClient] Implemented LoggerAwareInterface in HttpClientInterface decorators --- .../Component/HttpClient/CachingHttpClient.php | 11 ++++++++++- .../Component/HttpClient/EventSourceHttpClient.php | 11 ++++++++++- .../Component/HttpClient/RetryableHttpClient.php | 11 ++++++++++- .../Component/HttpClient/ThrottlingHttpClient.php | 11 ++++++++++- .../Component/HttpClient/UriTemplateHttpClient.php | 11 ++++++++++- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpClient/CachingHttpClient.php b/src/Symfony/Component/HttpClient/CachingHttpClient.php index fd6a18c3cc1b1..f576a14a00a0b 100644 --- a/src/Symfony/Component/HttpClient/CachingHttpClient.php +++ b/src/Symfony/Component/HttpClient/CachingHttpClient.php @@ -11,6 +11,8 @@ namespace Symfony\Component\HttpClient; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Response\MockResponse; use Symfony\Component\HttpClient\Response\ResponseStream; use Symfony\Component\HttpFoundation\Request; @@ -31,7 +33,7 @@ * * @author Nicolas Grekas */ -class CachingHttpClient implements HttpClientInterface, ResetInterface +class CachingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface { use HttpClientTrait; @@ -142,4 +144,11 @@ public function reset(): void $this->client->reset(); } } + + public function setLogger(LoggerInterface $logger): void + { + if ($this->client instanceof LoggerAwareInterface) { + $this->client->setLogger($logger); + } + } } diff --git a/src/Symfony/Component/HttpClient/EventSourceHttpClient.php b/src/Symfony/Component/HttpClient/EventSourceHttpClient.php index 4e551ac0409f6..7bb43532cbbc0 100644 --- a/src/Symfony/Component/HttpClient/EventSourceHttpClient.php +++ b/src/Symfony/Component/HttpClient/EventSourceHttpClient.php @@ -11,6 +11,8 @@ namespace Symfony\Component\HttpClient; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Chunk\ServerSentEvent; use Symfony\Component\HttpClient\Exception\EventSourceException; use Symfony\Component\HttpClient\Response\AsyncContext; @@ -25,7 +27,7 @@ * @author Antoine Bluchet * @author Nicolas Grekas */ -final class EventSourceHttpClient implements HttpClientInterface, ResetInterface +final class EventSourceHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface { use AsyncDecoratorTrait, HttpClientTrait { AsyncDecoratorTrait::withOptions insteadof HttpClientTrait; @@ -156,4 +158,11 @@ public function request(string $method, string $url, array $options = []): Respo } }); } + + public function setLogger(LoggerInterface $logger): void + { + if ($this->client instanceof LoggerAwareInterface) { + $this->client->setLogger($logger); + } + } } diff --git a/src/Symfony/Component/HttpClient/RetryableHttpClient.php b/src/Symfony/Component/HttpClient/RetryableHttpClient.php index d3b779420ffa9..412dfa262ab77 100644 --- a/src/Symfony/Component/HttpClient/RetryableHttpClient.php +++ b/src/Symfony/Component/HttpClient/RetryableHttpClient.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpClient; +use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Response\AsyncContext; use Symfony\Component\HttpClient\Response\AsyncResponse; @@ -27,7 +28,7 @@ * * @author Jérémy Derussé */ -class RetryableHttpClient implements HttpClientInterface, ResetInterface +class RetryableHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface { use AsyncDecoratorTrait; @@ -163,6 +164,14 @@ public function request(string $method, string $url, array $options = []): Respo }); } + public function setLogger(LoggerInterface $logger): void + { + $this->logger = $logger; + if ($this->client instanceof LoggerAwareInterface) { + $this->client->setLogger($logger); + } + } + private function getDelayFromHeader(array $headers): ?int { if (null !== $after = $headers['retry-after'][0] ?? null) { diff --git a/src/Symfony/Component/HttpClient/ThrottlingHttpClient.php b/src/Symfony/Component/HttpClient/ThrottlingHttpClient.php index 66fc173053771..d3125ff444b39 100644 --- a/src/Symfony/Component/HttpClient/ThrottlingHttpClient.php +++ b/src/Symfony/Component/HttpClient/ThrottlingHttpClient.php @@ -11,6 +11,8 @@ namespace Symfony\Component\HttpClient; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\RateLimiter\LimiterInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; @@ -19,7 +21,7 @@ /** * Limits the number of requests within a certain period. */ -class ThrottlingHttpClient implements HttpClientInterface, ResetInterface +class ThrottlingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface { use DecoratorTrait { reset as private traitReset; @@ -48,4 +50,11 @@ public function reset(): void $this->traitReset(); $this->rateLimiter->reset(); } + + public function setLogger(LoggerInterface $logger): void + { + if ($this->client instanceof LoggerAwareInterface) { + $this->client->setLogger($logger); + } + } } diff --git a/src/Symfony/Component/HttpClient/UriTemplateHttpClient.php b/src/Symfony/Component/HttpClient/UriTemplateHttpClient.php index 2767ed3687eaf..ccbd4a2d83cf3 100644 --- a/src/Symfony/Component/HttpClient/UriTemplateHttpClient.php +++ b/src/Symfony/Component/HttpClient/UriTemplateHttpClient.php @@ -11,11 +11,13 @@ namespace Symfony\Component\HttpClient; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\Service\ResetInterface; -class UriTemplateHttpClient implements HttpClientInterface, ResetInterface +class UriTemplateHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface { use DecoratorTrait; @@ -62,6 +64,13 @@ public function withOptions(array $options): static return $clone; } + public function setLogger(LoggerInterface $logger): void + { + if ($this->client instanceof LoggerAwareInterface) { + $this->client->setLogger($logger); + } + } + /** * @return \Closure(string $url, array $vars): string */