| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Response; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Exception\ClientException; |
| 15 |
use Symfony\Component\HttpClient\Exception\JsonException; |
| 16 |
use Symfony\Component\HttpClient\Exception\RedirectionException; |
| 17 |
use Symfony\Component\HttpClient\Exception\ServerException; |
| 18 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
trait CommonResponseTrait |
| 28 |
{ |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $initializer; |
| 33 |
|
| 34 |
private $shouldBuffer; |
| 35 |
|
| 36 |
private $content; |
| 37 |
private int $offset = 0; |
| 38 |
private ?array $jsonData = null; |
| 39 |
|
| 40 |
public function getContent(bool $throw = true): string |
| 41 |
{ |
| 42 |
if ($this->initializer) { |
| 43 |
self::initialize($this); |
| 44 |
} |
| 45 |
|
| 46 |
if ($throw) { |
| 47 |
$this->checkStatusCode(); |
| 48 |
} |
| 49 |
|
| 50 |
if (null === $this->content) { |
| 51 |
$content = null; |
| 52 |
|
| 53 |
foreach (self::stream([$this]) as $chunk) { |
| 54 |
if (!$chunk->isLast()) { |
| 55 |
$content .= $chunk->getContent(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (null !== $content) { |
| 60 |
return $content; |
| 61 |
} |
| 62 |
|
| 63 |
if (null === $this->content) { |
| 64 |
throw new TransportException('Cannot get the content of the response twice: buffering is disabled.'); |
| 65 |
} |
| 66 |
} else { |
| 67 |
foreach (self::stream([$this]) as $chunk) { |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
rewind($this->content); |
| 73 |
|
| 74 |
return stream_get_contents($this->content); |
| 75 |
} |
| 76 |
|
| 77 |
public function toArray(bool $throw = true): array |
| 78 |
{ |
| 79 |
if ('' === $content = $this->getContent($throw)) { |
| 80 |
throw new JsonException('Response body is empty.'); |
| 81 |
} |
| 82 |
|
| 83 |
if (null !== $this->jsonData) { |
| 84 |
return $this->jsonData; |
| 85 |
} |
| 86 |
|
| 87 |
try { |
| 88 |
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); |
| 89 |
} catch (\JsonException $e) { |
| 90 |
throw new JsonException($e->getMessage().\sprintf(' for "%s".', $this->getInfo('url')), $e->getCode()); |
| 91 |
} |
| 92 |
|
| 93 |
if (!\is_array($content)) { |
| 94 |
throw new JsonException(\sprintf('JSON content was expected to decode to an array, "%s" returned for "%s".', get_debug_type($content), $this->getInfo('url'))); |
| 95 |
} |
| 96 |
|
| 97 |
if (null !== $this->content) { |
| 98 |
|
| 99 |
return $this->jsonData = $content; |
| 100 |
} |
| 101 |
|
| 102 |
return $content; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function toStream(bool $throw = true) |
| 109 |
{ |
| 110 |
if ($throw) { |
| 111 |
|
| 112 |
$this->getHeaders($throw); |
| 113 |
} |
| 114 |
|
| 115 |
$stream = StreamWrapper::createResource($this); |
| 116 |
stream_get_meta_data($stream)['wrapper_data'] |
| 117 |
->bindHandles($this->handle, $this->content); |
| 118 |
|
| 119 |
return $stream; |
| 120 |
} |
| 121 |
|
| 122 |
public function __serialize(): array |
| 123 |
{ |
| 124 |
throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
| 125 |
} |
| 126 |
|
| 127 |
public function __unserialize(array $data): void |
| 128 |
{ |
| 129 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
abstract protected function close(): void; |
| 136 |
|
| 137 |
private static function initialize(self $response): void |
| 138 |
{ |
| 139 |
if (null !== $response->getInfo('error')) { |
| 140 |
throw new TransportException($response->getInfo('error')); |
| 141 |
} |
| 142 |
|
| 143 |
try { |
| 144 |
if (($response->initializer)($response, -0.0)) { |
| 145 |
foreach (self::stream([$response], -0.0) as $chunk) { |
| 146 |
if ($chunk->isFirst()) { |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} catch (\Throwable $e) { |
| 152 |
|
| 153 |
$response->info['error'] = $e->getMessage(); |
| 154 |
$response->close(); |
| 155 |
throw $e; |
| 156 |
} |
| 157 |
|
| 158 |
$response->initializer = null; |
| 159 |
} |
| 160 |
|
| 161 |
private function checkStatusCode(): void |
| 162 |
{ |
| 163 |
$code = $this->getInfo('http_code'); |
| 164 |
|
| 165 |
if (500 <= $code) { |
| 166 |
throw new ServerException($this); |
| 167 |
} |
| 168 |
|
| 169 |
if (400 <= $code) { |
| 170 |
throw new ClientException($this); |
| 171 |
} |
| 172 |
|
| 173 |
if (300 <= $code) { |
| 174 |
throw new RedirectionException($this); |
| 175 |
} |
| 176 |
} |
| 177 |
} |