[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> StreamedResponse.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1