mirror of
https://codeberg.org/icewind/streams.git
synced 2026-06-03 16:44:07 +02:00
add readme
This commit is contained in:
parent
e1a270f184
commit
eeebf10462
1 changed files with 55 additions and 0 deletions
55
README.md
Normal file
55
README.md
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
#Streams#
|
||||||
|
|
||||||
|
[](https://travis-ci.org/icewind1991/Streams)
|
||||||
|
[](https://coveralls.io/r/icewind1991/Streams?branch=master)
|
||||||
|
|
||||||
|
Generic stream wrappers for php.
|
||||||
|
|
||||||
|
##CallBackWrapper##
|
||||||
|
|
||||||
|
A `CallBackWrapper` can be used to register callbacks on read, write and closing of the stream,
|
||||||
|
it wraps an existing stream and can thus be used for any stream in php
|
||||||
|
|
||||||
|
The callbacks are passed in the stream context along with the source stream
|
||||||
|
and can be any valid [php callable](http://php.net/manual/en/language.types.callable.php)
|
||||||
|
|
||||||
|
###Example###
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use \Icewind\Streams\CallBackWrapper;
|
||||||
|
|
||||||
|
require('vendor/autoload.php');
|
||||||
|
|
||||||
|
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
|
||||||
|
|
||||||
|
// get an existing stream to wrap
|
||||||
|
$source = fopen('php://temp', 'r+');
|
||||||
|
|
||||||
|
$context = stream_context_create(array(
|
||||||
|
'callback' => array(
|
||||||
|
'source' => $source,
|
||||||
|
'read' => function ($count) {
|
||||||
|
echo "read " . $count . "bytes\n";
|
||||||
|
},
|
||||||
|
'write' => function ($data) {
|
||||||
|
echo "wrote '" . $data . "'\n";
|
||||||
|
},
|
||||||
|
'close' => function () {
|
||||||
|
echo "stream closed\n";
|
||||||
|
}
|
||||||
|
)
|
||||||
|
));
|
||||||
|
$stream = fopen('callback://', 'r+', false, $context);
|
||||||
|
|
||||||
|
fwrite($stream, 'some dummy data');
|
||||||
|
|
||||||
|
rewind($stream);
|
||||||
|
fread($stream, 5);
|
||||||
|
|
||||||
|
fclose($stream);
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: due to php's internal stream buffering the `$count` passed to the read callback
|
||||||
|
will be equal to php's internal buffer size (8192 on default) an not the number of bytes
|
||||||
|
requested by `fopen()`
|
||||||
Loading…
Add table
Add a link
Reference in a new issue