annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpFoundation;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * StreamedResponse represents a streamed HTTP response.
Chris@0 16 *
Chris@0 17 * A StreamedResponse uses a callback for its content.
Chris@0 18 *
Chris@0 19 * The callback should use the standard PHP functions like echo
Chris@0 20 * to stream the response back to the client. The flush() method
Chris@0 21 * can also be used if needed.
Chris@0 22 *
Chris@0 23 * @see flush()
Chris@0 24 *
Chris@0 25 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 26 */
Chris@0 27 class StreamedResponse extends Response
Chris@0 28 {
Chris@0 29 protected $callback;
Chris@0 30 protected $streamed;
Chris@0 31 private $headersSent;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructor.
Chris@0 35 *
Chris@0 36 * @param callable|null $callback A valid PHP callback or null to set it later
Chris@0 37 * @param int $status The response status code
Chris@0 38 * @param array $headers An array of response headers
Chris@0 39 */
Chris@0 40 public function __construct(callable $callback = null, $status = 200, $headers = array())
Chris@0 41 {
Chris@0 42 parent::__construct(null, $status, $headers);
Chris@0 43
Chris@0 44 if (null !== $callback) {
Chris@0 45 $this->setCallback($callback);
Chris@0 46 }
Chris@0 47 $this->streamed = false;
Chris@0 48 $this->headersSent = false;
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Factory method for chainability.
Chris@0 53 *
Chris@0 54 * @param callable|null $callback A valid PHP callback or null to set it later
Chris@0 55 * @param int $status The response status code
Chris@0 56 * @param array $headers An array of response headers
Chris@0 57 *
Chris@0 58 * @return static
Chris@0 59 */
Chris@0 60 public static function create($callback = null, $status = 200, $headers = array())
Chris@0 61 {
Chris@0 62 return new static($callback, $status, $headers);
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * Sets the PHP callback associated with this Response.
Chris@0 67 *
Chris@0 68 * @param callable $callback A valid PHP callback
Chris@0 69 */
Chris@0 70 public function setCallback(callable $callback)
Chris@0 71 {
Chris@0 72 $this->callback = $callback;
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 *
Chris@0 78 * This method only sends the headers once.
Chris@0 79 */
Chris@0 80 public function sendHeaders()
Chris@0 81 {
Chris@0 82 if ($this->headersSent) {
Chris@0 83 return;
Chris@0 84 }
Chris@0 85
Chris@0 86 $this->headersSent = true;
Chris@0 87
Chris@0 88 parent::sendHeaders();
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 *
Chris@0 94 * This method only sends the content once.
Chris@0 95 */
Chris@0 96 public function sendContent()
Chris@0 97 {
Chris@0 98 if ($this->streamed) {
Chris@0 99 return;
Chris@0 100 }
Chris@0 101
Chris@0 102 $this->streamed = true;
Chris@0 103
Chris@0 104 if (null === $this->callback) {
Chris@0 105 throw new \LogicException('The Response callback must not be null.');
Chris@0 106 }
Chris@0 107
Chris@0 108 call_user_func($this->callback);
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * {@inheritdoc}
Chris@0 113 *
Chris@0 114 * @throws \LogicException when the content is not null
Chris@0 115 */
Chris@0 116 public function setContent($content)
Chris@0 117 {
Chris@0 118 if (null !== $content) {
Chris@0 119 throw new \LogicException('The content cannot be set on a StreamedResponse instance.');
Chris@0 120 }
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 *
Chris@0 126 * @return false
Chris@0 127 */
Chris@0 128 public function getContent()
Chris@0 129 {
Chris@0 130 return false;
Chris@0 131 }
Chris@0 132 }