[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/ -> Stream.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  use RuntimeException;
  15  
  16  use function array_key_exists;
  17  use function fclose;
  18  use function feof;
  19  use function fopen;
  20  use function fread;
  21  use function fseek;
  22  use function fstat;
  23  use function ftell;
  24  use function fwrite;
  25  use function get_resource_type;
  26  use function is_int;
  27  use function is_resource;
  28  use function is_string;
  29  use function restore_error_handler;
  30  use function set_error_handler;
  31  use function stream_get_contents;
  32  use function stream_get_meta_data;
  33  use function strstr;
  34  
  35  use const E_WARNING;
  36  use const SEEK_SET;
  37  
  38  /**
  39   * Implementation of PSR HTTP streams
  40   */
  41  class Stream implements StreamInterface
  42  {
  43      /**
  44       * A list of allowed stream resource types that are allowed to instantiate a Stream
  45       */
  46      private const ALLOWED_STREAM_RESOURCE_TYPES = ['gd', 'stream'];
  47  
  48      /**
  49       * @var resource|null
  50       */
  51      protected $resource;
  52  
  53      /**
  54       * @var string|resource
  55       */
  56      protected $stream;
  57  
  58      /**
  59       * @param string|resource $stream
  60       * @param string $mode Mode with which to open stream
  61       * @throws Exception\InvalidArgumentException
  62       */
  63      public function __construct($stream, string $mode = 'r')
  64      {
  65          $this->setStream($stream, $mode);
  66      }
  67  
  68      /**
  69       * {@inheritdoc}
  70       */
  71      public function __toString() : string
  72      {
  73          if (! $this->isReadable()) {
  74              return '';
  75          }
  76  
  77          try {
  78              if ($this->isSeekable()) {
  79                  $this->rewind();
  80              }
  81  
  82              return $this->getContents();
  83          } catch (RuntimeException $e) {
  84              return '';
  85          }
  86      }
  87  
  88      /**
  89       * {@inheritdoc}
  90       */
  91      public function close() : void
  92      {
  93          if (! $this->resource) {
  94              return;
  95          }
  96  
  97          $resource = $this->detach();
  98          fclose($resource);
  99      }
 100  
 101      /**
 102       * {@inheritdoc}
 103       */
 104      public function detach()
 105      {
 106          $resource = $this->resource;
 107          $this->resource = null;
 108          return $resource;
 109      }
 110  
 111      /**
 112       * Attach a new stream/resource to the instance.
 113       *
 114       * @param string|resource $resource
 115       * @param string $mode
 116       * @throws Exception\InvalidArgumentException for stream identifier that cannot be
 117       *     cast to a resource
 118       * @throws Exception\InvalidArgumentException for non-resource stream
 119       */
 120      public function attach($resource, string $mode = 'r') : void
 121      {
 122          $this->setStream($resource, $mode);
 123      }
 124  
 125      /**
 126       * {@inheritdoc}
 127       */
 128      public function getSize() : ?int
 129      {
 130          if (null === $this->resource) {
 131              return null;
 132          }
 133  
 134          $stats = fstat($this->resource);
 135          if ($stats !== false) {
 136              return $stats['size'];
 137          }
 138  
 139          return null;
 140      }
 141  
 142      /**
 143       * {@inheritdoc}
 144       */
 145      public function tell() : int
 146      {
 147          if (! $this->resource) {
 148              throw Exception\UntellableStreamException::dueToMissingResource();
 149          }
 150  
 151          $result = ftell($this->resource);
 152          if (! is_int($result)) {
 153              throw Exception\UntellableStreamException::dueToPhpError();
 154          }
 155  
 156          return $result;
 157      }
 158  
 159      /**
 160       * {@inheritdoc}
 161       */
 162      public function eof() : bool
 163      {
 164          if (! $this->resource) {
 165              return true;
 166          }
 167  
 168          return feof($this->resource);
 169      }
 170  
 171      /**
 172       * {@inheritdoc}
 173       */
 174      public function isSeekable() : bool
 175      {
 176          if (! $this->resource) {
 177              return false;
 178          }
 179  
 180          $meta = stream_get_meta_data($this->resource);
 181          return $meta['seekable'];
 182      }
 183  
 184      /**
 185       * {@inheritdoc}
 186       */
 187      public function seek($offset, $whence = SEEK_SET) : void
 188      {
 189          if (! $this->resource) {
 190              throw Exception\UnseekableStreamException::dueToMissingResource();
 191          }
 192  
 193          if (! $this->isSeekable()) {
 194              throw Exception\UnseekableStreamException::dueToConfiguration();
 195          }
 196  
 197          $result = fseek($this->resource, $offset, $whence);
 198  
 199          if (0 !== $result) {
 200              throw Exception\UnseekableStreamException::dueToPhpError();
 201          }
 202      }
 203  
 204      /**
 205       * {@inheritdoc}
 206       */
 207      public function rewind() : void
 208      {
 209          $this->seek(0);
 210      }
 211  
 212      /**
 213       * {@inheritdoc}
 214       */
 215      public function isWritable() : bool
 216      {
 217          if (! $this->resource) {
 218              return false;
 219          }
 220  
 221          $meta = stream_get_meta_data($this->resource);
 222          $mode = $meta['mode'];
 223  
 224          return (
 225              strstr($mode, 'x')
 226              || strstr($mode, 'w')
 227              || strstr($mode, 'c')
 228              || strstr($mode, 'a')
 229              || strstr($mode, '+')
 230          );
 231      }
 232  
 233      /**
 234       * {@inheritdoc}
 235       */
 236      public function write($string) : int
 237      {
 238          if (! $this->resource) {
 239              throw Exception\UnwritableStreamException::dueToMissingResource();
 240          }
 241  
 242          if (! $this->isWritable()) {
 243              throw Exception\UnwritableStreamException::dueToConfiguration();
 244          }
 245  
 246          $result = fwrite($this->resource, $string);
 247  
 248          if (false === $result) {
 249              throw Exception\UnwritableStreamException::dueToPhpError();
 250          }
 251  
 252          return $result;
 253      }
 254  
 255      /**
 256       * {@inheritdoc}
 257       */
 258      public function isReadable() : bool
 259      {
 260          if (! $this->resource) {
 261              return false;
 262          }
 263  
 264          $meta = stream_get_meta_data($this->resource);
 265          $mode = $meta['mode'];
 266  
 267          return (strstr($mode, 'r') || strstr($mode, '+'));
 268      }
 269  
 270      /**
 271       * {@inheritdoc}
 272       */
 273      public function read($length) : string
 274      {
 275          if (! $this->resource) {
 276              throw Exception\UnreadableStreamException::dueToMissingResource();
 277          }
 278  
 279          if (! $this->isReadable()) {
 280              throw Exception\UnreadableStreamException::dueToConfiguration();
 281          }
 282  
 283          $result = fread($this->resource, $length);
 284  
 285          if (false === $result) {
 286              throw Exception\UnreadableStreamException::dueToPhpError();
 287          }
 288  
 289          return $result;
 290      }
 291  
 292      /**
 293       * {@inheritdoc}
 294       */
 295      public function getContents() : string
 296      {
 297          if (! $this->isReadable()) {
 298              throw Exception\UnreadableStreamException::dueToConfiguration();
 299          }
 300  
 301          $result = stream_get_contents($this->resource);
 302          if (false === $result) {
 303              throw Exception\UnreadableStreamException::dueToPhpError();
 304          }
 305          return $result;
 306      }
 307  
 308      /**
 309       * {@inheritdoc}
 310       */
 311      public function getMetadata($key = null)
 312      {
 313          if (null === $key) {
 314              return stream_get_meta_data($this->resource);
 315          }
 316  
 317          $metadata = stream_get_meta_data($this->resource);
 318          if (! array_key_exists($key, $metadata)) {
 319              return null;
 320          }
 321  
 322          return $metadata[$key];
 323      }
 324  
 325      /**
 326       * Set the internal stream resource.
 327       *
 328       * @param string|resource $stream String stream target or stream resource.
 329       * @param string $mode Resource mode for stream target.
 330       * @throws Exception\InvalidArgumentException for invalid streams or resources.
 331       */
 332      private function setStream($stream, string $mode = 'r') : void
 333      {
 334          $error    = null;
 335          $resource = $stream;
 336  
 337          if (is_string($stream)) {
 338              set_error_handler(function ($e) use (&$error) {
 339                  if ($e !== E_WARNING) {
 340                      return;
 341                  }
 342  
 343                  $error = $e;
 344              });
 345              $resource = fopen($stream, $mode);
 346              restore_error_handler();
 347          }
 348  
 349          if ($error) {
 350              throw new Exception\InvalidArgumentException('Invalid stream reference provided');
 351          }
 352  
 353          if (! $this->isValidStreamResourceType($resource)) {
 354              throw new Exception\InvalidArgumentException(
 355                  'Invalid stream provided; must be a string stream identifier or stream resource'
 356              );
 357          }
 358  
 359          if ($stream !== $resource) {
 360              $this->stream = $stream;
 361          }
 362  
 363          $this->resource = $resource;
 364      }
 365  
 366      /**
 367       * Determine if a resource is one of the resource types allowed to instantiate a Stream
 368       *
 369       * @param resource $resource Stream resource.
 370       */
 371      private function isValidStreamResourceType($resource): bool
 372      {
 373          return (
 374              is_resource($resource) &&
 375              in_array(get_resource_type($resource), self::ALLOWED_STREAM_RESOURCE_TYPES, true)
 376          );
 377      }
 378  }


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