2
namespace GuzzleHttp\Stream;
5
* Stream decorator trait
6
* @property StreamInterface stream
8
trait StreamDecoratorTrait
11
* @param StreamInterface $stream Stream to decorate
13
public function __construct(StreamInterface $stream)
15
$this->stream = $stream;
19
* Magic method used to create a new stream if streams are not added in
20
* the constructor of a decorator (e.g., LazyOpenStream).
22
public function __get($name)
24
if ($name == 'stream') {
25
$this->stream = $this->createStream();
29
throw new \UnexpectedValueException("$name not found on class");
32
public function __toString()
36
return $this->getContents();
37
} catch (\Exception $e) {
38
// Really, PHP? https://bugs.php.net/bug.php?id=53648
39
trigger_error('StreamDecorator::__toString exception: '
40
. (string) $e, E_USER_ERROR);
45
public function getContents($maxLength = -1)
47
return Utils::copyToString($this, $maxLength);
51
* Allow decorators to implement custom methods
53
* @param string $method Missing method name
54
* @param array $args Method arguments
58
public function __call($method, array $args)
60
$result = call_user_func_array(array($this->stream, $method), $args);
62
// Always return the wrapped object if the result is a return $this
63
return $result === $this->stream ? $this : $result;
66
public function close()
68
$this->stream->close();
71
public function getMetadata($key = null)
73
return $this->stream instanceof MetadataStreamInterface
74
? $this->stream->getMetadata($key)
78
public function detach()
80
return $this->stream->detach();
83
public function getSize()
85
return $this->stream->getSize();
90
return $this->stream->eof();
93
public function tell()
95
return $this->stream->tell();
98
public function isReadable()
100
return $this->stream->isReadable();
103
public function isWritable()
105
return $this->stream->isWritable();
108
public function isSeekable()
110
return $this->stream->isSeekable();
113
public function seek($offset, $whence = SEEK_SET)
115
return $this->stream->seek($offset, $whence);
118
public function read($length)
120
return $this->stream->read($length);
123
public function write($string)
125
return $this->stream->write($string);
128
public function flush()
130
return $this->stream->flush();
134
* Implement in subclasses to dynamically create streams when requested.
136
* @return StreamInterface
137
* @throws \BadMethodCallException
139
protected function createStream()
141
throw new \BadMethodCallException('createStream() not implemented in '