1 | <?php |
||
20 | class Stream implements StreamInterface |
||
21 | { |
||
22 | /** |
||
23 | * Underline stream |
||
24 | * |
||
25 | * @var Resource|null |
||
26 | */ |
||
27 | private $stream = null; |
||
28 | |||
29 | /** |
||
30 | * Size of file |
||
31 | * |
||
32 | * @var int|null |
||
33 | */ |
||
34 | private $size = null; |
||
35 | |||
36 | /** |
||
37 | * Metadata of file |
||
38 | * |
||
39 | * @var array|null |
||
40 | */ |
||
41 | private $meta = null; |
||
42 | |||
43 | /** |
||
44 | * Resource modes |
||
45 | * |
||
46 | * @var array |
||
47 | * @link http://php.net/manual/function.fopen.php |
||
48 | */ |
||
49 | private $modes = [ |
||
50 | 'readable' => ['r', 'r+', 'w+', 'a+', 'x+', 'c+', 'w+b', 'rb', 'r+b'], |
||
51 | 'writable' => ['r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+', 'w+b'], |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param Resource $stream Underline resource stream |
||
58 | */ |
||
59 | 56 | public function __construct($stream) |
|
63 | |||
64 | /** |
||
65 | * Closes the stream and any underlying resources. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | 1 | public function close() |
|
77 | |||
78 | /** |
||
79 | * Separates any underlying resources from the stream. |
||
80 | * |
||
81 | * After the stream has been detached, the stream is in an unusable state. |
||
82 | * |
||
83 | * @return resource|null Underlying PHP stream, if any |
||
84 | */ |
||
85 | 7 | public function detach() |
|
95 | |||
96 | /** |
||
97 | * Get the size of the stream if known. |
||
98 | * |
||
99 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
100 | */ |
||
101 | 1 | public function getSize() |
|
110 | |||
111 | /** |
||
112 | * Returns the current position of the file read/write pointer |
||
113 | * |
||
114 | * @return int Position of the file pointer |
||
115 | * |
||
116 | * @throws \RuntimeException on error. |
||
117 | */ |
||
118 | 2 | public function tell() |
|
126 | |||
127 | /** |
||
128 | * Returns true if the stream is at the end of the stream. |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | 2 | public function eof() |
|
136 | |||
137 | /** |
||
138 | * Returns whether or not the stream is seekable. |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | 8 | public function isSeekable() |
|
146 | |||
147 | /** |
||
148 | * Seek to a position in the stream. |
||
149 | * |
||
150 | * @link http://www.php.net/manual/en/function.fseek.php |
||
151 | * |
||
152 | * @param int $offset Stream offset |
||
153 | * @param int $whence Specifies how the cursor position will be calculated |
||
154 | * based on the seek offset. Valid values are identical to the built-in |
||
155 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
156 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
157 | * SEEK_END: Set position to end-of-stream plus offset. |
||
158 | * |
||
159 | * @throws \RuntimeException on failure. |
||
160 | */ |
||
161 | 8 | public function seek($offset, $whence = SEEK_SET) |
|
167 | |||
168 | /** |
||
169 | * Seek to the beginning of the stream. |
||
170 | * |
||
171 | * If the stream is not seekable, this method will raise an exception; |
||
172 | * otherwise, it will perform a seek(0). |
||
173 | * |
||
174 | * @see seek() |
||
175 | * @link http://www.php.net/manual/en/function.fseek.php |
||
176 | * |
||
177 | * @throws \RuntimeException on failure. |
||
178 | */ |
||
179 | 5 | public function rewind() |
|
183 | |||
184 | /** |
||
185 | * Returns whether or not the stream is writable. |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 4 | public function isWritable() |
|
194 | |||
195 | /** |
||
196 | * Write data to the stream. |
||
197 | * |
||
198 | * @param string $string The string that is to be written. |
||
199 | * |
||
200 | * @return int Returns the number of bytes written to the stream. |
||
201 | * |
||
202 | * @throws \RuntimeException on failure. |
||
203 | */ |
||
204 | 4 | public function write($string) |
|
215 | |||
216 | /** |
||
217 | * Returns whether or not the stream is readable. |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 10 | public function isReadable() |
|
226 | |||
227 | /** |
||
228 | * Read data from the stream. |
||
229 | * |
||
230 | * @param int $length Read up to $length bytes from the object and return |
||
231 | * them. Fewer than $length bytes may be returned if underlying stream |
||
232 | * call returns fewer bytes. |
||
233 | * |
||
234 | * @return string Returns the data read from the stream, or an empty string |
||
235 | * if no bytes are available. |
||
236 | * |
||
237 | * @throws \RuntimeException if an error occurs. |
||
238 | */ |
||
239 | 2 | public function read($length) |
|
247 | |||
248 | /** |
||
249 | * Returns the remaining contents in a string |
||
250 | * |
||
251 | * @return string |
||
252 | * |
||
253 | * @throws \RuntimeException if unable to read or an error occurs while reading. |
||
254 | */ |
||
255 | 8 | public function getContents() |
|
263 | |||
264 | /** |
||
265 | * Get stream metadata as an associative array or retrieve a specific key. |
||
266 | * |
||
267 | * The keys returned are identical to the keys returned from PHP's |
||
268 | * stream_get_meta_data() function. |
||
269 | * |
||
270 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
271 | * |
||
272 | * @param string $key Specific metadata to retrieve. |
||
273 | * |
||
274 | * @return array|mixed|null Returns an associative array if no key is provided. |
||
275 | * Returns a specific key value if a key is provided and the |
||
276 | * value is found, or null if the key is not found. |
||
277 | */ |
||
278 | 17 | public function getMetadata($key = null) |
|
289 | |||
290 | /** |
||
291 | * Reads all data from the stream into a string, from the beginning to end. |
||
292 | * |
||
293 | * This method attempt to seek to the beginning of the stream before |
||
294 | * reading data and read the stream until the end is reached. |
||
295 | * |
||
296 | * Warning: This could attempt to load a large amount of data into memory. |
||
297 | * |
||
298 | * This method does not raise an exception in order to conform with PHP's |
||
299 | * string casting operations. |
||
300 | * |
||
301 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 4 | public function __toString() |
|
318 | } |
||
319 |