PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / vendor / symfony / http-foundation / StreamedResponse.php

StreamedResponse.php in WPIDE – File Manager & Code Editor 3.5.9, at vendor/symfony/http-foundation/StreamedResponse.php

140 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 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 <fabien@symfony.com>
26 */
27 class StreamedResponse extends Response
28 {
29 protected $callback;
30 protected $streamed;
31 private $headersSent;
32
33 public function __construct(?callable $callback = null, int $status = 200, array $headers = [])
34 {
35 parent::__construct(null, $status, $headers);
36
37 if (null !== $callback) {
38 $this->setCallback($callback);
39 }
40 $this->streamed = false;
41 $this->headersSent = false;
42 }
43
44 /**
45 * Factory method for chainability.
46 *
47 * @param callable|null $callback A valid PHP callback or null to set it later
48 *
49 * @return static
50 *
51 * @deprecated since Symfony 5.1, use __construct() instead.
52 */
53 public static function create($callback = null, int $status = 200, array $headers = [])
54 {
55 trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
56
57 return new static($callback, $status, $headers);
58 }
59
60 /**
61 * Sets the PHP callback associated with this Response.
62 *
63 * @return $this
64 */
65 public function setCallback(callable $callback)
66 {
67 $this->callback = $callback;
68
69 return $this;
70 }
71
72 /**
73 * {@inheritdoc}
74 *
75 * This method only sends the headers once.
76 *
77 * @return $this
78 */
79 public function sendHeaders()
80 {
81 if ($this->headersSent) {
82 return $this;
83 }
84
85 $this->headersSent = true;
86
87 return parent::sendHeaders();
88 }
89
90 /**
91 * {@inheritdoc}
92 *
93 * This method only sends the content once.
94 *
95 * @return $this
96 */
97 public function sendContent()
98 {
99 if ($this->streamed) {
100 return $this;
101 }
102
103 $this->streamed = true;
104
105 if (null === $this->callback) {
106 throw new \LogicException('The Response callback must not be null.');
107 }
108
109 ($this->callback)();
110
111 return $this;
112 }
113
114 /**
115 * {@inheritdoc}
116 *
117 * @return $this
118 *
119 * @throws \LogicException when the content is not null
120 */
121 public function setContent(?string $content)
122 {
123 if (null !== $content) {
124 throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
125 }
126
127 $this->streamed = true;
128
129 return $this;
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function getContent()
136 {
137 return false;
138 }
139 }
140