2declare(strict_types=1);
81 throw new \InvalidArgumentException(
'Stream must be a valid resource but "' . gettype(
$stream) .
'" was given.');
84 if ($options !==
null) {
85 $this->customMetadata = $options->getMetadata();
86 $this->size = ($options->getSize() !== -1) ? $options->getSize() :
null;
88 $this->customMetadata = [];
93 $meta = stream_get_meta_data($this->stream);
94 $mode = $meta[
'mode'];
96 $this->readable = array_key_exists($mode,
97 self::$accessMap) && boolval(self::$accessMap[$mode]&self::MASK_ACCESS_READ);
98 $this->writeable = array_key_exists($mode,
99 self::$accessMap) && boolval(self::$accessMap[$mode]&self::MASK_ACCESS_WRITE);
100 $this->seekable = boolval($meta[
'seekable']);
109 if ($this->stream !==
null && is_resource($this->stream)) {
122 $this->stream = $this->size = $this->uri =
null;
134 if ($this->size !==
null) {
139 if ($this->stream ===
null) {
144 if ($this->uri !==
null) {
145 clearstatcache(
true, $this->uri);
148 $stats = fstat($this->stream);
149 if (array_key_exists(
'size', $stats)) {
150 $this->size = $stats[
'size'];
168 throw new \RuntimeException(
'Unable to determine stream position');
181 return feof($this->stream);
195 public function seek($offset, $whence = SEEK_SET)
200 throw new \RuntimeException(
'Stream is not seekable');
204 throw new \RuntimeException(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
232 throw new \RuntimeException(
'Can not write to a non-writable stream');
240 throw new \RuntimeException(
'Unable to write to stream');
262 throw new \RuntimeException(
'Can not read from non-readable stream');
266 throw new \RuntimeException(
'Length parameter must not be negative');
274 if ($junk ===
false) {
275 throw new \RuntimeException(
'Unable to read from stream');
290 if ($content ===
false) {
291 throw new \RuntimeException(
'Unable to read stream contents');
304 if ($this->stream ===
null) {
310 return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
316 if (array_key_exists($key, $this->customMetadata)) {
317 return $this->customMetadata[$key];
321 $meta = stream_get_meta_data($this->stream);
322 if (array_key_exists($key, $meta)) {
338 }
catch (\Exception $ex) {
351 if (!is_null($this->stream)) {
363 if ($this->stream ===
null) {
364 throw new \RuntimeException(
'Stream is detached');
An exception for terminatinating execution or to throw for unit testing.
Class StreamOptions 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
Class PHPFunctions The purpose of this class is to wrap all stream handling php functions.
static fwrite($handle, $string, $length=null)
fwrite wrapper
static fread($handle, $length)
fread wrapper
static stream_get_contents($handle, $length=-1)
stream_get_contents wrapper
static ftell($handle)
ftell wrapper
static fclose($handle)
fclose wrapper
static fseek($stream, $offset, $whence)
fseek wrapper.
Interface FileStream The base interface for all filesystem streams.