comparison vendor/symfony/http-foundation/StreamedResponse.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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() method
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 /**
34 * Constructor.
35 *
36 * @param callable|null $callback A valid PHP callback or null to set it later
37 * @param int $status The response status code
38 * @param array $headers An array of response headers
39 */
40 public function __construct(callable $callback = null, $status = 200, $headers = array())
41 {
42 parent::__construct(null, $status, $headers);
43
44 if (null !== $callback) {
45 $this->setCallback($callback);
46 }
47 $this->streamed = false;
48 $this->headersSent = false;
49 }
50
51 /**
52 * Factory method for chainability.
53 *
54 * @param callable|null $callback A valid PHP callback or null to set it later
55 * @param int $status The response status code
56 * @param array $headers An array of response headers
57 *
58 * @return static
59 */
60 public static function create($callback = null, $status = 200, $headers = array())
61 {
62 return new static($callback, $status, $headers);
63 }
64
65 /**
66 * Sets the PHP callback associated with this Response.
67 *
68 * @param callable $callback A valid PHP callback
69 */
70 public function setCallback(callable $callback)
71 {
72 $this->callback = $callback;
73 }
74
75 /**
76 * {@inheritdoc}
77 *
78 * This method only sends the headers once.
79 */
80 public function sendHeaders()
81 {
82 if ($this->headersSent) {
83 return;
84 }
85
86 $this->headersSent = true;
87
88 parent::sendHeaders();
89 }
90
91 /**
92 * {@inheritdoc}
93 *
94 * This method only sends the content once.
95 */
96 public function sendContent()
97 {
98 if ($this->streamed) {
99 return;
100 }
101
102 $this->streamed = true;
103
104 if (null === $this->callback) {
105 throw new \LogicException('The Response callback must not be null.');
106 }
107
108 call_user_func($this->callback);
109 }
110
111 /**
112 * {@inheritdoc}
113 *
114 * @throws \LogicException when the content is not null
115 */
116 public function setContent($content)
117 {
118 if (null !== $content) {
119 throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
120 }
121 }
122
123 /**
124 * {@inheritdoc}
125 *
126 * @return false
127 */
128 public function getContent()
129 {
130 return false;
131 }
132 }