Updated to Drupal 8.5. Core Media not yet in use.
[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      * @param callable|null $callback A valid PHP callback or null to set it later
35      * @param int           $status   The response status code
36      * @param array         $headers  An array of response headers
37      */
38     public function __construct(callable $callback = null, $status = 200, $headers = array())
39     {
40         parent::__construct(null, $status, $headers);
41
42         if (null !== $callback) {
43             $this->setCallback($callback);
44         }
45         $this->streamed = false;
46         $this->headersSent = false;
47     }
48
49     /**
50      * Factory method for chainability.
51      *
52      * @param callable|null $callback A valid PHP callback or null to set it later
53      * @param int           $status   The response status code
54      * @param array         $headers  An array of response headers
55      *
56      * @return static
57      */
58     public static function create($callback = null, $status = 200, $headers = array())
59     {
60         return new static($callback, $status, $headers);
61     }
62
63     /**
64      * Sets the PHP callback associated with this Response.
65      *
66      * @param callable $callback A valid PHP callback
67      *
68      * @return $this
69      */
70     public function setCallback(callable $callback)
71     {
72         $this->callback = $callback;
73
74         return $this;
75     }
76
77     /**
78      * {@inheritdoc}
79      *
80      * This method only sends the headers once.
81      *
82      * @return $this
83      */
84     public function sendHeaders()
85     {
86         if ($this->headersSent) {
87             return $this;
88         }
89
90         $this->headersSent = true;
91
92         return parent::sendHeaders();
93     }
94
95     /**
96      * {@inheritdoc}
97      *
98      * This method only sends the content once.
99      *
100      * @return $this
101      */
102     public function sendContent()
103     {
104         if ($this->streamed) {
105             return $this;
106         }
107
108         $this->streamed = true;
109
110         if (null === $this->callback) {
111             throw new \LogicException('The Response callback must not be null.');
112         }
113
114         call_user_func($this->callback);
115
116         return $this;
117     }
118
119     /**
120      * {@inheritdoc}
121      *
122      * @throws \LogicException when the content is not null
123      *
124      * @return $this
125      */
126     public function setContent($content)
127     {
128         if (null !== $content) {
129             throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
130         }
131
132         return $this;
133     }
134
135     /**
136      * {@inheritdoc}
137      *
138      * @return false
139      */
140     public function getContent()
141     {
142         return false;
143     }
144 }