19declare(strict_types=1);
67 protected ?
string $uri =
null;
82 throw new \InvalidArgumentException(
83 'Stream must be a valid resource but "' . gettype(
$stream) .
'" was given.'
87 if ($options !==
null) {
88 $this->customMetadata = $options->getMetadata();
89 $this->size = ($options->getSize() !== -1) ? $options->getSize() :
null;
91 $this->customMetadata = [];
96 $meta = stream_get_meta_data($this->stream);
97 $this->_mode = $mode = $meta[
'mode'];
99 $this->readable = array_key_exists(
102 ) && (bool) (self::$accessMap[$mode] & self::MASK_ACCESS_READ);
103 $this->writeable = array_key_exists(
106 ) && (bool) (self::$accessMap[$mode] & self::MASK_ACCESS_WRITE);
107 $this->seekable = $meta[
'seekable'];
116 if (is_resource($this->stream)) {
129 $this->stream = $this->size = $this->uri =
null;
140 if ($this->size !==
null) {
145 if ($this->stream ===
null) {
150 if ($this->uri !==
null) {
151 clearstatcache(
true, $this->uri);
154 $stats = fstat($this->stream) ?: [];
155 if (array_key_exists(
'size', $stats)) {
156 $this->size = $stats[
'size'];
173 if ($result ===
false) {
174 throw new \RuntimeException(
'Unable to determine stream position');
183 public function eof(): bool
187 return feof($this->stream);
201 public function seek($offset, $whence = SEEK_SET): void
206 throw new \RuntimeException(
'Stream is not seekable');
210 throw new \RuntimeException(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
238 throw new \RuntimeException(
'Can not write to a non-writable stream');
245 if ($result ===
false) {
246 throw new \RuntimeException(
'Unable to write to stream');
263 public function read($length): string
268 throw new \RuntimeException(
'Can not read from non-readable stream');
272 throw new \RuntimeException(
'Length parameter must not be negative');
280 if ($junk ===
false) {
281 throw new \RuntimeException(
'Unable to read from stream');
296 if ($content ===
false) {
297 throw new \RuntimeException(
'Unable to read stream contents');
309 if ($this->stream ===
null) {
315 return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
321 if (array_key_exists($key, $this->customMetadata)) {
322 return $this->customMetadata[$key];
326 $meta = stream_get_meta_data($this->stream);
327 if (array_key_exists($key, $meta)) {
343 }
catch (\Exception) {
355 if (!is_null($this->stream)) {
368 if ($this->stream ===
null) {
369 throw new \RuntimeException(
'Stream is detached');
The streaming options are used by the stream implementation.
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
The purpose of this class is to wrap all stream handling php functions.
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)
The base interface for all filesystem streams.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...