| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Response; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Chunk\ErrorChunk; |
| 15 |
use Symfony\Component\HttpClient\Exception\ClientException; |
| 16 |
use Symfony\Component\HttpClient\Exception\RedirectionException; |
| 17 |
use Symfony\Component\HttpClient\Exception\ServerException; |
| 18 |
use Symfony\Component\HttpClient\TraceableHttpClient; |
| 19 |
use Symfony\Component\Stopwatch\StopwatchEvent; |
| 20 |
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
| 21 |
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 22 |
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 23 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 24 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 25 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class TraceableResponse implements ResponseInterface, StreamableInterface |
| 33 |
{ |
| 34 |
public function __construct( |
| 35 |
private HttpClientInterface $client, |
| 36 |
private ResponseInterface $response, |
| 37 |
private mixed &$content = false, |
| 38 |
private ?StopwatchEvent $event = null, |
| 39 |
) { |
| 40 |
} |
| 41 |
|
| 42 |
public function __serialize(): array |
| 43 |
{ |
| 44 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
| 45 |
} |
| 46 |
|
| 47 |
public function __unserialize(array $data): void |
| 48 |
{ |
| 49 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
| 50 |
} |
| 51 |
|
| 52 |
public function __destruct() |
| 53 |
{ |
| 54 |
try { |
| 55 |
if (method_exists($this->response, '__destruct')) { |
| 56 |
$this->response->__destruct(); |
| 57 |
} |
| 58 |
} finally { |
| 59 |
if ($this->event?->isStarted()) { |
| 60 |
$this->event->stop(); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function getStatusCode(): int |
| 66 |
{ |
| 67 |
try { |
| 68 |
return $this->response->getStatusCode(); |
| 69 |
} finally { |
| 70 |
if ($this->event?->isStarted()) { |
| 71 |
$this->event->lap(); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public function getHeaders(bool $throw = true): array |
| 77 |
{ |
| 78 |
try { |
| 79 |
return $this->response->getHeaders($throw); |
| 80 |
} finally { |
| 81 |
if ($this->event?->isStarted()) { |
| 82 |
$this->event->lap(); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function getContent(bool $throw = true): string |
| 88 |
{ |
| 89 |
try { |
| 90 |
if (false === $this->content) { |
| 91 |
return $this->response->getContent($throw); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->content = $this->response->getContent(false); |
| 95 |
} finally { |
| 96 |
if ($this->event?->isStarted()) { |
| 97 |
$this->event->stop(); |
| 98 |
} |
| 99 |
if ($throw) { |
| 100 |
$this->checkStatusCode($this->response->getStatusCode()); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function toArray(bool $throw = true): array |
| 106 |
{ |
| 107 |
try { |
| 108 |
if (false === $this->content) { |
| 109 |
return $this->response->toArray($throw); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->content = $this->response->toArray(false); |
| 113 |
} finally { |
| 114 |
if ($this->event?->isStarted()) { |
| 115 |
$this->event->stop(); |
| 116 |
} |
| 117 |
if ($throw) { |
| 118 |
$this->checkStatusCode($this->response->getStatusCode()); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
public function cancel(): void |
| 124 |
{ |
| 125 |
$this->response->cancel(); |
| 126 |
|
| 127 |
if ($this->event?->isStarted()) { |
| 128 |
$this->event->stop(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function getInfo(?string $type = null): mixed |
| 133 |
{ |
| 134 |
return $this->response->getInfo($type); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function toStream(bool $throw = true) |
| 148 |
{ |
| 149 |
if ($throw) { |
| 150 |
|
| 151 |
$this->response->getHeaders(true); |
| 152 |
} |
| 153 |
|
| 154 |
if ($this->response instanceof StreamableInterface) { |
| 155 |
return $this->response->toStream(false); |
| 156 |
} |
| 157 |
|
| 158 |
return StreamWrapper::createResource($this->response, $this->client); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public static function stream(HttpClientInterface $client, iterable $responses, ?float $timeout): \Generator |
| 165 |
{ |
| 166 |
$wrappedResponses = []; |
| 167 |
$traceableMap = new \SplObjectStorage(); |
| 168 |
|
| 169 |
foreach ($responses as $r) { |
| 170 |
if (!$r instanceof self) { |
| 171 |
throw new \TypeError(\sprintf('"%s::stream()" expects parameter 1 to be an iterable of TraceableResponse objects, "%s" given.', TraceableHttpClient::class, get_debug_type($r))); |
| 172 |
} |
| 173 |
|
| 174 |
$traceableMap[$r->response] = $r; |
| 175 |
$wrappedResponses[] = $r->response; |
| 176 |
if ($r->event && !$r->event->isStarted()) { |
| 177 |
$r->event->start(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
foreach ($client->stream($wrappedResponses, $timeout) as $r => $chunk) { |
| 182 |
if ($traceableMap[$r]->event && $traceableMap[$r]->event->isStarted()) { |
| 183 |
try { |
| 184 |
if ($chunk->isTimeout() || !$chunk->isLast()) { |
| 185 |
$traceableMap[$r]->event->lap(); |
| 186 |
} else { |
| 187 |
$traceableMap[$r]->event->stop(); |
| 188 |
} |
| 189 |
} catch (TransportExceptionInterface $e) { |
| 190 |
$traceableMap[$r]->event->stop(); |
| 191 |
if ($chunk instanceof ErrorChunk) { |
| 192 |
$chunk->didThrow(false); |
| 193 |
} else { |
| 194 |
$chunk = new ErrorChunk($chunk->getOffset(), $e); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
yield $traceableMap[$r] => $chunk; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private function checkStatusCode(int $code): void |
| 203 |
{ |
| 204 |
if (500 <= $code) { |
| 205 |
throw new ServerException($this); |
| 206 |
} |
| 207 |
|
| 208 |
if (400 <= $code) { |
| 209 |
throw new ClientException($this); |
| 210 |
} |
| 211 |
|
| 212 |
if (300 <= $code) { |
| 213 |
throw new RedirectionException($this); |
| 214 |
} |
| 215 |
} |
| 216 |
} |