3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use InvalidArgumentException;
14 use Psr\Http\Message\StreamInterface;
17 * Implementation of PSR HTTP streams
19 class Stream implements StreamInterface
27 * @var string|resource
32 * @param string|resource $stream
33 * @param string $mode Mode with which to open stream
34 * @throws InvalidArgumentException
36 public function __construct($stream, $mode = 'r')
38 $this->setStream($stream, $mode);
44 public function __toString()
46 if (! $this->isReadable()) {
52 return $this->getContents();
53 } catch (RuntimeException $e) {
61 public function close()
63 if (! $this->resource) {
67 $resource = $this->detach();
74 public function detach()
76 $resource = $this->resource;
77 $this->resource = null;
82 * Attach a new stream/resource to the instance.
84 * @param string|resource $resource
86 * @throws InvalidArgumentException for stream identifier that cannot be
88 * @throws InvalidArgumentException for non-resource stream
90 public function attach($resource, $mode = 'r')
92 $this->setStream($resource, $mode);
98 public function getSize()
100 if (null === $this->resource) {
104 $stats = fstat($this->resource);
105 return $stats['size'];
111 public function tell()
113 if (! $this->resource) {
114 throw new RuntimeException('No resource available; cannot tell position');
117 $result = ftell($this->resource);
118 if (! is_int($result)) {
119 throw new RuntimeException('Error occurred during tell operation');
128 public function eof()
130 if (! $this->resource) {
134 return feof($this->resource);
140 public function isSeekable()
142 if (! $this->resource) {
146 $meta = stream_get_meta_data($this->resource);
147 return $meta['seekable'];
153 public function seek($offset, $whence = SEEK_SET)
155 if (! $this->resource) {
156 throw new RuntimeException('No resource available; cannot seek position');
159 if (! $this->isSeekable()) {
160 throw new RuntimeException('Stream is not seekable');
163 $result = fseek($this->resource, $offset, $whence);
166 throw new RuntimeException('Error seeking within stream');
175 public function rewind()
177 return $this->seek(0);
183 public function isWritable()
185 if (! $this->resource) {
189 $meta = stream_get_meta_data($this->resource);
190 $mode = $meta['mode'];
194 || strstr($mode, 'w')
195 || strstr($mode, 'c')
196 || strstr($mode, 'a')
197 || strstr($mode, '+')
204 public function write($string)
206 if (! $this->resource) {
207 throw new RuntimeException('No resource available; cannot write');
210 if (! $this->isWritable()) {
211 throw new RuntimeException('Stream is not writable');
214 $result = fwrite($this->resource, $string);
216 if (false === $result) {
217 throw new RuntimeException('Error writing to stream');
225 public function isReadable()
227 if (! $this->resource) {
231 $meta = stream_get_meta_data($this->resource);
232 $mode = $meta['mode'];
234 return (strstr($mode, 'r') || strstr($mode, '+'));
240 public function read($length)
242 if (! $this->resource) {
243 throw new RuntimeException('No resource available; cannot read');
246 if (! $this->isReadable()) {
247 throw new RuntimeException('Stream is not readable');
250 $result = fread($this->resource, $length);
252 if (false === $result) {
253 throw new RuntimeException('Error reading stream');
262 public function getContents()
264 if (! $this->isReadable()) {
265 throw new RuntimeException('Stream is not readable');
268 $result = stream_get_contents($this->resource);
269 if (false === $result) {
270 throw new RuntimeException('Error reading from stream');
278 public function getMetadata($key = null)
281 return stream_get_meta_data($this->resource);
284 $metadata = stream_get_meta_data($this->resource);
285 if (! array_key_exists($key, $metadata)) {
289 return $metadata[$key];
293 * Set the internal stream resource.
295 * @param string|resource $stream String stream target or stream resource.
296 * @param string $mode Resource mode for stream target.
297 * @throws InvalidArgumentException for invalid streams or resources.
299 private function setStream($stream, $mode = 'r')
304 if (is_string($stream)) {
305 set_error_handler(function ($e) use (&$error) {
308 $resource = fopen($stream, $mode);
309 restore_error_handler();
313 throw new InvalidArgumentException('Invalid stream reference provided');
316 if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) {
317 throw new InvalidArgumentException(
318 'Invalid stream provided; must be a string stream identifier or stream resource'
322 if ($stream !== $resource) {
323 $this->stream = $stream;
326 $this->resource = $resource;