@@ -19,241 +19,241 @@ |
||
19 | 19 | */ |
20 | 20 | class Stream implements StreamInterface |
21 | 21 | { |
22 | - /** @var resource The resource. */ |
|
23 | - private $resource; |
|
24 | - |
|
25 | - /** @var array The metadata. */ |
|
26 | - private $metadata; |
|
27 | - |
|
28 | - /** @var string[] The read modes. */ |
|
29 | - private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+']; |
|
30 | - |
|
31 | - /** @var string[] The write modes. */ |
|
32 | - private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+']; |
|
33 | - |
|
34 | - /** |
|
35 | - * Construct a Stream object with the given resource. |
|
36 | - * |
|
37 | - * @param resource $resource |
|
38 | - */ |
|
39 | - public function __construct($resource) |
|
40 | - { |
|
41 | - if (!is_resource($resource)) { |
|
42 | - throw new \InvalidArgumentException('Invalid resource'); |
|
43 | - } |
|
44 | - |
|
45 | - $this->resource = $resource; |
|
46 | - $this->metadata = stream_get_meta_data($resource); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Destruct the Stream object. |
|
51 | - */ |
|
52 | - public function __destruct() |
|
53 | - { |
|
54 | - $this->close(); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * {@inheritdoc} |
|
59 | - */ |
|
60 | - public function __toString() |
|
61 | - { |
|
62 | - try { |
|
63 | - $this->seek(0); |
|
64 | - |
|
65 | - return $this->getContents(); |
|
66 | - } catch (\Exception $e) { |
|
67 | - return ''; |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * {@inheritdoc} |
|
73 | - */ |
|
74 | - public function close() |
|
75 | - { |
|
76 | - if (isset($this->resource)) { |
|
77 | - if (is_resource($this->resource)) { |
|
78 | - fclose($this->resource); |
|
79 | - } |
|
80 | - |
|
81 | - $this->detach(); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * {@inheritdoc} |
|
87 | - */ |
|
88 | - public function detach() |
|
89 | - { |
|
90 | - if (!isset($this->resource)) { |
|
91 | - return null; |
|
92 | - } |
|
93 | - |
|
94 | - $result = $this->resource; |
|
95 | - unset($this->resource); |
|
96 | - |
|
97 | - return $result; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * {@inheritdoc} |
|
102 | - */ |
|
103 | - public function getSize() |
|
104 | - { |
|
105 | - if (!isset($this->resource)) { |
|
106 | - return null; |
|
107 | - } |
|
108 | - |
|
109 | - if ($this->getMetadata('uri')) { |
|
110 | - clearstatcache(true, $this->getMetadata('uri')); |
|
111 | - } |
|
112 | - |
|
113 | - $stats = fstat($this->resource); |
|
114 | - |
|
115 | - return isset($stats['size']) ? $stats['size'] : null; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * {@inheritdoc} |
|
120 | - */ |
|
121 | - public function tell() |
|
122 | - { |
|
123 | - try { |
|
124 | - $result = ftell($this->resource); |
|
125 | - } catch (\TypeError $e) { |
|
126 | - throw new \RuntimeException('Error while getting the position of the pointer', 0, $e); |
|
127 | - } |
|
128 | - |
|
129 | - if ($result === false) { |
|
130 | - throw new \RuntimeException('Error while getting the position of the pointer'); |
|
131 | - } |
|
132 | - |
|
133 | - return $result; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * {@inheritdoc} |
|
138 | - */ |
|
139 | - public function eof() |
|
140 | - { |
|
141 | - return isset($this->resource) && feof($this->resource); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - public function isSeekable() |
|
148 | - { |
|
149 | - return isset($this->resource) && $this->getMetadata('seekable'); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * {@inheritdoc} |
|
154 | - */ |
|
155 | - public function seek($offset, $whence = SEEK_SET) |
|
156 | - { |
|
157 | - if (!$this->isSeekable()) { |
|
158 | - throw new \RuntimeException('Stream is not seekable'); |
|
159 | - } |
|
22 | + /** @var resource The resource. */ |
|
23 | + private $resource; |
|
24 | + |
|
25 | + /** @var array The metadata. */ |
|
26 | + private $metadata; |
|
27 | + |
|
28 | + /** @var string[] The read modes. */ |
|
29 | + private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', 'r+t', 'x+t', 'c+t', 'a+']; |
|
30 | + |
|
31 | + /** @var string[] The write modes. */ |
|
32 | + private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', 'r+t', 'x+t', 'c+t', 'a', 'a+']; |
|
33 | + |
|
34 | + /** |
|
35 | + * Construct a Stream object with the given resource. |
|
36 | + * |
|
37 | + * @param resource $resource |
|
38 | + */ |
|
39 | + public function __construct($resource) |
|
40 | + { |
|
41 | + if (!is_resource($resource)) { |
|
42 | + throw new \InvalidArgumentException('Invalid resource'); |
|
43 | + } |
|
44 | + |
|
45 | + $this->resource = $resource; |
|
46 | + $this->metadata = stream_get_meta_data($resource); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Destruct the Stream object. |
|
51 | + */ |
|
52 | + public function __destruct() |
|
53 | + { |
|
54 | + $this->close(); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * {@inheritdoc} |
|
59 | + */ |
|
60 | + public function __toString() |
|
61 | + { |
|
62 | + try { |
|
63 | + $this->seek(0); |
|
64 | + |
|
65 | + return $this->getContents(); |
|
66 | + } catch (\Exception $e) { |
|
67 | + return ''; |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * {@inheritdoc} |
|
73 | + */ |
|
74 | + public function close() |
|
75 | + { |
|
76 | + if (isset($this->resource)) { |
|
77 | + if (is_resource($this->resource)) { |
|
78 | + fclose($this->resource); |
|
79 | + } |
|
80 | + |
|
81 | + $this->detach(); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * {@inheritdoc} |
|
87 | + */ |
|
88 | + public function detach() |
|
89 | + { |
|
90 | + if (!isset($this->resource)) { |
|
91 | + return null; |
|
92 | + } |
|
93 | + |
|
94 | + $result = $this->resource; |
|
95 | + unset($this->resource); |
|
96 | + |
|
97 | + return $result; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * {@inheritdoc} |
|
102 | + */ |
|
103 | + public function getSize() |
|
104 | + { |
|
105 | + if (!isset($this->resource)) { |
|
106 | + return null; |
|
107 | + } |
|
108 | + |
|
109 | + if ($this->getMetadata('uri')) { |
|
110 | + clearstatcache(true, $this->getMetadata('uri')); |
|
111 | + } |
|
112 | + |
|
113 | + $stats = fstat($this->resource); |
|
114 | + |
|
115 | + return isset($stats['size']) ? $stats['size'] : null; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * {@inheritdoc} |
|
120 | + */ |
|
121 | + public function tell() |
|
122 | + { |
|
123 | + try { |
|
124 | + $result = ftell($this->resource); |
|
125 | + } catch (\TypeError $e) { |
|
126 | + throw new \RuntimeException('Error while getting the position of the pointer', 0, $e); |
|
127 | + } |
|
128 | + |
|
129 | + if ($result === false) { |
|
130 | + throw new \RuntimeException('Error while getting the position of the pointer'); |
|
131 | + } |
|
132 | + |
|
133 | + return $result; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * {@inheritdoc} |
|
138 | + */ |
|
139 | + public function eof() |
|
140 | + { |
|
141 | + return isset($this->resource) && feof($this->resource); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + public function isSeekable() |
|
148 | + { |
|
149 | + return isset($this->resource) && $this->getMetadata('seekable'); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * {@inheritdoc} |
|
154 | + */ |
|
155 | + public function seek($offset, $whence = SEEK_SET) |
|
156 | + { |
|
157 | + if (!$this->isSeekable()) { |
|
158 | + throw new \RuntimeException('Stream is not seekable'); |
|
159 | + } |
|
160 | 160 | |
161 | - try { |
|
162 | - $result = fseek($this->resource, $offset, $whence); |
|
163 | - } catch (\TypeError $e) { |
|
164 | - throw new \RuntimeException('Error while seeking the stream', 0, $e); |
|
165 | - } |
|
166 | - |
|
167 | - if ($result === false) { |
|
168 | - throw new \RuntimeException('Error while seeking the stream'); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * {@inheritdoc} |
|
174 | - */ |
|
175 | - public function rewind() |
|
176 | - { |
|
177 | - $this->seek(0); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * {@inheritdoc} |
|
182 | - */ |
|
183 | - public function isWritable() |
|
184 | - { |
|
185 | - return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * {@inheritdoc} |
|
190 | - */ |
|
191 | - public function write($string) |
|
192 | - { |
|
193 | - if (!$this->isWritable()) { |
|
194 | - throw new \RuntimeException('Stream is not writable'); |
|
195 | - } |
|
196 | - |
|
197 | - try { |
|
198 | - $result = fwrite($this->resource, $string); |
|
199 | - } catch (\TypeError $e) { |
|
200 | - throw new \RuntimeException('Error while writing the stream', 0, $e); |
|
201 | - } |
|
202 | - |
|
203 | - if ($result === false) { |
|
204 | - throw new \RuntimeException('Error while writing the stream'); |
|
205 | - } |
|
206 | - |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * {@inheritdoc} |
|
212 | - */ |
|
213 | - public function isReadable() |
|
214 | - { |
|
215 | - return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * {@inheritdoc} |
|
220 | - */ |
|
221 | - public function read($length) |
|
222 | - { |
|
223 | - if (!$this->isReadable()) { |
|
224 | - throw new \RuntimeException('Stream is not readable'); |
|
225 | - } |
|
226 | - |
|
227 | - try { |
|
228 | - $result = stream_get_contents($this->resource, $length); |
|
229 | - } catch (\TypeError $e) { |
|
230 | - throw new \RuntimeException('Error while reading the stream', 0, $e); |
|
231 | - } |
|
232 | - |
|
233 | - if ($result === false) { |
|
234 | - throw new \RuntimeException('Error while reading the stream'); |
|
235 | - } |
|
236 | - |
|
237 | - return $result; |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * {@inheritdoc} |
|
242 | - */ |
|
243 | - public function getContents() |
|
244 | - { |
|
245 | - return $this->read(-1); |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * {@inheritdoc} |
|
250 | - */ |
|
251 | - public function getMetadata($key = null) |
|
252 | - { |
|
253 | - if ($key === null) { |
|
254 | - return $this->metadata; |
|
255 | - } |
|
256 | - |
|
257 | - return isset($this->metadata[$key]) ? $this->metadata[$key] : null; |
|
258 | - } |
|
161 | + try { |
|
162 | + $result = fseek($this->resource, $offset, $whence); |
|
163 | + } catch (\TypeError $e) { |
|
164 | + throw new \RuntimeException('Error while seeking the stream', 0, $e); |
|
165 | + } |
|
166 | + |
|
167 | + if ($result === false) { |
|
168 | + throw new \RuntimeException('Error while seeking the stream'); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * {@inheritdoc} |
|
174 | + */ |
|
175 | + public function rewind() |
|
176 | + { |
|
177 | + $this->seek(0); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * {@inheritdoc} |
|
182 | + */ |
|
183 | + public function isWritable() |
|
184 | + { |
|
185 | + return isset($this->resource) && in_array($this->getMetadata('mode'), self::$writeModes); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * {@inheritdoc} |
|
190 | + */ |
|
191 | + public function write($string) |
|
192 | + { |
|
193 | + if (!$this->isWritable()) { |
|
194 | + throw new \RuntimeException('Stream is not writable'); |
|
195 | + } |
|
196 | + |
|
197 | + try { |
|
198 | + $result = fwrite($this->resource, $string); |
|
199 | + } catch (\TypeError $e) { |
|
200 | + throw new \RuntimeException('Error while writing the stream', 0, $e); |
|
201 | + } |
|
202 | + |
|
203 | + if ($result === false) { |
|
204 | + throw new \RuntimeException('Error while writing the stream'); |
|
205 | + } |
|
206 | + |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * {@inheritdoc} |
|
212 | + */ |
|
213 | + public function isReadable() |
|
214 | + { |
|
215 | + return isset($this->resource) && in_array($this->getMetadata('mode'), self::$readModes); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * {@inheritdoc} |
|
220 | + */ |
|
221 | + public function read($length) |
|
222 | + { |
|
223 | + if (!$this->isReadable()) { |
|
224 | + throw new \RuntimeException('Stream is not readable'); |
|
225 | + } |
|
226 | + |
|
227 | + try { |
|
228 | + $result = stream_get_contents($this->resource, $length); |
|
229 | + } catch (\TypeError $e) { |
|
230 | + throw new \RuntimeException('Error while reading the stream', 0, $e); |
|
231 | + } |
|
232 | + |
|
233 | + if ($result === false) { |
|
234 | + throw new \RuntimeException('Error while reading the stream'); |
|
235 | + } |
|
236 | + |
|
237 | + return $result; |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * {@inheritdoc} |
|
242 | + */ |
|
243 | + public function getContents() |
|
244 | + { |
|
245 | + return $this->read(-1); |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * {@inheritdoc} |
|
250 | + */ |
|
251 | + public function getMetadata($key = null) |
|
252 | + { |
|
253 | + if ($key === null) { |
|
254 | + return $this->metadata; |
|
255 | + } |
|
256 | + |
|
257 | + return isset($this->metadata[$key]) ? $this->metadata[$key] : null; |
|
258 | + } |
|
259 | 259 | } |