Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Psr7;
|
Chris@0
|
3
|
Chris@0
|
4 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Stream decorator trait
|
Chris@0
|
8 * @property StreamInterface stream
|
Chris@0
|
9 */
|
Chris@0
|
10 trait StreamDecoratorTrait
|
Chris@0
|
11 {
|
Chris@0
|
12 /**
|
Chris@0
|
13 * @param StreamInterface $stream Stream to decorate
|
Chris@0
|
14 */
|
Chris@0
|
15 public function __construct(StreamInterface $stream)
|
Chris@0
|
16 {
|
Chris@0
|
17 $this->stream = $stream;
|
Chris@0
|
18 }
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Magic method used to create a new stream if streams are not added in
|
Chris@0
|
22 * the constructor of a decorator (e.g., LazyOpenStream).
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param string $name Name of the property (allows "stream" only).
|
Chris@0
|
25 *
|
Chris@0
|
26 * @return StreamInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 public function __get($name)
|
Chris@0
|
29 {
|
Chris@0
|
30 if ($name == 'stream') {
|
Chris@0
|
31 $this->stream = $this->createStream();
|
Chris@0
|
32 return $this->stream;
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 throw new \UnexpectedValueException("$name not found on class");
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 public function __toString()
|
Chris@0
|
39 {
|
Chris@0
|
40 try {
|
Chris@0
|
41 if ($this->isSeekable()) {
|
Chris@0
|
42 $this->seek(0);
|
Chris@0
|
43 }
|
Chris@0
|
44 return $this->getContents();
|
Chris@0
|
45 } catch (\Exception $e) {
|
Chris@0
|
46 // Really, PHP? https://bugs.php.net/bug.php?id=53648
|
Chris@0
|
47 trigger_error('StreamDecorator::__toString exception: '
|
Chris@0
|
48 . (string) $e, E_USER_ERROR);
|
Chris@0
|
49 return '';
|
Chris@0
|
50 }
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 public function getContents()
|
Chris@0
|
54 {
|
Chris@0
|
55 return copy_to_string($this);
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Allow decorators to implement custom methods
|
Chris@0
|
60 *
|
Chris@0
|
61 * @param string $method Missing method name
|
Chris@0
|
62 * @param array $args Method arguments
|
Chris@0
|
63 *
|
Chris@0
|
64 * @return mixed
|
Chris@0
|
65 */
|
Chris@0
|
66 public function __call($method, array $args)
|
Chris@0
|
67 {
|
Chris@0
|
68 $result = call_user_func_array([$this->stream, $method], $args);
|
Chris@0
|
69
|
Chris@0
|
70 // Always return the wrapped object if the result is a return $this
|
Chris@0
|
71 return $result === $this->stream ? $this : $result;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 public function close()
|
Chris@0
|
75 {
|
Chris@0
|
76 $this->stream->close();
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 public function getMetadata($key = null)
|
Chris@0
|
80 {
|
Chris@0
|
81 return $this->stream->getMetadata($key);
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 public function detach()
|
Chris@0
|
85 {
|
Chris@0
|
86 return $this->stream->detach();
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 public function getSize()
|
Chris@0
|
90 {
|
Chris@0
|
91 return $this->stream->getSize();
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 public function eof()
|
Chris@0
|
95 {
|
Chris@0
|
96 return $this->stream->eof();
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 public function tell()
|
Chris@0
|
100 {
|
Chris@0
|
101 return $this->stream->tell();
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 public function isReadable()
|
Chris@0
|
105 {
|
Chris@0
|
106 return $this->stream->isReadable();
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 public function isWritable()
|
Chris@0
|
110 {
|
Chris@0
|
111 return $this->stream->isWritable();
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 public function isSeekable()
|
Chris@0
|
115 {
|
Chris@0
|
116 return $this->stream->isSeekable();
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 public function rewind()
|
Chris@0
|
120 {
|
Chris@0
|
121 $this->seek(0);
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 public function seek($offset, $whence = SEEK_SET)
|
Chris@0
|
125 {
|
Chris@0
|
126 $this->stream->seek($offset, $whence);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 public function read($length)
|
Chris@0
|
130 {
|
Chris@0
|
131 return $this->stream->read($length);
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 public function write($string)
|
Chris@0
|
135 {
|
Chris@0
|
136 return $this->stream->write($string);
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * Implement in subclasses to dynamically create streams when requested.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @return StreamInterface
|
Chris@0
|
143 * @throws \BadMethodCallException
|
Chris@0
|
144 */
|
Chris@0
|
145 protected function createStream()
|
Chris@0
|
146 {
|
Chris@0
|
147 throw new \BadMethodCallException('Not implemented');
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|