| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class StreamedJsonResponse extends StreamedResponse |
| 46 |
{ |
| 47 |
private const PLACEHOLDER = '__symfony_json__'; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
private readonly iterable $data, |
| 57 |
int $status = 200, |
| 58 |
array $headers = [], |
| 59 |
private int $encodingOptions = JsonResponse::DEFAULT_ENCODING_OPTIONS, |
| 60 |
) { |
| 61 |
parent::__construct($this->stream(...), $status, $headers); |
| 62 |
|
| 63 |
if (!$this->headers->get('Content-Type')) { |
| 64 |
$this->headers->set('Content-Type', 'application/json'); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private function stream(): void |
| 69 |
{ |
| 70 |
$jsonEncodingOptions = \JSON_THROW_ON_ERROR | $this->encodingOptions; |
| 71 |
$keyEncodingOptions = $jsonEncodingOptions & ~\JSON_NUMERIC_CHECK; |
| 72 |
|
| 73 |
$this->streamData($this->data, $jsonEncodingOptions, $keyEncodingOptions); |
| 74 |
} |
| 75 |
|
| 76 |
private function streamData(mixed $data, int $jsonEncodingOptions, int $keyEncodingOptions): void |
| 77 |
{ |
| 78 |
if (\is_array($data)) { |
| 79 |
$this->streamArray($data, $jsonEncodingOptions, $keyEncodingOptions); |
| 80 |
|
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (is_iterable($data) && !$data instanceof \JsonSerializable) { |
| 85 |
$this->streamIterable($data, $jsonEncodingOptions, $keyEncodingOptions); |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
echo json_encode($data, $jsonEncodingOptions); |
| 91 |
} |
| 92 |
|
| 93 |
private function streamArray(array $data, int $jsonEncodingOptions, int $keyEncodingOptions): void |
| 94 |
{ |
| 95 |
$generators = []; |
| 96 |
|
| 97 |
array_walk_recursive($data, static function (&$item, $key) use (&$generators) { |
| 98 |
if (self::PLACEHOLDER === $key) { |
| 99 |
|
| 100 |
|
| 101 |
$generators[] = $key; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if (\is_object($item)) { |
| 106 |
$generators[] = $item; |
| 107 |
$item = self::PLACEHOLDER; |
| 108 |
} elseif (self::PLACEHOLDER === $item) { |
| 109 |
|
| 110 |
|
| 111 |
$generators[] = $item; |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
$jsonParts = explode('"'.self::PLACEHOLDER.'"', json_encode($data, $jsonEncodingOptions)); |
| 116 |
|
| 117 |
foreach ($generators as $index => $generator) { |
| 118 |
|
| 119 |
echo $jsonParts[$index]; |
| 120 |
|
| 121 |
$this->streamData($generator, $jsonEncodingOptions, $keyEncodingOptions); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
echo $jsonParts[array_key_last($jsonParts)]; |
| 126 |
} |
| 127 |
|
| 128 |
private function streamIterable(iterable $iterable, int $jsonEncodingOptions, int $keyEncodingOptions): void |
| 129 |
{ |
| 130 |
$isFirstItem = true; |
| 131 |
$startTag = '['; |
| 132 |
|
| 133 |
foreach ($iterable as $key => $item) { |
| 134 |
if ($isFirstItem) { |
| 135 |
$isFirstItem = false; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if (0 !== $key) { |
| 140 |
$startTag = '{'; |
| 141 |
} |
| 142 |
|
| 143 |
echo $startTag; |
| 144 |
} else { |
| 145 |
|
| 146 |
echo ','; |
| 147 |
} |
| 148 |
|
| 149 |
if ('{' === $startTag) { |
| 150 |
echo json_encode((string) $key, $keyEncodingOptions).':'; |
| 151 |
} |
| 152 |
|
| 153 |
$this->streamData($item, $jsonEncodingOptions, $keyEncodingOptions); |
| 154 |
} |
| 155 |
|
| 156 |
if ($isFirstItem) { |
| 157 |
echo '['; |
| 158 |
} |
| 159 |
|
| 160 |
echo '[' === $startTag ? ']' : '}'; |
| 161 |
} |
| 162 |
} |