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 | 15 | public static function streamize($resource = '') |
|
118 | { |
||
119 | 15 | $type = gettype($resource); |
|
120 | |||
121 | 15 | if ($type == 'string') { |
|
122 | 15 | $stream = self::open('php://temp', 'r+'); |
|
123 | 15 | if ($resource !== '') { |
|
124 | 13 | fwrite($stream, $resource); |
|
125 | 13 | fseek($stream, 0); |
|
126 | 13 | } |
|
127 | 15 | return new self($stream); |
|
128 | } |
||
129 | |||
130 | 1 | if ($type == 'object' && method_exists($resource, '__toString')) { |
|
131 | 1 | return self::streamize((string) $resource); |
|
132 | } |
||
133 | |||
134 | throw new InvalidArgumentException('Invalid resource type: ' . $type); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Stream Object Constructor. |
||
139 | * |
||
140 | * Instantiates the stream object |
||
141 | * |
||
142 | * @param string|resource $stream Either a valid stream URI or an open |
||
143 | * stream resource (using fopen, fsockopen, et al.) |
||
144 | * @param string file/stream open mode as passed to native php |
||
145 | * ``fopen`` function |
||
146 | * @param array Stream context options array as passed to native php |
||
147 | * ``stream_context_create`` function |
||
148 | * @see http://php.net/manual/en/function.fopen.php |
||
149 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
150 | */ |
||
151 | 50 | public function __construct($stream, $mode = null, $context = null) |
|
155 | |||
156 | /** |
||
157 | * Stream Object Destructor. |
||
158 | * |
||
159 | * Closes stream connection. |
||
160 | */ |
||
161 | 45 | public function __destruct() |
|
165 | |||
166 | /** |
||
167 | * Open a new stream URI and return stream resource. |
||
168 | * |
||
169 | * Pass in either a valid stream URI or a stream resource and this will |
||
170 | * return a stream resource object. |
||
171 | * |
||
172 | * @param string|resource $stream Either stream URI or resource object |
||
173 | * @param string $mode File/stream open mode (as passed to native php |
||
174 | * ``fopen`` function) |
||
175 | * @param array $context Stream context options array as passed to native |
||
176 | * php ``stream_context_create`` function |
||
177 | * @return stream resource object |
||
178 | * @throws CSVelte\Exception\IOException on invalid stream uri/resource |
||
179 | * @throws \InvalidArgumentException if context param is not an array |
||
180 | * @see http://php.net/manual/en/function.fopen.php |
||
181 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
182 | */ |
||
183 | 48 | protected static function open($stream, $mode = null, $context = null) |
|
205 | |||
206 | /** |
||
207 | * Close stream resource. |
||
208 | * |
||
209 | * @return boolean True on success or false on failure |
||
210 | */ |
||
211 | 45 | public function close() |
|
218 | |||
219 | /** |
||
220 | * Set stream meta data via stream resource. |
||
221 | * |
||
222 | * Pass in stream resource to set this object's stream metadata as returned |
||
223 | * by the native php function ``stream_get_meta_data`` |
||
224 | * |
||
225 | * @param resource $stream Stream resource object |
||
226 | * @return $this |
||
227 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
228 | */ |
||
229 | 45 | protected function setMetaData($stream) |
|
237 | |||
238 | /** |
||
239 | * Get stream metadata (all or certain value). |
||
240 | * |
||
241 | * Get either the entire stream metadata array or a single value from it by key. |
||
242 | * |
||
243 | * @param string $key If set, must be one of ``stream_get_meta_data`` array keys |
||
244 | * @return string|array Either a single value or whole array returned by ``stream_get_meta_data`` |
||
245 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
246 | */ |
||
247 | 9 | public function getMetaData($key = null) |
|
252 | |||
253 | /** |
||
254 | * Accessor for seekability. |
||
255 | * |
||
256 | * Returns true if possible to seek to a certain position within this stream |
||
257 | * |
||
258 | * @return boolean True if stream is seekable |
||
259 | */ |
||
260 | 2 | public function isSeekable() |
|
264 | |||
265 | /** |
||
266 | * Accessor for readability. |
||
267 | * |
||
268 | * Returns true if possible to read from this stream |
||
269 | * |
||
270 | * @return boolean True if stream is readable |
||
271 | */ |
||
272 | 31 | public function isReadable() |
|
276 | |||
277 | /** |
||
278 | * Accessor for writability. |
||
279 | * |
||
280 | * Returns true if possible to write to this stream |
||
281 | * |
||
282 | * @return boolean True if stream is writable |
||
283 | */ |
||
284 | 16 | public function isWritable() |
|
288 | |||
289 | /** |
||
290 | * Accessor for internal stream resource. |
||
291 | * |
||
292 | * Returns the internal stream resource pointer |
||
293 | * |
||
294 | * @return resource The open stream resource pointer |
||
295 | */ |
||
296 | 3 | public function getResource() |
|
300 | |||
301 | /** |
||
302 | * Accessor for stream URI. |
||
303 | * |
||
304 | * Returns the stream URI |
||
305 | * |
||
306 | * @return string The URI for the stream |
||
307 | */ |
||
308 | 8 | public function getUri() |
|
312 | |||
313 | /** |
||
314 | * Accessor for stream name. |
||
315 | * |
||
316 | * Alias for ``getUri()`` |
||
317 | * |
||
318 | * @return string The name for this stream |
||
319 | */ |
||
320 | 7 | public function getName() |
|
324 | |||
325 | /** |
||
326 | * Read $length bytes from stream. |
||
327 | * |
||
328 | * Reads $length bytes (number of characters) from the stream |
||
329 | * |
||
330 | * @param int $length Number of bytes to read from stream |
||
331 | * @return string|boolean The data read from stream or false if at end of |
||
332 | * file or some other problem. |
||
333 | * @throws CSVelte\Exception\IOException |
||
334 | */ |
||
335 | 30 | public function read($length) |
|
341 | |||
342 | /** |
||
343 | * Read the entire contents of file/stream. |
||
344 | * |
||
345 | * Read and return the entire contents of the stream. |
||
346 | * |
||
347 | * @param void |
||
348 | * @return string The entire file contents |
||
349 | * @throws CSVelte\Exception\IOException |
||
350 | */ |
||
351 | 1 | public function getContents() |
|
359 | |||
360 | /** |
||
361 | * Is file pointer at the end of the stream? |
||
362 | * |
||
363 | * Returns true if internal pointer has reached the end of the stream. |
||
364 | * |
||
365 | * @return boolean True if end of stream has been reached |
||
366 | */ |
||
367 | 29 | public function eof() |
|
371 | |||
372 | /** |
||
373 | * Rewind pointer to beginning of stream. |
||
374 | * |
||
375 | * Rewinds the stream, meaning it returns the pointer to the beginning of the |
||
376 | * stream as if it had just been initialized. |
||
377 | * |
||
378 | * @return boolean True on success |
||
379 | */ |
||
380 | 19 | public function rewind() |
|
384 | |||
385 | /** |
||
386 | * Write to stream |
||
387 | * |
||
388 | * Writes a string to the stream (if it is writable) |
||
389 | * |
||
390 | * @param string $str The data to be written to the stream |
||
391 | * @return int The number of bytes written to the stream |
||
392 | * @throws CSVelte\Exception\IOException |
||
393 | */ |
||
394 | 16 | public function write($str) |
|
399 | |||
400 | /** |
||
401 | * Seek to position. |
||
402 | * |
||
403 | * Seek to a specific position within the stream (if seekable). |
||
404 | * |
||
405 | * @param int $offset The position to seek to |
||
406 | * @param int $whence One of three native php ``SEEK_`` constants |
||
407 | * @return boolean True on success false on failure |
||
408 | * @throws CSVelte\Exception\IOException |
||
409 | * @see http://php.net/manual/en/function.seek.php |
||
410 | */ |
||
411 | 2 | public function seek($offset, $whence = SEEK_SET) |
|
416 | |||
417 | } |
||
418 |