Version 1
[yaffs-website] / vendor / symfony / http-foundation / StreamedResponse.php
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($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      * @throws \LogicException
71      */
72     public function setCallback($callback)
73     {
74         if (!is_callable($callback)) {
75             throw new \LogicException('The Response callback must be a valid PHP callable.');
76         }
77         $this->callback = $callback;
78     }
79
80     /**
81      * {@inheritdoc}
82      *
83      * This method only sends the headers once.
84      */
85     public function sendHeaders()
86     {
87         if ($this->headersSent) {
88             return;
89         }
90
91         $this->headersSent = true;
92
93         parent::sendHeaders();
94     }
95
96     /**
97      * {@inheritdoc}
98      *
99      * This method only sends the content once.
100      */
101     public function sendContent()
102     {
103         if ($this->streamed) {
104             return;
105         }
106
107         $this->streamed = true;
108
109         if (null === $this->callback) {
110             throw new \LogicException('The Response callback must not be null.');
111         }
112
113         call_user_func($this->callback);
114     }
115
116     /**
117      * {@inheritdoc}
118      *
119      * @throws \LogicException when the content is not null
120      */
121     public function setContent($content)
122     {
123         if (null !== $content) {
124             throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
125         }
126     }
127
128     /**
129      * {@inheritdoc}
130      *
131      * @return false
132      */
133     public function getContent()
134     {
135         return false;
136     }
137 }