| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\GuzzleHttp\Psr7\Exception\TimeoutException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 8 |
/** |
| 9 |
* PHP stream implementation. |
| 10 |
*/ |
| 11 |
class Stream implements StreamInterface |
| 12 |
{ |
| 13 |
use NonSerializableStreamTrait; |
| 14 |
/** @var resource */ |
| 15 |
private $stream; |
| 16 |
private ?int $size = null; |
| 17 |
private bool $seekable; |
| 18 |
private bool $readable; |
| 19 |
private bool $writable; |
| 20 |
private ?string $uri = null; |
| 21 |
/** @var mixed[] */ |
| 22 |
private array $customMetadata; |
| 23 |
/** |
| 24 |
* This constructor accepts an associative array of options. |
| 25 |
* |
| 26 |
* - size: (int) If a read stream would otherwise have an indeterminate |
| 27 |
* size, but the size is known due to foreknowledge, then you can |
| 28 |
* provide that size, in bytes. |
| 29 |
* - metadata: (array) Any additional metadata to return when the metadata |
| 30 |
* of the stream is accessed. |
| 31 |
* |
| 32 |
* @param resource $stream Stream resource to wrap. |
| 33 |
* @param array{size?: int, metadata?: array} $options Associative array of options. |
| 34 |
* |
| 35 |
* @throws \InvalidArgumentException if the stream is not a stream resource |
| 36 |
*/ |
| 37 |
public function __construct($stream, array $options = []) |
| 38 |
{ |
| 39 |
if (!\is_resource($stream)) { |
| 40 |
throw new \InvalidArgumentException('Stream must be a resource'); |
| 41 |
} |
| 42 |
$this->size = Integers::assertOptionalNonNegativeSize($options['size'] ?? null, 'Stream size'); |
| 43 |
$this->customMetadata = $options['metadata'] ?? []; |
| 44 |
$this->stream = $stream; |
| 45 |
$meta = \stream_get_meta_data($this->stream); |
| 46 |
$this->seekable = $meta['seekable']; |
| 47 |
$this->readable = self::isReadableMode($meta['mode']); |
| 48 |
$this->writable = self::isWritableMode($meta['mode']); |
| 49 |
$this->uri = $meta['uri'] ?? null; |
| 50 |
} |
| 51 |
/** |
| 52 |
* Closes the stream when the destructed |
| 53 |
*/ |
| 54 |
public function __destruct() |
| 55 |
{ |
| 56 |
$this->close(); |
| 57 |
} |
| 58 |
public function __toString() : string |
| 59 |
{ |
| 60 |
if ($this->isSeekable()) { |
| 61 |
$this->seek(0); |
| 62 |
} |
| 63 |
return $this->getContents(); |
| 64 |
} |
| 65 |
public function getContents() : string |
| 66 |
{ |
| 67 |
if (!isset($this->stream)) { |
| 68 |
throw new \RuntimeException('Stream is detached'); |
| 69 |
} |
| 70 |
if (!$this->readable) { |
| 71 |
throw new \RuntimeException('Cannot read from non-readable stream'); |
| 72 |
} |
| 73 |
return Utils::tryGetContents($this->stream); |
| 74 |
} |
| 75 |
public function close() : void |
| 76 |
{ |
| 77 |
if (isset($this->stream)) { |
| 78 |
if (\is_resource($this->stream)) { |
| 79 |
\fclose($this->stream); |
| 80 |
} |
| 81 |
$this->detach(); |
| 82 |
} |
| 83 |
} |
| 84 |
public function detach() |
| 85 |
{ |
| 86 |
if (!isset($this->stream)) { |
| 87 |
return null; |
| 88 |
} |
| 89 |
$result = $this->stream; |
| 90 |
unset($this->stream); |
| 91 |
$this->size = $this->uri = null; |
| 92 |
$this->readable = $this->writable = $this->seekable = \false; |
| 93 |
return $result; |
| 94 |
} |
| 95 |
public function getSize() : ?int |
| 96 |
{ |
| 97 |
if ($this->size !== null) { |
| 98 |
return $this->size; |
| 99 |
} |
| 100 |
if (!isset($this->stream)) { |
| 101 |
return null; |
| 102 |
} |
| 103 |
// Clear the stat cache if the stream has a URI |
| 104 |
if ($this->uri) { |
| 105 |
\clearstatcache(\true, $this->uri); |
| 106 |
} |
| 107 |
$stats = \fstat($this->stream); |
| 108 |
if ($stats === \false) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
$this->size = Integers::assertEngineInteger($stats['size'], 'Stream size'); |
| 112 |
return $this->size; |
| 113 |
} |
| 114 |
public function isReadable() : bool |
| 115 |
{ |
| 116 |
return $this->readable; |
| 117 |
} |
| 118 |
public function isWritable() : bool |
| 119 |
{ |
| 120 |
return $this->writable; |
| 121 |
} |
| 122 |
public function isSeekable() : bool |
| 123 |
{ |
| 124 |
return $this->seekable; |
| 125 |
} |
| 126 |
public function eof() : bool |
| 127 |
{ |
| 128 |
if (!isset($this->stream)) { |
| 129 |
throw new \RuntimeException('Stream is detached'); |
| 130 |
} |
| 131 |
return \feof($this->stream); |
| 132 |
} |
| 133 |
public function tell() : int |
| 134 |
{ |
| 135 |
if (!isset($this->stream)) { |
| 136 |
throw new \RuntimeException('Stream is detached'); |
| 137 |
} |
| 138 |
$result = \ftell($this->stream); |
| 139 |
if ($result === \false) { |
| 140 |
throw new \RuntimeException('Unable to determine stream position'); |
| 141 |
} |
| 142 |
$position = Integers::assertEngineInteger($result, 'Stream position'); |
| 143 |
if ($position === null) { |
| 144 |
throw new \RuntimeException('Unable to determine stream position'); |
| 145 |
} |
| 146 |
return $position; |
| 147 |
} |
| 148 |
public function rewind() : void |
| 149 |
{ |
| 150 |
$this->seek(0); |
| 151 |
} |
| 152 |
public function seek(int $offset, int $whence = \SEEK_SET) : void |
| 153 |
{ |
| 154 |
if (!isset($this->stream)) { |
| 155 |
throw new \RuntimeException('Stream is detached'); |
| 156 |
} |
| 157 |
if (!$this->seekable) { |
| 158 |
throw new \RuntimeException('Stream is not seekable'); |
| 159 |
} |
| 160 |
if (\fseek($this->stream, $offset, $whence) === -1) { |
| 161 |
throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, \true)); |
| 162 |
} |
| 163 |
} |
| 164 |
public function read(int $length) : string |
| 165 |
{ |
| 166 |
if (!isset($this->stream)) { |
| 167 |
throw new \RuntimeException('Stream is detached'); |
| 168 |
} |
| 169 |
if (!$this->readable) { |
| 170 |
throw new \RuntimeException('Cannot read from non-readable stream'); |
| 171 |
} |
| 172 |
if ($length < 0) { |
| 173 |
throw new \RuntimeException('Length parameter cannot be negative'); |
| 174 |
} |
| 175 |
if (0 === $length) { |
| 176 |
return ''; |
| 177 |
} |
| 178 |
try { |
| 179 |
$string = \fread($this->stream, $length); |
| 180 |
} catch (TimeoutException $e) { |
| 181 |
throw $e; |
| 182 |
} catch (\Exception $e) { |
| 183 |
if ($this->timedOut()) { |
| 184 |
throw new TimeoutException('Unable to read from stream: timed out', 0, $e); |
| 185 |
} |
| 186 |
throw new \RuntimeException('Unable to read from stream', 0, $e); |
| 187 |
} |
| 188 |
if (\false === $string) { |
| 189 |
if ($this->timedOut()) { |
| 190 |
throw new TimeoutException('Unable to read from stream: timed out'); |
| 191 |
} |
| 192 |
throw new \RuntimeException('Unable to read from stream'); |
| 193 |
} |
| 194 |
if ($string === '' && $this->timedOut()) { |
| 195 |
throw new TimeoutException('Unable to read from stream: timed out'); |
| 196 |
} |
| 197 |
return $string; |
| 198 |
} |
| 199 |
public function write(string $string) : int |
| 200 |
{ |
| 201 |
if (!isset($this->stream)) { |
| 202 |
throw new \RuntimeException('Stream is detached'); |
| 203 |
} |
| 204 |
if (!$this->writable) { |
| 205 |
throw new \RuntimeException('Cannot write to a non-writable stream'); |
| 206 |
} |
| 207 |
if ($string === '') { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
// We can't know the size after writing anything |
| 211 |
$this->size = null; |
| 212 |
try { |
| 213 |
$result = \fwrite($this->stream, $string); |
| 214 |
} catch (TimeoutException $e) { |
| 215 |
throw $e; |
| 216 |
} catch (\Exception $e) { |
| 217 |
if ($this->writeTimedOut()) { |
| 218 |
throw new TimeoutException('Unable to write to stream: timed out', 0, $e); |
| 219 |
} |
| 220 |
throw new \RuntimeException('Unable to write to stream', 0, $e); |
| 221 |
} |
| 222 |
if ($result === \false) { |
| 223 |
if ($this->writeTimedOut()) { |
| 224 |
throw new TimeoutException('Unable to write to stream: timed out'); |
| 225 |
} |
| 226 |
throw new \RuntimeException('Unable to write to stream'); |
| 227 |
} |
| 228 |
if ($result === 0 && $this->writeTimedOut()) { |
| 229 |
throw new TimeoutException('Unable to write to stream: timed out'); |
| 230 |
} |
| 231 |
return $result; |
| 232 |
} |
| 233 |
/** |
| 234 |
* @return mixed |
| 235 |
*/ |
| 236 |
public function getMetadata(?string $key = null) |
| 237 |
{ |
| 238 |
if (!isset($this->stream)) { |
| 239 |
return $key === null ? [] : null; |
| 240 |
} elseif ($key === null) { |
| 241 |
return $this->customMetadata + \stream_get_meta_data($this->stream); |
| 242 |
} elseif (isset($this->customMetadata[$key])) { |
| 243 |
return $this->customMetadata[$key]; |
| 244 |
} |
| 245 |
$meta = \stream_get_meta_data($this->stream); |
| 246 |
return $meta[$key] ?? null; |
| 247 |
} |
| 248 |
/** |
| 249 |
* @see https://www.php.net/manual/en/function.fopen.php |
| 250 |
* @see https://www.php.net/manual/en/function.gzopen.php |
| 251 |
*/ |
| 252 |
private static function isReadableMode(string $mode) : bool |
| 253 |
{ |
| 254 |
return \str_starts_with($mode, 'r') || \str_contains($mode, '+'); |
| 255 |
} |
| 256 |
/** |
| 257 |
* @see https://www.php.net/manual/en/function.fopen.php |
| 258 |
* @see https://www.php.net/manual/en/function.gzopen.php |
| 259 |
*/ |
| 260 |
private static function isWritableMode(string $mode) : bool |
| 261 |
{ |
| 262 |
return \str_starts_with($mode, 'a') || \str_starts_with($mode, 'w') || \str_starts_with($mode, 'x') || \str_starts_with($mode, 'c') || \str_contains($mode, '+'); |
| 263 |
} |
| 264 |
private function timedOut() : bool |
| 265 |
{ |
| 266 |
return StreamTimeout::isResourceReadTimedOut($this->stream); |
| 267 |
} |
| 268 |
private function writeTimedOut() : bool |
| 269 |
{ |
| 270 |
return StreamTimeout::isResourceWriteTimedOut($this->stream); |
| 271 |
} |
| 272 |
} |
| 273 |
|