3declare(strict_types=1);
69 private ?
string $uri =
null;
85 throw new \InvalidArgumentException(
'Stream must be a valid resource but "' . gettype(
$stream) .
'" was given.');
88 if ($options !==
null) {
89 $this->customMetadata = $options->getMetadata();
90 $this->size = ($options->getSize() !== -1) ? $options->getSize() :
null;
92 $this->customMetadata = [];
97 $meta = stream_get_meta_data($this->stream);
98 $mode = $meta[
'mode'];
100 $this->readable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_READ);
101 $this->writeable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_WRITE);
102 $this->seekable = boolval($meta[
'seekable']);
112 if (is_resource($this->stream)) {
126 $this->stream = $this->size = $this->uri =
null;
138 if ($this->size !==
null) {
143 if ($this->stream ===
null) {
148 if ($this->uri !==
null) {
149 clearstatcache(
true, $this->uri);
152 $stats = fstat($this->stream);
153 if (array_key_exists(
'size', $stats)) {
154 $this->size = $stats[
'size'];
172 if ($result ===
false) {
173 throw new \RuntimeException(
'Unable to determine stream position');
183 public function eof(): bool
187 return feof($this->stream);
203 public function seek($offset, $whence = SEEK_SET): void
208 throw new \RuntimeException(
'Stream is not seekable');
212 throw new \RuntimeException(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
243 throw new \RuntimeException(
'Can not write to a non-writable stream');
250 if ($result ===
false) {
251 throw new \RuntimeException(
'Unable to write to stream');
275 throw new \RuntimeException(
'Can not read from non-readable stream');
279 throw new \RuntimeException(
'Length parameter must not be negative');
287 if ($junk ===
false) {
288 throw new \RuntimeException(
'Unable to read from stream');
304 if ($content ===
false) {
305 throw new \RuntimeException(
'Unable to read stream contents');
319 if ($this->stream ===
null) {
325 return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
331 if (array_key_exists(
$key, $this->customMetadata)) {
332 return $this->customMetadata[
$key];
336 $meta = stream_get_meta_data($this->stream);
337 if (array_key_exists(
$key, $meta)) {
353 }
catch (\Exception $ex) {
367 if (!is_null($this->stream)) {
381 if ($this->stream ===
null) {
382 throw new \RuntimeException(
'Stream is detached');
getMetadata($key=null)
@inheritDoc
__construct($stream, StreamOptions $options=null)
Stream constructor.
const MASK_ACCESS_READ_WRITE
assertStreamAttached()
Checks if the stream is attached to the wrapper.
write($string)
@inheritDoc
seek($offset, $whence=SEEK_SET)
@inheritDoc
static fread($handle, int $length)
static stream_get_contents($handle, $length=-1)
static fseek($stream, int $offset, int $whence)
static ftell($handle)
ftell wrapper
static fwrite($handle, string $string, ?int $length=null)