Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation; Chris@0: Chris@0: /** Chris@0: * StreamedResponse represents a streamed HTTP response. Chris@0: * Chris@0: * A StreamedResponse uses a callback for its content. Chris@0: * Chris@0: * The callback should use the standard PHP functions like echo Chris@17: * to stream the response back to the client. The flush() function Chris@0: * can also be used if needed. Chris@0: * Chris@0: * @see flush() Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class StreamedResponse extends Response Chris@0: { Chris@0: protected $callback; Chris@0: protected $streamed; Chris@0: private $headersSent; Chris@0: Chris@0: /** Chris@0: * @param callable|null $callback A valid PHP callback or null to set it later Chris@0: * @param int $status The response status code Chris@0: * @param array $headers An array of response headers Chris@0: */ Chris@17: public function __construct(callable $callback = null, $status = 200, $headers = []) Chris@0: { Chris@0: parent::__construct(null, $status, $headers); Chris@0: Chris@0: if (null !== $callback) { Chris@0: $this->setCallback($callback); Chris@0: } Chris@0: $this->streamed = false; Chris@0: $this->headersSent = false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Factory method for chainability. Chris@0: * Chris@0: * @param callable|null $callback A valid PHP callback or null to set it later Chris@0: * @param int $status The response status code Chris@0: * @param array $headers An array of response headers Chris@0: * Chris@0: * @return static Chris@0: */ Chris@17: public static function create($callback = null, $status = 200, $headers = []) Chris@0: { Chris@0: return new static($callback, $status, $headers); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the PHP callback associated with this Response. Chris@0: * Chris@0: * @param callable $callback A valid PHP callback Chris@14: * Chris@14: * @return $this Chris@0: */ Chris@0: public function setCallback(callable $callback) Chris@0: { Chris@0: $this->callback = $callback; Chris@14: Chris@14: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * This method only sends the headers once. Chris@14: * Chris@14: * @return $this Chris@0: */ Chris@0: public function sendHeaders() Chris@0: { Chris@0: if ($this->headersSent) { Chris@14: return $this; Chris@0: } Chris@0: Chris@0: $this->headersSent = true; Chris@0: Chris@14: return parent::sendHeaders(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * This method only sends the content once. Chris@14: * Chris@14: * @return $this Chris@0: */ Chris@0: public function sendContent() Chris@0: { Chris@0: if ($this->streamed) { Chris@14: return $this; Chris@0: } Chris@0: Chris@0: $this->streamed = true; Chris@0: Chris@0: if (null === $this->callback) { Chris@0: throw new \LogicException('The Response callback must not be null.'); Chris@0: } Chris@0: Chris@17: \call_user_func($this->callback); Chris@14: Chris@14: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @throws \LogicException when the content is not null Chris@14: * Chris@14: * @return $this Chris@0: */ Chris@0: public function setContent($content) Chris@0: { Chris@0: if (null !== $content) { Chris@0: throw new \LogicException('The content cannot be set on a StreamedResponse instance.'); Chris@0: } Chris@14: Chris@17: $this->streamed = true; Chris@17: Chris@14: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @return false Chris@0: */ Chris@0: public function getContent() Chris@0: { Chris@0: return false; Chris@0: } Chris@0: }