[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1