1 | <?php |
||
38 | class BufferStream implements Streamable |
||
39 | { |
||
40 | use IsReadable, IsWritable; |
||
41 | |||
42 | /** |
||
43 | * Buffer contents. |
||
44 | * |
||
45 | * @var string|false A string containing the buffer contents |
||
46 | */ |
||
47 | protected $buffer = ''; |
||
48 | |||
49 | /** |
||
50 | * Is stream readable? |
||
51 | * |
||
52 | * @var bool Whether stream is readable |
||
53 | */ |
||
54 | protected $readable = true; |
||
55 | |||
56 | /** |
||
57 | * Is stream writable? |
||
58 | * |
||
59 | * @var bool Whether stream is writable |
||
60 | */ |
||
61 | protected $writable = true; |
||
62 | |||
63 | /** |
||
64 | * Is stream seekable? |
||
65 | * |
||
66 | * @var bool Whether stream is seekable |
||
67 | */ |
||
68 | protected $seekable = false; |
||
69 | |||
70 | /** |
||
71 | * @var array Stream meta data |
||
72 | * hwm: "high water mark" - once buffer reaches this number (in bytes) |
||
73 | * write() operations will begin returning false defaults to 16384 bytes (16KB) |
||
74 | */ |
||
75 | protected $meta = [ |
||
76 | 'hwm' => 16384, |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * Instantiate a buffer stream. |
||
81 | * |
||
82 | * Instantiate a new buffer stream, optionally changing the high water mark |
||
83 | * from its default of 16384 bytes (16KB). Once buffer reaches high water |
||
84 | * mark, write operations will begin returning false. It's possible for buffer |
||
85 | * size to exceed this level since it is only AFTER it is reached that writes |
||
86 | * begin returning false. |
||
87 | * |
||
88 | * @param int Number (in bytes) representing buffer "high water mark" |
||
89 | * @param null|mixed $hwm |
||
90 | */ |
||
91 | 31 | public function __construct($hwm = null) |
|
97 | |||
98 | /** |
||
99 | * Read the entire stream, beginning to end. |
||
100 | * |
||
101 | * In most stream implementations, __toString() differs from getContents() |
||
102 | * in that it returns the entire stream rather than just the remainder, but |
||
103 | * due to the way this stream works (sort of like a conveyor belt), this |
||
104 | * method is an alias to getContents() |
||
105 | * |
||
106 | * @return string The entire stream, beginning to end |
||
107 | */ |
||
108 | public function __toString() |
||
112 | |||
113 | 13 | public function isEmpty() |
|
117 | |||
118 | 13 | public function isFull() |
|
122 | |||
123 | /** |
||
124 | * Readability accessor. |
||
125 | * |
||
126 | * Despite the fact that any class that implements this interface must also |
||
127 | * define methods such as read and readLine, that is no guarantee that an |
||
128 | * object will necessarily be readable. This method should tell the user |
||
129 | * whether a stream is, in fact, readable. |
||
130 | * |
||
131 | * @return bool True if readable, false otherwise |
||
132 | */ |
||
133 | 1 | public function isReadable() |
|
137 | |||
138 | /** |
||
139 | * Read in the specified amount of characters from the input source. |
||
140 | * |
||
141 | * @param int $chars Amount of characters to read from input source |
||
142 | * |
||
143 | * @return string|bool The specified amount of characters read from input source |
||
144 | */ |
||
145 | 19 | public function read($chars) |
|
149 | |||
150 | /** |
||
151 | * Read a chunk of buffer data. |
||
152 | * |
||
153 | * Removes a specific chunk of data from the buffer and return it. |
||
154 | * |
||
155 | * @param int|null $start |
||
156 | * @param int|null $length |
||
157 | * |
||
158 | * @return string The chunk of data read from the buffer |
||
159 | */ |
||
160 | 20 | public function readChunk($start = null, $length = null) |
|
172 | |||
173 | /** |
||
174 | * Read the remainder of the stream. |
||
175 | * |
||
176 | * @return string The remainder of the stream |
||
177 | */ |
||
178 | 3 | public function getContents() |
|
185 | |||
186 | /** |
||
187 | * Return the size (in bytes) of this readable (if known). |
||
188 | * |
||
189 | * @return int|null Size (in bytes) of this readable |
||
190 | */ |
||
191 | 22 | public function getSize() |
|
195 | |||
196 | /** |
||
197 | * Return the current position within the stream/readable. |
||
198 | * |
||
199 | * @return int|false The current position within readable |
||
200 | */ |
||
201 | 2 | public function tell() |
|
205 | |||
206 | /** |
||
207 | * Determine whether the end of the readable resource has been reached. |
||
208 | * |
||
209 | * @return bool Whether we're at the end of the readable |
||
210 | */ |
||
211 | 7 | public function eof() |
|
215 | |||
216 | /** |
||
217 | * File must be able to be rewound when the end is reached. |
||
218 | */ |
||
219 | 4 | public function rewind() |
|
223 | |||
224 | /** |
||
225 | * Get stream metadata as an associative array or retrieve a specific key. |
||
226 | * |
||
227 | * The keys returned are identical to the keys returned from PHP's |
||
228 | * stream_get_meta_data() function. |
||
229 | * |
||
230 | * @param string $key Specific metadata to retrieve. |
||
231 | * |
||
232 | * @return array|mixed|null Returns an associative array if no key is |
||
233 | * provided. Returns a specific key value if a key is provided and the |
||
234 | * value is found, or null if the key is not found. |
||
235 | * |
||
236 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
237 | */ |
||
238 | 23 | public function getMetadata($key = null) |
|
246 | |||
247 | /** |
||
248 | * Closes the stream and any underlying resources. |
||
249 | * |
||
250 | * @return true |
||
251 | */ |
||
252 | 2 | public function close() |
|
258 | |||
259 | /** |
||
260 | * Separates any underlying resources from the stream. |
||
261 | * |
||
262 | * After the stream has been detached, the stream is in an unusable state. |
||
263 | * |
||
264 | * @return BufferStream|null Underlying PHP stream, if any |
||
265 | */ |
||
266 | 1 | public function detach() |
|
273 | |||
274 | /** |
||
275 | * Writability accessor. |
||
276 | * |
||
277 | * Despite the fact that any class that implements this interface must also |
||
278 | * define methods such as write and writeLine, that is no guarantee that an |
||
279 | * object will necessarily be writable. This method should tell the user |
||
280 | * whether a stream is, in fact, writable. |
||
281 | * |
||
282 | * @return bool True if writable, false otherwise |
||
283 | */ |
||
284 | 1 | public function isWritable() |
|
288 | |||
289 | /** |
||
290 | * Write data to the output. |
||
291 | * |
||
292 | * @param string $data The data to write |
||
293 | * |
||
294 | * @return false|int The number of bytes written |
||
295 | */ |
||
296 | 22 | public function write($data) |
|
305 | |||
306 | /** |
||
307 | * Seekability accessor. |
||
308 | * |
||
309 | * Despite the fact that any class that implements this interface must also |
||
310 | * define methods such as seek, that is no guarantee that an |
||
311 | * object will necessarily be seekable. This method should tell the user |
||
312 | * whether a stream is, in fact, seekable. |
||
313 | * |
||
314 | * @return bool True if seekable, false otherwise |
||
315 | */ |
||
316 | 1 | public function isSeekable() |
|
320 | |||
321 | /** |
||
322 | * Seek to specified offset. |
||
323 | * |
||
324 | * @param int $offset Offset to seek to |
||
325 | * @param int $whence Position from whence the offset should be applied |
||
326 | * |
||
327 | * @return bool True if seek was successful |
||
328 | */ |
||
329 | 1 | public function seek($offset, $whence = SEEK_SET) |
|
333 | } |
||
334 |