annotate vendor/symfony/http-foundation/StreamedResponse.php @ 19:fa3358dc1485 tip

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