[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/ -> CallbackStream.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros;
  12  
  13  use Psr\Http\Message\StreamInterface;
  14  
  15  use function array_key_exists;
  16  
  17  use const SEEK_SET;
  18  
  19  /**
  20   * Implementation of PSR HTTP streams
  21   */
  22  class CallbackStream implements StreamInterface
  23  {
  24      /**
  25       * @var callable|null
  26       */
  27      protected $callback;
  28  
  29      /**
  30       * @param callable $callback
  31       * @throws Exception\InvalidArgumentException
  32       */
  33      public function __construct(callable $callback)
  34      {
  35          $this->attach($callback);
  36      }
  37  
  38      /**
  39       * {@inheritdoc}
  40       */
  41      public function __toString() : string
  42      {
  43          return $this->getContents();
  44      }
  45  
  46      /**
  47       * {@inheritdoc}
  48       */
  49      public function close() : void
  50      {
  51          $this->callback = null;
  52      }
  53  
  54      /**
  55       * {@inheritdoc}
  56       */
  57      public function detach() : ?callable
  58      {
  59          $callback = $this->callback;
  60          $this->callback = null;
  61          return $callback;
  62      }
  63  
  64      /**
  65       * Attach a new callback to the instance.
  66       */
  67      public function attach(callable $callback) : void
  68      {
  69          $this->callback = $callback;
  70      }
  71  
  72      /**
  73       * {@inheritdoc}
  74       */
  75      public function getSize() : ?int
  76      {
  77          return null;
  78      }
  79  
  80      /**
  81       * {@inheritdoc}
  82       */
  83      public function tell() : int
  84      {
  85          throw Exception\UntellableStreamException::forCallbackStream();
  86      }
  87  
  88      /**
  89       * {@inheritdoc}
  90       */
  91      public function eof() : bool
  92      {
  93          return empty($this->callback);
  94      }
  95  
  96      /**
  97       * {@inheritdoc}
  98       */
  99      public function isSeekable() : bool
 100      {
 101          return false;
 102      }
 103  
 104      /**
 105       * {@inheritdoc}
 106       */
 107      public function seek($offset, $whence = SEEK_SET)
 108      {
 109          throw Exception\UnseekableStreamException::forCallbackStream();
 110      }
 111  
 112      /**
 113       * {@inheritdoc}
 114       */
 115      public function rewind() : void
 116      {
 117          throw Exception\UnrewindableStreamException::forCallbackStream();
 118      }
 119  
 120      /**
 121       * {@inheritdoc}
 122       */
 123      public function isWritable() : bool
 124      {
 125          return false;
 126      }
 127  
 128      /**
 129       * {@inheritdoc}
 130       */
 131      public function write($string) : void
 132      {
 133          throw Exception\UnwritableStreamException::forCallbackStream();
 134      }
 135  
 136      /**
 137       * {@inheritdoc}
 138       */
 139      public function isReadable() : bool
 140      {
 141          return false;
 142      }
 143  
 144      /**
 145       * {@inheritdoc}
 146       */
 147      public function read($length) : string
 148      {
 149          throw Exception\UnreadableStreamException::forCallbackStream();
 150      }
 151  
 152      /**
 153       * {@inheritdoc}
 154       */
 155      public function getContents() : string
 156      {
 157          $callback = $this->detach();
 158          $contents = $callback ? $callback() : '';
 159          return (string) $contents;
 160      }
 161  
 162      /**
 163       * {@inheritdoc}
 164       */
 165      public function getMetadata($key = null)
 166      {
 167          $metadata = [
 168              'eof' => $this->eof(),
 169              'stream_type' => 'callback',
 170              'seekable' => false
 171          ];
 172  
 173          if (null === $key) {
 174              return $metadata;
 175          }
 176  
 177          if (! array_key_exists($key, $metadata)) {
 178              return null;
 179          }
 180  
 181          return $metadata[$key];
 182      }
 183  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer