| @@ -1,32 +1,34 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | 8 | * PHP stream implementation. |
| 8 | - * | |
| 9 | - * @var $stream | |
| 10 | 9 | */ |
| 11 | 10 | class Stream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface |
| 12 | 11 | { |
| 13 | 12 | /** |
| 14 | - * Resource modes. | |
| 15 | - * | |
| 16 | - * @var string | |
| 17 | - * | |
| 18 | - * @see http://php.net/manual/function.fopen.php | |
| 19 | - * @see http://php.net/manual/en/function.gzopen.php | |
| 13 | + * @see https://www.php.net/manual/en/function.fopen.php | |
| 14 | + * @see https://www.php.net/manual/en/function.gzopen.php | |
| 20 | 15 | */ |
| 21 | - const READABLE_MODES = '/r|a\\+|ab\\+|w\\+|wb\\+|x\\+|xb\\+|c\\+|cb\\+/'; | |
| 22 | - const WRITABLE_MODES = '/a|w|r\\+|rb\\+|rw|x|c/'; | |
| 16 | + private const READABLE_MODES = '/r|a\\+|ab\\+|w\\+|wb\\+|x\\+|xb\\+|c\\+|cb\\+/'; | |
| 17 | + private const WRITABLE_MODES = '/a|w|r\\+|rb\\+|rw|x|c/'; | |
| 18 | + /** @var resource */ | |
| 23 | 19 | private $stream; |
| 20 | + /** @var int|null */ | |
| 24 | 21 | private $size; |
| 22 | + /** @var bool */ | |
| 25 | 23 | private $seekable; |
| 24 | + /** @var bool */ | |
| 26 | 25 | private $readable; |
| 26 | + /** @var bool */ | |
| 27 | 27 | private $writable; |
| 28 | + /** @var string|null */ | |
| 28 | 29 | private $uri; |
| 30 | + /** @var mixed[] */ | |
| 29 | 31 | private $customMetadata; |
| 30 | 32 | /** |
| 31 | 33 | * This constructor accepts an associative array of options. |
| 32 | 34 | * |
| @@ -35,14 +37,14 @@ | ||
| 35 | 37 | * provide that size, in bytes. |
| 36 | 38 | * - metadata: (array) Any additional metadata to return when the metadata |
| 37 | 39 | * of the stream is accessed. |
| 38 | 40 | * |
| 39 | - * @param resource $stream Stream resource to wrap. | |
| 40 | - * @param array $options Associative array of options. | |
| 41 | + * @param resource $stream Stream resource to wrap. | |
| 42 | + * @param array{size?: int, metadata?: array} $options Associative array of options. | |
| 41 | 43 | * |
| 42 | 44 | * @throws \InvalidArgumentException if the stream is not a stream resource |
| 43 | 45 | */ |
| 44 | - public function __construct($stream, $options = []) | |
| 46 | + public function __construct($stream, array $options = []) | |
| 45 | 47 | { |
| 46 | 48 | if (!\is_resource($stream)) { |
| 47 | 49 | throw new \InvalidArgumentException('Stream must be a resource'); |
| 48 | 50 | } |
| @@ -48,15 +50,15 @@ | ||
| 48 | 50 | } |
| 49 | 51 | if (isset($options['size'])) { |
| 50 | 52 | $this->size = $options['size']; |
| 51 | 53 | } |
| 52 | - $this->customMetadata = isset($options['metadata']) ? $options['metadata'] : []; | |
| 54 | + $this->customMetadata = $options['metadata'] ?? []; | |
| 53 | 55 | $this->stream = $stream; |
| 54 | 56 | $meta = \stream_get_meta_data($this->stream); |
| 55 | 57 | $this->seekable = $meta['seekable']; |
| 56 | 58 | $this->readable = (bool) \preg_match(self::READABLE_MODES, $meta['mode']); |
| 57 | 59 | $this->writable = (bool) \preg_match(self::WRITABLE_MODES, $meta['mode']); |
| 58 | - $this->uri = $this->getMetadata('uri'); | |
| 60 | + $this->uri = $meta['uri'] ?? null; | |
| 59 | 61 | } |
| 60 | 62 | /** |
| 61 | 63 | * Closes the stream when the destructed |
| 62 | 64 | */ |
| @@ -63,9 +65,9 @@ | ||
| 63 | 65 | public function __destruct() |
| 64 | 66 | { |
| 65 | 67 | $this->close(); |
| 66 | 68 | } |
| 67 | - public function __toString() | |
| 69 | + public function __toString() : string | |
| 68 | 70 | { |
| 69 | 71 | try { |
| 70 | 72 | if ($this->isSeekable()) { |
| 71 | 73 | $this->seek(0); |
| @@ -70,24 +72,27 @@ | ||
| 70 | 72 | if ($this->isSeekable()) { |
| 71 | 73 | $this->seek(0); |
| 72 | 74 | } |
| 73 | 75 | return $this->getContents(); |
| 74 | - } catch (\Exception $e) { | |
| 76 | + } catch (\Throwable $e) { | |
| 77 | + if (\PHP_VERSION_ID >= 70400) { | |
| 78 | + throw $e; | |
| 79 | + } | |
| 80 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 75 | 81 | return ''; |
| 76 | 82 | } |
| 77 | 83 | } |
| 78 | - public function getContents() | |
| 84 | + public function getContents() : string | |
| 79 | 85 | { |
| 80 | 86 | if (!isset($this->stream)) { |
| 81 | 87 | throw new \RuntimeException('Stream is detached'); |
| 82 | 88 | } |
| 83 | - $contents = \stream_get_contents($this->stream); | |
| 84 | - if ($contents === \false) { | |
| 85 | - throw new \RuntimeException('Unable to read stream contents'); | |
| 89 | + if (!$this->readable) { | |
| 90 | + throw new \RuntimeException('Cannot read from non-readable stream'); | |
| 86 | 91 | } |
| 87 | - return $contents; | |
| 92 | + return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::tryGetContents($this->stream); | |
| 88 | 93 | } |
| 89 | - public function close() | |
| 94 | + public function close() : void | |
| 90 | 95 | { |
| 91 | 96 | if (isset($this->stream)) { |
| 92 | 97 | if (\is_resource($this->stream)) { |
| 93 | 98 | \fclose($this->stream); |
| @@ -105,9 +110,9 @@ | ||
| 105 | 110 | $this->size = $this->uri = null; |
| 106 | 111 | $this->readable = $this->writable = $this->seekable = \false; |
| 107 | 112 | return $result; |
| 108 | 113 | } |
| 109 | - public function getSize() | |
| 114 | + public function getSize() : ?int | |
| 110 | 115 | { |
| 111 | 116 | if ($this->size !== null) { |
| 112 | 117 | return $this->size; |
| 113 | 118 | } |
| @@ -118,27 +123,27 @@ | ||
| 118 | 123 | if ($this->uri) { |
| 119 | 124 | \clearstatcache(\true, $this->uri); |
| 120 | 125 | } |
| 121 | 126 | $stats = \fstat($this->stream); |
| 122 | - if (isset($stats['size'])) { | |
| 127 | + if (\is_array($stats) && isset($stats['size'])) { | |
| 123 | 128 | $this->size = $stats['size']; |
| 124 | 129 | return $this->size; |
| 125 | 130 | } |
| 126 | 131 | return null; |
| 127 | 132 | } |
| 128 | - public function isReadable() | |
| 133 | + public function isReadable() : bool | |
| 129 | 134 | { |
| 130 | 135 | return $this->readable; |
| 131 | 136 | } |
| 132 | - public function isWritable() | |
| 137 | + public function isWritable() : bool | |
| 133 | 138 | { |
| 134 | 139 | return $this->writable; |
| 135 | 140 | } |
| 136 | - public function isSeekable() | |
| 141 | + public function isSeekable() : bool | |
| 137 | 142 | { |
| 138 | 143 | return $this->seekable; |
| 139 | 144 | } |
| 140 | - public function eof() | |
| 145 | + public function eof() : bool | |
| 141 | 146 | { |
| 142 | 147 | if (!isset($this->stream)) { |
| 143 | 148 | throw new \RuntimeException('Stream is detached'); |
| 144 | 149 | } |
| @@ -143,9 +148,9 @@ | ||
| 143 | 148 | throw new \RuntimeException('Stream is detached'); |
| 144 | 149 | } |
| 145 | 150 | return \feof($this->stream); |
| 146 | 151 | } |
| 147 | - public function tell() | |
| 152 | + public function tell() : int | |
| 148 | 153 | { |
| 149 | 154 | if (!isset($this->stream)) { |
| 150 | 155 | throw new \RuntimeException('Stream is detached'); |
| 151 | 156 | } |
| @@ -154,14 +159,20 @@ | ||
| 154 | 159 | throw new \RuntimeException('Unable to determine stream position'); |
| 155 | 160 | } |
| 156 | 161 | return $result; |
| 157 | 162 | } |
| 158 | - public function rewind() | |
| 163 | + public function rewind() : void | |
| 159 | 164 | { |
| 160 | 165 | $this->seek(0); |
| 161 | 166 | } |
| 162 | - public function seek($offset, $whence = \SEEK_SET) | |
| 167 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 163 | 168 | { |
| 169 | + if (!\is_int($offset)) { | |
| 170 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.', \get_debug_type($offset)); | |
| 171 | + } | |
| 172 | + if (!\is_int($whence)) { | |
| 173 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.', \get_debug_type($whence)); | |
| 174 | + } | |
| 164 | 175 | $whence = (int) $whence; |
| 165 | 176 | if (!isset($this->stream)) { |
| 166 | 177 | throw new \RuntimeException('Stream is detached'); |
| 167 | 178 | } |
| @@ -171,10 +182,13 @@ | ||
| 171 | 182 | if (\fseek($this->stream, $offset, $whence) === -1) { |
| 172 | 183 | throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, \true)); |
| 173 | 184 | } |
| 174 | 185 | } |
| 175 | - public function read($length) | |
| 186 | + public function read($length) : string | |
| 176 | 187 | { |
| 188 | + if (!\is_int($length)) { | |
| 189 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.', \get_debug_type($length)); | |
| 190 | + } | |
| 177 | 191 | if (!isset($this->stream)) { |
| 178 | 192 | throw new \RuntimeException('Stream is detached'); |
| 179 | 193 | } |
| 180 | 194 | if (!$this->readable) { |
| @@ -185,16 +199,23 @@ | ||
| 185 | 199 | } |
| 186 | 200 | if (0 === $length) { |
| 187 | 201 | return ''; |
| 188 | 202 | } |
| 189 | - $string = \fread($this->stream, $length); | |
| 203 | + try { | |
| 204 | + $string = \fread($this->stream, $length); | |
| 205 | + } catch (\Exception $e) { | |
| 206 | + throw new \RuntimeException('Unable to read from stream', 0, $e); | |
| 207 | + } | |
| 190 | 208 | if (\false === $string) { |
| 191 | 209 | throw new \RuntimeException('Unable to read from stream'); |
| 192 | 210 | } |
| 193 | 211 | return $string; |
| 194 | 212 | } |
| 195 | - public function write($string) | |
| 213 | + public function write($string) : int | |
| 196 | 214 | { |
| 215 | + if (!\is_string($string)) { | |
| 216 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.', \get_debug_type($string)); | |
| 217 | + } | |
| 197 | 218 | if (!isset($this->stream)) { |
| 198 | 219 | throw new \RuntimeException('Stream is detached'); |
| 199 | 220 | } |
| 200 | 221 | if (!$this->writable) { |
| @@ -207,10 +228,16 @@ | ||
| 207 | 228 | throw new \RuntimeException('Unable to write to stream'); |
| 208 | 229 | } |
| 209 | 230 | return $result; |
| 210 | 231 | } |
| 232 | + /** | |
| 233 | + * @return mixed | |
| 234 | + */ | |
| 211 | 235 | public function getMetadata($key = null) |
| 212 | 236 | { |
| 237 | + if ($key !== null && !\is_string($key)) { | |
| 238 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.', \get_debug_type($key)); | |
| 239 | + } | |
| 213 | 240 | if (!isset($this->stream)) { |
| 214 | 241 | return $key ? null : []; |
| 215 | 242 | } elseif (!$key) { |
| 216 | 243 | return $this->customMetadata + \stream_get_meta_data($this->stream); |
| @@ -217,7 +244,7 @@ | ||
| 217 | 244 | } elseif (isset($this->customMetadata[$key])) { |
| 218 | 245 | return $this->customMetadata[$key]; |
| 219 | 246 | } |
| 220 | 247 | $meta = \stream_get_meta_data($this->stream); |
| 221 | - return isset($meta[$key]) ? $meta[$key] : null; | |
| 248 | + return $meta[$key] ?? null; | |
| 222 | 249 | } |
| 223 | 250 | } |