Skip to content

Commit 1356158

Browse files
yann-eugonenicolas-grekas
authored andcommitted
[HttpClient] Implemented LoggerAwareInterface in HttpClientInterface decorators
1 parent 899ece2 commit 1356158

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

CachingHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Component\HttpClient\Response\MockResponse;
1517
use Symfony\Component\HttpClient\Response\ResponseStream;
1618
use Symfony\Component\HttpFoundation\Request;
@@ -31,7 +33,7 @@
3133
*
3234
* @author Nicolas Grekas <[email protected]>
3335
*/
34-
class CachingHttpClient implements HttpClientInterface, ResetInterface
36+
class CachingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
3537
{
3638
use HttpClientTrait;
3739

@@ -142,4 +144,11 @@ public function reset(): void
142144
$this->client->reset();
143145
}
144146
}
147+
148+
public function setLogger(LoggerInterface $logger): void
149+
{
150+
if ($this->client instanceof LoggerAwareInterface) {
151+
$this->client->setLogger($logger);
152+
}
153+
}
145154
}

EventSourceHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1517
use Symfony\Component\HttpClient\Exception\EventSourceException;
1618
use Symfony\Component\HttpClient\Response\AsyncContext;
@@ -25,7 +27,7 @@
2527
* @author Antoine Bluchet <[email protected]>
2628
* @author Nicolas Grekas <[email protected]>
2729
*/
28-
final class EventSourceHttpClient implements HttpClientInterface, ResetInterface
30+
final class EventSourceHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
2931
{
3032
use AsyncDecoratorTrait, HttpClientTrait {
3133
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;
@@ -156,4 +158,11 @@ public function request(string $method, string $url, array $options = []): Respo
156158
}
157159
});
158160
}
161+
162+
public function setLogger(LoggerInterface $logger): void
163+
{
164+
if ($this->client instanceof LoggerAwareInterface) {
165+
$this->client->setLogger($logger);
166+
}
167+
}
159168
}

RetryableHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerAwareInterface;
1415
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\HttpClient\Response\AsyncContext;
1617
use Symfony\Component\HttpClient\Response\AsyncResponse;
@@ -27,7 +28,7 @@
2728
*
2829
* @author Jérémy Derussé <[email protected]>
2930
*/
30-
class RetryableHttpClient implements HttpClientInterface, ResetInterface
31+
class RetryableHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
3132
{
3233
use AsyncDecoratorTrait;
3334

@@ -163,6 +164,14 @@ public function request(string $method, string $url, array $options = []): Respo
163164
});
164165
}
165166

167+
public function setLogger(LoggerInterface $logger): void
168+
{
169+
$this->logger = $logger;
170+
if ($this->client instanceof LoggerAwareInterface) {
171+
$this->client->setLogger($logger);
172+
}
173+
}
174+
166175
private function getDelayFromHeader(array $headers): ?int
167176
{
168177
if (null !== $after = $headers['retry-after'][0] ?? null) {

ThrottlingHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Component\RateLimiter\LimiterInterface;
1517
use Symfony\Contracts\HttpClient\HttpClientInterface;
1618
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -19,7 +21,7 @@
1921
/**
2022
* Limits the number of requests within a certain period.
2123
*/
22-
class ThrottlingHttpClient implements HttpClientInterface, ResetInterface
24+
class ThrottlingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
2325
{
2426
use DecoratorTrait {
2527
reset as private traitReset;
@@ -48,4 +50,11 @@ public function reset(): void
4850
$this->traitReset();
4951
$this->rateLimiter->reset();
5052
}
53+
54+
public function setLogger(LoggerInterface $logger): void
55+
{
56+
if ($this->client instanceof LoggerAwareInterface) {
57+
$this->client->setLogger($logger);
58+
}
59+
}
5160
}

UriTemplateHttpClient.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Contracts\HttpClient\HttpClientInterface;
1517
use Symfony\Contracts\HttpClient\ResponseInterface;
1618
use Symfony\Contracts\Service\ResetInterface;
1719

18-
class UriTemplateHttpClient implements HttpClientInterface, ResetInterface
20+
class UriTemplateHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
1921
{
2022
use DecoratorTrait;
2123

@@ -62,6 +64,13 @@ public function withOptions(array $options): static
6264
return $clone;
6365
}
6466

67+
public function setLogger(LoggerInterface $logger): void
68+
{
69+
if ($this->client instanceof LoggerAwareInterface) {
70+
$this->client->setLogger($logger);
71+
}
72+
}
73+
6574
/**
6675
* @return \Closure(string $url, array $vars): string
6776
*/

0 commit comments

Comments
 (0)