|
| 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 | + * @api |
| 28 | + */ |
| 29 | +class StreamedResponse extends Response |
| 30 | +{ |
| 31 | + protected $callback; |
| 32 | + protected $streamed; |
| 33 | + |
| 34 | + /** |
| 35 | + * Constructor. |
| 36 | + * |
| 37 | + * @param mixed $callback A valid PHP callback |
| 38 | + * @param integer $status The response status code |
| 39 | + * @param array $headers An array of response headers |
| 40 | + * |
| 41 | + * @api |
| 42 | + */ |
| 43 | + public function __construct($callback = null, $status = 200, $headers = array()) |
| 44 | + { |
| 45 | + parent::__construct(null, $status, $headers); |
| 46 | + |
| 47 | + if (null !== $callback) { |
| 48 | + $this->setCallback($callback); |
| 49 | + } |
| 50 | + $this->streamed = false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets the PHP callback associated with this Response. |
| 55 | + * |
| 56 | + * @param mixed $callback A valid PHP callback |
| 57 | + */ |
| 58 | + public function setCallback($callback) |
| 59 | + { |
| 60 | + $this->callback = $callback; |
| 61 | + if (!is_callable($this->callback)) { |
| 62 | + throw new \LogicException('The Response callback must be a valid PHP callable.'); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @{inheritdoc} |
| 68 | + */ |
| 69 | + public function prepare(Request $request) |
| 70 | + { |
| 71 | + if ('1.0' != $request->server->get('SERVER_PROTOCOL')) { |
| 72 | + $this->setProtocolVersion('1.1'); |
| 73 | + $this->headers->set('Transfer-Encoding', 'chunked'); |
| 74 | + } |
| 75 | + |
| 76 | + $this->headers->set('Cache-Control', 'no-cache'); |
| 77 | + |
| 78 | + parent::prepare($request); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @{inheritdoc} |
| 83 | + * |
| 84 | + * This method only sends the content once. |
| 85 | + */ |
| 86 | + public function sendContent() |
| 87 | + { |
| 88 | + if ($this->streamed) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $this->streamed = true; |
| 93 | + |
| 94 | + if (null === $this->callback) { |
| 95 | + throw new \LogicException('The Response callback must not be null.'); |
| 96 | + } |
| 97 | + |
| 98 | + call_user_func($this->callback); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @{inheritdoc} |
| 103 | + * |
| 104 | + * @throws \LogicException when the content is not null |
| 105 | + */ |
| 106 | + public function setContent($content) |
| 107 | + { |
| 108 | + if (null !== $content) { |
| 109 | + throw new \LogicException('The content cannot be set on a StreamedResponse instance.'); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @{inheritdoc} |
| 115 | + * |
| 116 | + * @return false |
| 117 | + */ |
| 118 | + public function getContent() |
| 119 | + { |
| 120 | + return false; |
| 121 | + } |
| 122 | +} |
0 commit comments