| 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 |
class StreamedResponse extends Response |
| 28 |
{ |
| 29 |
protected ?\Closure $callback = null; |
| 30 |
protected bool $streamed = false; |
| 31 |
|
| 32 |
private bool $headersSent = false; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function __construct(callable|iterable|null $callbackOrChunks = null, int $status = 200, array $headers = []) |
| 39 |
{ |
| 40 |
parent::__construct(null, $status, $headers); |
| 41 |
|
| 42 |
if (\is_callable($callbackOrChunks)) { |
| 43 |
$this->setCallback($callbackOrChunks); |
| 44 |
} elseif ($callbackOrChunks) { |
| 45 |
$this->setChunks($callbackOrChunks); |
| 46 |
} |
| 47 |
$this->streamed = false; |
| 48 |
$this->headersSent = false; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function setChunks(iterable $chunks): static |
| 55 |
{ |
| 56 |
$this->callback = static function () use ($chunks): void { |
| 57 |
foreach ($chunks as $chunk) { |
| 58 |
echo $chunk; |
| 59 |
@ob_flush(); |
| 60 |
flush(); |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public function setCallback(callable $callback): static |
| 73 |
{ |
| 74 |
$this->callback = $callback(...); |
| 75 |
|
| 76 |
return $this; |
| 77 |
} |
| 78 |
|
| 79 |
public function getCallback(): ?\Closure |
| 80 |
{ |
| 81 |
if (!isset($this->callback)) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
return ($this->callback)(...); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function sendHeaders(?int $statusCode = null): static |
| 96 |
{ |
| 97 |
if ($this->headersSent) { |
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
if ($statusCode < 100 || $statusCode >= 200) { |
| 102 |
$this->headersSent = true; |
| 103 |
} |
| 104 |
|
| 105 |
return parent::sendHeaders($statusCode); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function sendContent(): static |
| 114 |
{ |
| 115 |
if ($this->streamed) { |
| 116 |
return $this; |
| 117 |
} |
| 118 |
|
| 119 |
$this->streamed = true; |
| 120 |
|
| 121 |
if (!isset($this->callback)) { |
| 122 |
throw new \LogicException('The Response callback must be set.'); |
| 123 |
} |
| 124 |
|
| 125 |
($this->callback)(); |
| 126 |
|
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function setContent(?string $content): static |
| 136 |
{ |
| 137 |
if (null !== $content) { |
| 138 |
throw new \LogicException('The content cannot be set on a StreamedResponse instance.'); |
| 139 |
} |
| 140 |
|
| 141 |
$this->streamed = true; |
| 142 |
|
| 143 |
return $this; |
| 144 |
} |
| 145 |
|
| 146 |
public function getContent(): string|false |
| 147 |
{ |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |