Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/StreamedResponse.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\HttpFoundation;
13
14 /**
15 * StreamedResponse represents a streamed HTTP response.
16 *
17 * A StreamedResponse uses a callback or an iterable of strings for its content.
18 *
19 * The callback should use the standard PHP functions like echo
20 * to stream the response back to the client. The flush() function
21 * can also be used if needed.
22 *
23 * @see flush()
24 *
25 * @author Fabien Potencier <[email protected]>
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 * @param callable|iterable<string>|null $callbackOrChunks
36 * @param int $status The HTTP status code (200 "OK" by default)
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 * @param iterable<string> $chunks
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 * Sets the PHP callback associated with this Response.
69 *
70 * @return $this
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 * This method only sends the headers once.
90 *
91 * @param positive-int|null $statusCode The status code to use, override the statusCode property if set and not null
92 *
93 * @return $this
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 * This method only sends the content once.
110 *
111 * @return $this
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 * @return $this
132 *
133 * @throws \LogicException when the content is not null
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 }