diff --git a/src/Stream.php b/src/Stream.php index 7614d33..2086489 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -15,11 +15,6 @@ * Import classes */ use Psr\Http\Message\StreamInterface; -use Sunrise\Stream\Exception\InvalidArgumentException; -use Sunrise\Stream\Exception\UnreadableStreamException; -use Sunrise\Stream\Exception\UnseekableStreamException; -use Sunrise\Stream\Exception\UntellableStreamException; -use Sunrise\Stream\Exception\UnwritableStreamException; /** * Stream @@ -45,7 +40,7 @@ public function __construct($resource) { if (! \is_resource($resource)) { - throw new InvalidArgumentException('Invalid stream resource'); + throw new Exception\InvalidArgumentException('Invalid stream resource'); } $this->resource = $resource; @@ -126,14 +121,14 @@ public function tell() : int { if (! $this->isResourceable()) { - throw new UntellableStreamException('Stream is not resourceable'); + throw new Exception\UntellableStreamException('Stream is not resourceable'); } $result = \ftell($this->resource); if (false === $result) { - throw new UntellableStreamException('Unable to get the stream pointer position'); + throw new Exception\UntellableStreamException('Unable to get the stream pointer position'); } return $result; @@ -169,19 +164,19 @@ public function rewind() : void { if (! $this->isResourceable()) { - throw new UnseekableStreamException('Stream is not resourceable'); + throw new Exception\UnseekableStreamException('Stream is not resourceable'); } if (! $this->isSeekable()) { - throw new UnseekableStreamException('Stream is not seekable'); + throw new Exception\UnseekableStreamException('Stream is not seekable'); } $result = \fseek($this->resource, 0, \SEEK_SET); if (! (0 === $result)) { - throw new UnseekableStreamException('Unable to move the stream pointer to beginning'); + throw new Exception\UnseekableStreamException('Unable to move the stream pointer to beginning'); } } @@ -201,19 +196,19 @@ public function seek($offset, $whence = \SEEK_SET) : void { if (! $this->isResourceable()) { - throw new UnseekableStreamException('Stream is not resourceable'); + throw new Exception\UnseekableStreamException('Stream is not resourceable'); } if (! $this->isSeekable()) { - throw new UnseekableStreamException('Stream is not seekable'); + throw new Exception\UnseekableStreamException('Stream is not seekable'); } $result = \fseek($this->resource, $offset, $whence); if (! (0 === $result)) { - throw new UnseekableStreamException('Unable to move the stream pointer to the given position'); + throw new Exception\UnseekableStreamException('Unable to move the stream pointer to the given position'); } } @@ -251,19 +246,19 @@ public function write($string) : int { if (! $this->isResourceable()) { - throw new UnwritableStreamException('Stream is not resourceable'); + throw new Exception\UnwritableStreamException('Stream is not resourceable'); } if (! $this->isWritable()) { - throw new UnwritableStreamException('Stream is not writable'); + throw new Exception\UnwritableStreamException('Stream is not writable'); } $result = \fwrite($this->resource, $string); if (false === $result) { - throw new UnwritableStreamException('Unable to write to the stream'); + throw new Exception\UnwritableStreamException('Unable to write to the stream'); } return $result; @@ -284,19 +279,19 @@ public function truncate(int $length = 0) : void { if (! $this->isResourceable()) { - throw new UnwritableStreamException('Stream is not resourceable'); + throw new Exception\UnwritableStreamException('Stream is not resourceable'); } if (! $this->isWritable()) { - throw new UnwritableStreamException('Stream is not writable'); + throw new Exception\UnwritableStreamException('Stream is not writable'); } $result = \ftruncate($this->resource, $length); if (false === $result) { - throw new UnwritableStreamException('Unable to truncate the stream'); + throw new Exception\UnwritableStreamException('Unable to truncate the stream'); } } @@ -332,19 +327,19 @@ public function read($length) : string { if (! $this->isResourceable()) { - throw new UnreadableStreamException('Stream is not resourceable'); + throw new Exception\UnreadableStreamException('Stream is not resourceable'); } if (! $this->isReadable()) { - throw new UnreadableStreamException('Stream is not readable'); + throw new Exception\UnreadableStreamException('Stream is not readable'); } $result = \fread($this->resource, $length); if (false === $result) { - throw new UnreadableStreamException('Unable to read from the stream'); + throw new Exception\UnreadableStreamException('Unable to read from the stream'); } return $result; @@ -363,19 +358,19 @@ public function getContents() : string { if (! $this->isResourceable()) { - throw new UnreadableStreamException('Stream is not resourceable'); + throw new Exception\UnreadableStreamException('Stream is not resourceable'); } if (! $this->isReadable()) { - throw new UnreadableStreamException('Stream is not readable'); + throw new Exception\UnreadableStreamException('Stream is not readable'); } $result = \stream_get_contents($this->resource); if (false === $result) { - throw new UnreadableStreamException('Unable to read remainder of the stream'); + throw new Exception\UnreadableStreamException('Unable to read remainder of the stream'); } return $result; diff --git a/src/StreamFactory.php b/src/StreamFactory.php index 8463d37..bbbd345 100644 --- a/src/StreamFactory.php +++ b/src/StreamFactory.php @@ -45,11 +45,10 @@ public function createStream(string $content = '') : StreamInterface */ public function createStreamFromFile(string $filename, string $mode = 'r') : StreamInterface { - \set_error_handler(function() {}, E_WARNING); - - $resource = \fopen($filename, $mode); - - \restore_error_handler(); + // If the open fails, an error of level E_WARNING is generated. + // You may use @ to suppress this warning. + // http://php.net/manual/en/function.fopen.php + $resource = @ \fopen($filename, $mode); if (false === $resource) {