1 |
cb15593b
|
Cajova-Houba
|
<?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 |
|
|
|
32 |
|
|
/**
|
33 |
|
|
* Constructor.
|
34 |
|
|
*
|
35 |
|
|
* @param callable|null $callback A valid PHP callback or null to set it later
|
36 |
|
|
* @param int $status The response status code
|
37 |
|
|
* @param array $headers An array of response headers
|
38 |
|
|
*/
|
39 |
|
|
public function __construct(callable $callback = null, $status = 200, $headers = array())
|
40 |
|
|
{
|
41 |
|
|
parent::__construct(null, $status, $headers);
|
42 |
|
|
|
43 |
|
|
if (null !== $callback) {
|
44 |
|
|
$this->setCallback($callback);
|
45 |
|
|
}
|
46 |
|
|
$this->streamed = 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 StreamedResponse
|
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 |
|
|
public function setCallback(callable $callback)
|
69 |
|
|
{
|
70 |
|
|
$this->callback = $callback;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* {@inheritdoc}
|
75 |
|
|
*
|
76 |
|
|
* This method only sends the content once.
|
77 |
|
|
*/
|
78 |
|
|
public function sendContent()
|
79 |
|
|
{
|
80 |
|
|
if ($this->streamed) {
|
81 |
|
|
return;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
$this->streamed = true;
|
85 |
|
|
|
86 |
|
|
if (null === $this->callback) {
|
87 |
|
|
throw new \LogicException('The Response callback must not be null.');
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
call_user_func($this->callback);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* {@inheritdoc}
|
95 |
|
|
*
|
96 |
|
|
* @throws \LogicException when the content is not null
|
97 |
|
|
*/
|
98 |
|
|
public function setContent($content)
|
99 |
|
|
{
|
100 |
|
|
if (null !== $content) {
|
101 |
|
|
throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
/**
|
106 |
|
|
* {@inheritdoc}
|
107 |
|
|
*
|
108 |
|
|
* @return false
|
109 |
|
|
*/
|
110 |
|
|
public function getContent()
|
111 |
|
|
{
|
112 |
|
|
return false;
|
113 |
|
|
}
|
114 |
|
|
}
|