1 | <?php |
||
36 | class IteratorStream implements Streamable |
||
37 | { |
||
38 | use IsReadable, IsWritable; |
||
39 | |||
40 | /** |
||
41 | * Buffer stream. |
||
42 | * |
||
43 | * @var BufferStream A BufferStream object |
||
44 | */ |
||
45 | protected $buffer; |
||
46 | |||
47 | /** |
||
48 | * Iterator to read from. |
||
49 | * |
||
50 | * @var Iterator |
||
51 | */ |
||
52 | protected $iter; |
||
53 | |||
54 | /** |
||
55 | * Is stream readable? |
||
56 | * |
||
57 | * @var bool Whether stream is readable |
||
58 | */ |
||
59 | protected $readable = true; |
||
60 | |||
61 | /** |
||
62 | * Is stream writable? |
||
63 | * |
||
64 | * @var bool Whether stream is writable |
||
65 | */ |
||
66 | protected $writable = false; |
||
67 | |||
68 | /** |
||
69 | * Is stream seekable? |
||
70 | * |
||
71 | * @var bool Whether stream is seekable |
||
72 | */ |
||
73 | protected $seekable = false; |
||
74 | |||
75 | /** |
||
76 | * @var array Any additional options / meta data |
||
77 | */ |
||
78 | protected $meta = [ |
||
79 | |||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Instantiate an iterator stream. |
||
84 | * |
||
85 | * Instantiate a new iterator stream. The iterator is used to continually |
||
86 | * refill a buffer as it is drained by read operations. |
||
87 | * |
||
88 | * @param \Iterator The iterator to stream data from |
||
89 | * @param BufferStream|null Either a buffer or null (to use |
||
90 | * default buffer) |
||
91 | * @param null|mixed $buffer |
||
92 | * |
||
93 | * @todo this should expect a BufferInterface or a Bufferable rather than |
||
94 | * a BufferStream |
||
95 | */ |
||
96 | 21 | public function __construct(Iterator $iter, $buffer = null) |
|
112 | |||
113 | /** |
||
114 | * Read the entire stream, beginning to end. |
||
115 | * |
||
116 | * In most stream implementations, __toString() differs from getContents() |
||
117 | * in that it returns the entire stream rather than just the remainder, but |
||
118 | * due to the way this stream works (sort of like a conveyor belt), this |
||
119 | * method is an alias to getContents() |
||
120 | * |
||
121 | * @return string The entire stream, beginning to end |
||
122 | */ |
||
123 | 4 | public function __toString() |
|
129 | |||
130 | /** |
||
131 | * Readability accessor. |
||
132 | * |
||
133 | * Despite the fact that any class that implements this interface must also |
||
134 | * define methods such as read and readLine, that is no guarantee that an |
||
135 | * object will necessarily be readable. This method should tell the user |
||
136 | * whether a stream is, in fact, readable. |
||
137 | * |
||
138 | * @return bool True if readable, false otherwise |
||
139 | */ |
||
140 | 2 | public function isReadable() |
|
144 | |||
145 | 13 | public function read($bytes) |
|
165 | |||
166 | /** |
||
167 | * Read the remainder of the stream. |
||
168 | * |
||
169 | * @return string The remainder of the stream |
||
170 | */ |
||
171 | 5 | public function getContents() |
|
184 | |||
185 | /** |
||
186 | * Return the size (in bytes) of this stream (if known). |
||
187 | * |
||
188 | * @return int|null Size (in bytes) of this stream |
||
189 | */ |
||
190 | 1 | public function getSize() |
|
194 | |||
195 | /** |
||
196 | * Return the current position within the stream/readable. |
||
197 | * |
||
198 | * I can't decide whether there is any meaningful way to "tell" the |
||
199 | * current position within this type of stream. For now I'm just |
||
200 | * going to return false because the nature of this type of stream |
||
201 | * means that it technically has no definitive beginning or end and |
||
202 | * therefor no absolute position. If a "tell" is truly needed, I |
||
203 | * suppose I could keep track of how many bytes have been read over |
||
204 | * the lifetime of the object, but I don't think that is meaningful |
||
205 | * and/or useful. |
||
206 | * |
||
207 | * @return int|false The current position within readable |
||
208 | */ |
||
209 | 1 | public function tell() |
|
213 | |||
214 | /** |
||
215 | * Determine whether the end of the stream has been reached. |
||
216 | * |
||
217 | * @return bool Whether we're at the end of the stream |
||
218 | */ |
||
219 | 5 | public function eof() |
|
225 | |||
226 | /** |
||
227 | * Rewind to beginning of stream. |
||
228 | */ |
||
229 | 4 | public function rewind() |
|
234 | |||
235 | /** |
||
236 | * Get stream metadata as an associative array or retrieve a specific key. |
||
237 | * |
||
238 | * The keys returned are identical to the keys returned from PHP's |
||
239 | * stream_get_meta_data() function. |
||
240 | * |
||
241 | * @param string $key Specific metadata to retrieve. |
||
242 | * |
||
243 | * @return array|mixed|null Returns an associative array if no key is |
||
244 | * provided. Returns a specific key value if a key is provided and the |
||
245 | * value is found, or null if the key is not found. |
||
246 | * |
||
247 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
248 | */ |
||
249 | 2 | public function getMetadata($key = null) |
|
257 | |||
258 | /** |
||
259 | * Closes the stream and any underlying resources. |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | 1 | public function close() |
|
274 | |||
275 | /** |
||
276 | * Separates any underlying resources from the stream. |
||
277 | * |
||
278 | * After the stream has been detached, the stream is in an unusable state. |
||
279 | * Because there is no underlying stream resource, the actual data in the |
||
280 | * buffer is returned instead. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | 1 | public function detach() |
|
293 | |||
294 | /** |
||
295 | * Writability accessor. |
||
296 | * |
||
297 | * Despite the fact that any class that implements this interface must also |
||
298 | * define methods such as write and writeLine, that is no guarantee that an |
||
299 | * object will necessarily be writable. This method should tell the user |
||
300 | * whether a stream is, in fact, writable. |
||
301 | * |
||
302 | * @return bool True if writable, false otherwise |
||
303 | */ |
||
304 | 1 | public function isWritable() |
|
308 | |||
309 | /** |
||
310 | * Write data to the output. |
||
311 | * |
||
312 | * @param string $data The data to write |
||
313 | * |
||
314 | * @return int|false The number of bytes written |
||
315 | */ |
||
316 | 2 | public function write($data) |
|
320 | |||
321 | /** |
||
322 | * Seekability accessor. |
||
323 | * |
||
324 | * Despite the fact that any class that implements this interface must also |
||
325 | * define methods such as seek, that is no guarantee that an |
||
326 | * object will necessarily be seekable. This method should tell the user |
||
327 | * whether a stream is, in fact, seekable. |
||
328 | * |
||
329 | * @return bool True if seekable, false otherwise |
||
330 | */ |
||
331 | 2 | public function isSeekable() |
|
335 | |||
336 | /** |
||
337 | * Seek to specified offset. |
||
338 | * |
||
339 | * @param int $offset Offset to seek to |
||
340 | * @param int $whence Position from whence the offset should be applied |
||
341 | * |
||
342 | * @return bool True if seek was successful |
||
343 | */ |
||
344 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
348 | |||
349 | /** |
||
350 | * Inflate the buffer. |
||
351 | * |
||
352 | * Loop through the iterator and fill the buffer with its contents. |
||
353 | */ |
||
354 | 12 | protected function inflateBuffer() |
|
362 | } |
||
363 |