Mercurial > hg > cmmr2012-drupal-site
comparison vendor/zendframework/zend-diactoros/src/Stream.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 5311817fb629 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 /** | |
3 * Zend Framework (http://framework.zend.com/) | |
4 * | |
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 | |
8 */ | |
9 | |
10 namespace Zend\Diactoros; | |
11 | |
12 use InvalidArgumentException; | |
13 use RuntimeException; | |
14 use Psr\Http\Message\StreamInterface; | |
15 | |
16 /** | |
17 * Implementation of PSR HTTP streams | |
18 */ | |
19 class Stream implements StreamInterface | |
20 { | |
21 /** | |
22 * @var resource|null | |
23 */ | |
24 protected $resource; | |
25 | |
26 /** | |
27 * @var string|resource | |
28 */ | |
29 protected $stream; | |
30 | |
31 /** | |
32 * @param string|resource $stream | |
33 * @param string $mode Mode with which to open stream | |
34 * @throws InvalidArgumentException | |
35 */ | |
36 public function __construct($stream, $mode = 'r') | |
37 { | |
38 $this->setStream($stream, $mode); | |
39 } | |
40 | |
41 /** | |
42 * {@inheritdoc} | |
43 */ | |
44 public function __toString() | |
45 { | |
46 if (! $this->isReadable()) { | |
47 return ''; | |
48 } | |
49 | |
50 try { | |
51 if ($this->isSeekable()) { | |
52 $this->rewind(); | |
53 } | |
54 | |
55 return $this->getContents(); | |
56 } catch (RuntimeException $e) { | |
57 return ''; | |
58 } | |
59 } | |
60 | |
61 /** | |
62 * {@inheritdoc} | |
63 */ | |
64 public function close() | |
65 { | |
66 if (! $this->resource) { | |
67 return; | |
68 } | |
69 | |
70 $resource = $this->detach(); | |
71 fclose($resource); | |
72 } | |
73 | |
74 /** | |
75 * {@inheritdoc} | |
76 */ | |
77 public function detach() | |
78 { | |
79 $resource = $this->resource; | |
80 $this->resource = null; | |
81 return $resource; | |
82 } | |
83 | |
84 /** | |
85 * Attach a new stream/resource to the instance. | |
86 * | |
87 * @param string|resource $resource | |
88 * @param string $mode | |
89 * @throws InvalidArgumentException for stream identifier that cannot be | |
90 * cast to a resource | |
91 * @throws InvalidArgumentException for non-resource stream | |
92 */ | |
93 public function attach($resource, $mode = 'r') | |
94 { | |
95 $this->setStream($resource, $mode); | |
96 } | |
97 | |
98 /** | |
99 * {@inheritdoc} | |
100 */ | |
101 public function getSize() | |
102 { | |
103 if (null === $this->resource) { | |
104 return null; | |
105 } | |
106 | |
107 $stats = fstat($this->resource); | |
108 return $stats['size']; | |
109 } | |
110 | |
111 /** | |
112 * {@inheritdoc} | |
113 */ | |
114 public function tell() | |
115 { | |
116 if (! $this->resource) { | |
117 throw new RuntimeException('No resource available; cannot tell position'); | |
118 } | |
119 | |
120 $result = ftell($this->resource); | |
121 if (! is_int($result)) { | |
122 throw new RuntimeException('Error occurred during tell operation'); | |
123 } | |
124 | |
125 return $result; | |
126 } | |
127 | |
128 /** | |
129 * {@inheritdoc} | |
130 */ | |
131 public function eof() | |
132 { | |
133 if (! $this->resource) { | |
134 return true; | |
135 } | |
136 | |
137 return feof($this->resource); | |
138 } | |
139 | |
140 /** | |
141 * {@inheritdoc} | |
142 */ | |
143 public function isSeekable() | |
144 { | |
145 if (! $this->resource) { | |
146 return false; | |
147 } | |
148 | |
149 $meta = stream_get_meta_data($this->resource); | |
150 return $meta['seekable']; | |
151 } | |
152 | |
153 /** | |
154 * {@inheritdoc} | |
155 */ | |
156 public function seek($offset, $whence = SEEK_SET) | |
157 { | |
158 if (! $this->resource) { | |
159 throw new RuntimeException('No resource available; cannot seek position'); | |
160 } | |
161 | |
162 if (! $this->isSeekable()) { | |
163 throw new RuntimeException('Stream is not seekable'); | |
164 } | |
165 | |
166 $result = fseek($this->resource, $offset, $whence); | |
167 | |
168 if (0 !== $result) { | |
169 throw new RuntimeException('Error seeking within stream'); | |
170 } | |
171 | |
172 return true; | |
173 } | |
174 | |
175 /** | |
176 * {@inheritdoc} | |
177 */ | |
178 public function rewind() | |
179 { | |
180 return $this->seek(0); | |
181 } | |
182 | |
183 /** | |
184 * {@inheritdoc} | |
185 */ | |
186 public function isWritable() | |
187 { | |
188 if (! $this->resource) { | |
189 return false; | |
190 } | |
191 | |
192 $meta = stream_get_meta_data($this->resource); | |
193 $mode = $meta['mode']; | |
194 | |
195 return ( | |
196 strstr($mode, 'x') | |
197 || strstr($mode, 'w') | |
198 || strstr($mode, 'c') | |
199 || strstr($mode, 'a') | |
200 || strstr($mode, '+') | |
201 ); | |
202 } | |
203 | |
204 /** | |
205 * {@inheritdoc} | |
206 */ | |
207 public function write($string) | |
208 { | |
209 if (! $this->resource) { | |
210 throw new RuntimeException('No resource available; cannot write'); | |
211 } | |
212 | |
213 if (! $this->isWritable()) { | |
214 throw new RuntimeException('Stream is not writable'); | |
215 } | |
216 | |
217 $result = fwrite($this->resource, $string); | |
218 | |
219 if (false === $result) { | |
220 throw new RuntimeException('Error writing to stream'); | |
221 } | |
222 return $result; | |
223 } | |
224 | |
225 /** | |
226 * {@inheritdoc} | |
227 */ | |
228 public function isReadable() | |
229 { | |
230 if (! $this->resource) { | |
231 return false; | |
232 } | |
233 | |
234 $meta = stream_get_meta_data($this->resource); | |
235 $mode = $meta['mode']; | |
236 | |
237 return (strstr($mode, 'r') || strstr($mode, '+')); | |
238 } | |
239 | |
240 /** | |
241 * {@inheritdoc} | |
242 */ | |
243 public function read($length) | |
244 { | |
245 if (! $this->resource) { | |
246 throw new RuntimeException('No resource available; cannot read'); | |
247 } | |
248 | |
249 if (! $this->isReadable()) { | |
250 throw new RuntimeException('Stream is not readable'); | |
251 } | |
252 | |
253 $result = fread($this->resource, $length); | |
254 | |
255 if (false === $result) { | |
256 throw new RuntimeException('Error reading stream'); | |
257 } | |
258 | |
259 return $result; | |
260 } | |
261 | |
262 /** | |
263 * {@inheritdoc} | |
264 */ | |
265 public function getContents() | |
266 { | |
267 if (! $this->isReadable()) { | |
268 throw new RuntimeException('Stream is not readable'); | |
269 } | |
270 | |
271 $result = stream_get_contents($this->resource); | |
272 if (false === $result) { | |
273 throw new RuntimeException('Error reading from stream'); | |
274 } | |
275 return $result; | |
276 } | |
277 | |
278 /** | |
279 * {@inheritdoc} | |
280 */ | |
281 public function getMetadata($key = null) | |
282 { | |
283 if (null === $key) { | |
284 return stream_get_meta_data($this->resource); | |
285 } | |
286 | |
287 $metadata = stream_get_meta_data($this->resource); | |
288 if (! array_key_exists($key, $metadata)) { | |
289 return null; | |
290 } | |
291 | |
292 return $metadata[$key]; | |
293 } | |
294 | |
295 /** | |
296 * Set the internal stream resource. | |
297 * | |
298 * @param string|resource $stream String stream target or stream resource. | |
299 * @param string $mode Resource mode for stream target. | |
300 * @throws InvalidArgumentException for invalid streams or resources. | |
301 */ | |
302 private function setStream($stream, $mode = 'r') | |
303 { | |
304 $error = null; | |
305 $resource = $stream; | |
306 | |
307 if (is_string($stream)) { | |
308 set_error_handler(function ($e) use (&$error) { | |
309 $error = $e; | |
310 }, E_WARNING); | |
311 $resource = fopen($stream, $mode); | |
312 restore_error_handler(); | |
313 } | |
314 | |
315 if ($error) { | |
316 throw new InvalidArgumentException('Invalid stream reference provided'); | |
317 } | |
318 | |
319 if (! is_resource($resource) || 'stream' !== get_resource_type($resource)) { | |
320 throw new InvalidArgumentException( | |
321 'Invalid stream provided; must be a string stream identifier or stream resource' | |
322 ); | |
323 } | |
324 | |
325 if ($stream !== $resource) { | |
326 $this->stream = $stream; | |
327 } | |
328 | |
329 $this->resource = $resource; | |
330 } | |
331 } |