1 | <?php |
||
42 | class Stream implements Readable, Writable, Seekable |
||
43 | { |
||
44 | use IsReadable, IsWritable, IsSeekable; |
||
45 | /** |
||
46 | * Hash of readable/writable stream open mode types. |
||
47 | * |
||
48 | * Mercilessly stolen from: |
||
49 | * https://github.com/guzzle/streams/blob/master/src/Stream.php |
||
50 | * |
||
51 | * My kudos and sincere thanks go out to Michael Dowling and Graham Campbell |
||
52 | * of the guzzle/streams PHP package. Thanks for the inspiration (in some cases) |
||
53 | * and the not suing me for outright theft (in this case). |
||
54 | * |
||
55 | * @var array Hash of readable and writable stream types |
||
56 | */ |
||
57 | protected static $readWriteHash = [ |
||
58 | 'read' => [ |
||
59 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
||
60 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
||
61 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
||
62 | 'x+t' => true, 'c+t' => true, 'a+' => true, |
||
63 | ], |
||
64 | 'write' => [ |
||
65 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
||
66 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
||
67 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
||
68 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
||
69 | ], |
||
70 | ]; |
||
71 | |||
72 | /** |
||
73 | * @var resource An open stream resource |
||
74 | */ |
||
75 | protected $stream; |
||
76 | |||
77 | /** |
||
78 | * Meta data about stream resource. |
||
79 | * Just contains the return value of stream_get_meta_data. |
||
80 | * @var array The return value of stream_get_meta_data |
||
81 | */ |
||
82 | protected $meta; |
||
83 | |||
84 | /** |
||
85 | * Is stream seekable |
||
86 | * @var boolean True if stream is seekable, false otherwise |
||
87 | */ |
||
88 | protected $seekable; |
||
89 | |||
90 | /** |
||
91 | * Is stream readable |
||
92 | * @var boolean True if stream is readable, false otherwise |
||
93 | */ |
||
94 | protected $readable; |
||
95 | |||
96 | /** |
||
97 | * Is stream writable |
||
98 | * @var boolean True if stream is writable, false otherwise |
||
99 | */ |
||
100 | protected $writable; |
||
101 | |||
102 | /** |
||
103 | * Converts object/string to a usable stream |
||
104 | * |
||
105 | * Mercilessly stolen from: |
||
106 | * https://github.com/guzzle/streams/blob/master/src/Stream.php |
||
107 | * |
||
108 | * My kudos and sincere thanks go out to Michael Dowling and Graham Campbell |
||
109 | * of the guzzle/streams PHP package. Thanks for the inspiration (in some cases) |
||
110 | * and the not suing me for outright theft (in this case). |
||
111 | * |
||
112 | * @param object|string The string/object to convert to a stream |
||
113 | * @param array Options to pass to the newly created stream |
||
114 | * @return \CSVelte\IO\Stream |
||
115 | * @throws \InvalidArgumentException |
||
116 | */ |
||
117 | 16 | public static function streamize($resource = '') |
|
118 | { |
||
119 | 16 | $type = gettype($resource); |
|
120 | |||
121 | 16 | if ($type == 'string' && file_exists($resource)) { |
|
122 | 2 | return new self($resource); |
|
123 | } |
||
124 | |||
125 | 15 | if ($type == 'string') { |
|
126 | 15 | $stream = self::open('php://temp', 'r+'); |
|
127 | 15 | if ($resource !== '') { |
|
128 | 13 | fwrite($stream, $resource); |
|
129 | 13 | fseek($stream, 0); |
|
130 | 13 | } |
|
131 | 15 | return new self($stream); |
|
132 | } |
||
133 | |||
134 | 1 | if ($type == 'object' && method_exists($resource, '__toString')) { |
|
135 | 1 | return self::streamize((string) $resource); |
|
136 | } |
||
137 | |||
138 | throw new InvalidArgumentException('Invalid resource type: ' . $type); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Stream Object Constructor. |
||
143 | * |
||
144 | * Instantiates the stream object |
||
145 | * |
||
146 | * @param string|resource $stream Either a valid stream URI or an open |
||
147 | * stream resource (using fopen, fsockopen, et al.) |
||
148 | * @param string file/stream open mode as passed to native php |
||
149 | * ``fopen`` function |
||
150 | * @param array Stream context options array as passed to native php |
||
151 | * ``stream_context_create`` function |
||
152 | * @see http://php.net/manual/en/function.fopen.php |
||
153 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
154 | */ |
||
155 | 50 | public function __construct($stream, $mode = null, $context = null) |
|
159 | |||
160 | /** |
||
161 | * Stream Object Destructor. |
||
162 | * |
||
163 | * Closes stream connection. |
||
164 | */ |
||
165 | 45 | public function __destruct() |
|
169 | |||
170 | /** |
||
171 | * Open a new stream URI and return stream resource. |
||
172 | * |
||
173 | * Pass in either a valid stream URI or a stream resource and this will |
||
174 | * return a stream resource object. |
||
175 | * |
||
176 | * @param string|resource $stream Either stream URI or resource object |
||
177 | * @param string $mode File/stream open mode (as passed to native php |
||
178 | * ``fopen`` function) |
||
179 | * @param array $context Stream context options array as passed to native |
||
180 | * php ``stream_context_create`` function |
||
181 | * @return stream resource object |
||
182 | * @throws CSVelte\Exception\IOException on invalid stream uri/resource |
||
183 | * @throws \InvalidArgumentException if context param is not an array |
||
184 | * @see http://php.net/manual/en/function.fopen.php |
||
185 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
186 | */ |
||
187 | 48 | protected static function open($stream, $mode = null, $context = null) |
|
209 | |||
210 | /** |
||
211 | * Close stream resource. |
||
212 | * |
||
213 | * @return boolean True on success or false on failure |
||
214 | */ |
||
215 | 45 | public function close() |
|
222 | |||
223 | /** |
||
224 | * Set stream meta data via stream resource. |
||
225 | * |
||
226 | * Pass in stream resource to set this object's stream metadata as returned |
||
227 | * by the native php function ``stream_get_meta_data`` |
||
228 | * |
||
229 | * @param resource $stream Stream resource object |
||
230 | * @return $this |
||
231 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
232 | */ |
||
233 | 45 | protected function setMetaData($stream) |
|
241 | |||
242 | /** |
||
243 | * Get stream metadata (all or certain value). |
||
244 | * |
||
245 | * Get either the entire stream metadata array or a single value from it by key. |
||
246 | * |
||
247 | * @param string $key If set, must be one of ``stream_get_meta_data`` array keys |
||
248 | * @return string|array Either a single value or whole array returned by ``stream_get_meta_data`` |
||
249 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
250 | */ |
||
251 | 9 | public function getMetaData($key = null) |
|
256 | |||
257 | /** |
||
258 | * Accessor for seekability. |
||
259 | * |
||
260 | * Returns true if possible to seek to a certain position within this stream |
||
261 | * |
||
262 | * @return boolean True if stream is seekable |
||
263 | */ |
||
264 | 2 | public function isSeekable() |
|
268 | |||
269 | /** |
||
270 | * Accessor for readability. |
||
271 | * |
||
272 | * Returns true if possible to read from this stream |
||
273 | * |
||
274 | * @return boolean True if stream is readable |
||
275 | */ |
||
276 | 31 | public function isReadable() |
|
280 | |||
281 | /** |
||
282 | * Accessor for writability. |
||
283 | * |
||
284 | * Returns true if possible to write to this stream |
||
285 | * |
||
286 | * @return boolean True if stream is writable |
||
287 | */ |
||
288 | 16 | public function isWritable() |
|
292 | |||
293 | /** |
||
294 | * Accessor for internal stream resource. |
||
295 | * |
||
296 | * Returns the internal stream resource pointer |
||
297 | * |
||
298 | * @return resource The open stream resource pointer |
||
299 | */ |
||
300 | 3 | public function getResource() |
|
304 | |||
305 | /** |
||
306 | * Accessor for stream URI. |
||
307 | * |
||
308 | * Returns the stream URI |
||
309 | * |
||
310 | * @return string The URI for the stream |
||
311 | */ |
||
312 | 8 | public function getUri() |
|
316 | |||
317 | /** |
||
318 | * Accessor for stream name. |
||
319 | * |
||
320 | * Alias for ``getUri()`` |
||
321 | * |
||
322 | * @return string The name for this stream |
||
323 | */ |
||
324 | 7 | public function getName() |
|
328 | |||
329 | /** |
||
330 | * Read $length bytes from stream. |
||
331 | * |
||
332 | * Reads $length bytes (number of characters) from the stream |
||
333 | * |
||
334 | * @param int $length Number of bytes to read from stream |
||
335 | * @return string|boolean The data read from stream or false if at end of |
||
336 | * file or some other problem. |
||
337 | * @throws CSVelte\Exception\IOException |
||
338 | */ |
||
339 | 30 | public function read($length) |
|
345 | |||
346 | /** |
||
347 | * Read the entire contents of file/stream. |
||
348 | * |
||
349 | * Read and return the entire contents of the stream. |
||
350 | * |
||
351 | * @param void |
||
352 | * @return string The entire file contents |
||
353 | * @throws CSVelte\Exception\IOException |
||
354 | */ |
||
355 | 1 | public function getContents() |
|
363 | |||
364 | /** |
||
365 | * Is file pointer at the end of the stream? |
||
366 | * |
||
367 | * Returns true if internal pointer has reached the end of the stream. |
||
368 | * |
||
369 | * @return boolean True if end of stream has been reached |
||
370 | */ |
||
371 | 29 | public function eof() |
|
375 | |||
376 | /** |
||
377 | * Rewind pointer to beginning of stream. |
||
378 | * |
||
379 | * Rewinds the stream, meaning it returns the pointer to the beginning of the |
||
380 | * stream as if it had just been initialized. |
||
381 | * |
||
382 | * @return boolean True on success |
||
383 | */ |
||
384 | 19 | public function rewind() |
|
388 | |||
389 | /** |
||
390 | * Write to stream |
||
391 | * |
||
392 | * Writes a string to the stream (if it is writable) |
||
393 | * |
||
394 | * @param string $str The data to be written to the stream |
||
395 | * @return int The number of bytes written to the stream |
||
396 | * @throws CSVelte\Exception\IOException |
||
397 | */ |
||
398 | 16 | public function write($str) |
|
403 | |||
404 | /** |
||
405 | * Seek to position. |
||
406 | * |
||
407 | * Seek to a specific position within the stream (if seekable). |
||
408 | * |
||
409 | * @param int $offset The position to seek to |
||
410 | * @param int $whence One of three native php ``SEEK_`` constants |
||
411 | * @return boolean True on success false on failure |
||
412 | * @throws CSVelte\Exception\IOException |
||
413 | * @see http://php.net/manual/en/function.seek.php |
||
414 | */ |
||
415 | 2 | public function seek($offset, $whence = SEEK_SET) |
|
420 | |||
421 | } |
||
422 |