Debian Codegraph @

lib/3rdParty/composer/symfony/http-client/TraceableHttpClient.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\HttpClient;
13
14 use Psr\Log\LoggerAwareInterface;
15 use Psr\Log\LoggerInterface;
16 use Symfony\Component\HttpClient\Response\ResponseStream;
17 use Symfony\Component\HttpClient\Response\TraceableResponse;
18 use Symfony\Component\Stopwatch\Stopwatch;
19 use Symfony\Contracts\HttpClient\HttpClientInterface;
20 use Symfony\Contracts\HttpClient\ResponseInterface;
21 use Symfony\Contracts\HttpClient\ResponseStreamInterface;
22 use Symfony\Contracts\Service\ResetInterface;
23
24 /**
25 * @author Jérémy Romey <[email protected]r>
26 */
27 final class TraceableHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface
28 {
29 private \ArrayObject $tracedRequests;
30
31 public function __construct(
32 private HttpClientInterface $client,
33 private ?Stopwatch $stopwatch = null,
34 private ?\Closure $disabled = null,
35 ) {
36 $this->tracedRequests = new \ArrayObject();
37 }
38
39 public function request(string $method, string $url, array $options = []): ResponseInterface
40 {
41 if ($this->disabled?->__invoke()) {
42 return new TraceableResponse($this->client, $this->client->request($method, $url, $options));
43 }
44
45 $content = null;
46 $traceInfo = [];
47 $tracedRequest = [
48 'method' => $method,
49 'url' => $url,
50 'options' => $options,
51 'info' => &$traceInfo,
52 'content' => &$content,
53 ];
54 $onProgress = $options['on_progress'] ?? null;
55
56 if (false === ($options['extra']['trace_content'] ?? true)) {
57 unset($content);
58 $content = false;
59 unset($tracedRequest['options']['body'], $tracedRequest['options']['json']);
60 }
61 $this->tracedRequests[] = $tracedRequest;
62
63 $options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$traceInfo, $onProgress) {
64 $traceInfo = $info;
65
66 if (null !== $onProgress) {
67 $onProgress($dlNow, $dlSize, $info);
68 }
69 };
70
71 return new TraceableResponse($this->client, $this->client->request($method, $url, $options), $content, $this->stopwatch?->start("$method $url", 'http_client'));
72 }
73
74 public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface
75 {
76 if ($responses instanceof TraceableResponse) {
77 $responses = [$responses];
78 }
79
80 return new ResponseStream(TraceableResponse::stream($this->client, $responses, $timeout));
81 }
82
83 public function getTracedRequests(): array
84 {
85 return $this->tracedRequests->getArrayCopy();
86 }
87
88 public function reset(): void
89 {
90 if ($this->client instanceof ResetInterface) {
91 $this->client->reset();
92 }
93
94 $this->tracedRequests->exchangeArray([]);
95 }
96
97 /**
98 * @deprecated since Symfony 7.1, configure the logger on the wrapped HTTP client directly instead
99 */
100 public function setLogger(LoggerInterface $logger): void
101 {
102 trigger_deprecation('symfony/http-client', '7.1', 'Configure the logger on the wrapped HTTP client directly instead.');
103
104 if ($this->client instanceof LoggerAwareInterface) {
105 $this->client->setLogger($logger);
106 }
107 }
108
109 public function withOptions(array $options): static
110 {
111 $clone = clone $this;
112 $clone->client = $this->client->withOptions($options);
113
114 return $clone;
115 }
116 }