2declare(strict_types=1);
86 throw new \InvalidArgumentException(
'Stream must be a valid resource but "' . gettype(
$stream) .
'" was given.');
90 $this->customMetadata =
$options->getMetadata();
93 $this->customMetadata = [];
98 $meta = stream_get_meta_data($this->stream);
99 $mode = $meta[
'mode'];
101 $this->readable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_READ);
102 $this->writeable = array_key_exists($mode, self::$accessMap) && boolval(self::$accessMap[$mode] & self::MASK_ACCESS_WRITE);
103 $this->seekable = boolval($meta[
'seekable']);
113 if ($this->stream !==
null && is_resource($this->stream)) {
127 $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)) {
175 throw new \RuntimeException(
'Unable to determine stream position');
189 return feof($this->stream);
205 public function seek($offset, $whence = SEEK_SET)
210 throw new \RuntimeException(
'Stream is not seekable');
214 throw new \RuntimeException(
"Unable to seek to stream position \"$offset\" with whence \"$whence\"");
245 throw new \RuntimeException(
'Can not write to a non-writable stream');
253 throw new \RuntimeException(
'Unable to write to stream');
277 throw new \RuntimeException(
'Can not read from non-readable stream');
281 throw new \RuntimeException(
'Length parameter must not be negative');
289 if ($junk ===
false) {
290 throw new \RuntimeException(
'Unable to read from stream');
306 if ($content ===
false) {
307 throw new \RuntimeException(
'Unable to read stream contents');
321 if ($this->stream ===
null) {
327 return array_merge(stream_get_meta_data($this->stream), $this->customMetadata);
333 if (array_key_exists(
$key, $this->customMetadata)) {
334 return $this->customMetadata[
$key];
338 $meta = stream_get_meta_data($this->stream);
339 if (array_key_exists(
$key, $meta)) {
355 }
catch (\Exception $ex) {
369 if (!is_null($this->stream)) {
383 if ($this->stream ===
null) {
384 throw new \RuntimeException(
'Stream is detached');
An exception for terminatinating execution or to throw for unit testing.
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 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.