icewind / streams
A set of generic stream wrappers
Installs: 1 822 503
Dependents: 3
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 10
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-11-05 20:59:42 UTC
README
Streams
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
Example
<?php use \Icewind\Streams\CallBackWrapper; require('vendor/autoload.php'); // get an existing stream to wrap $source = fopen('php://temp', 'r+'); // register the callbacks $stream = CallbackWrapper::wrap($source, // read callback function ($count) { echo "read " . $count . "bytes\n"; }, // write callback function ($data) { echo "wrote '" . $data . "'\n"; }, // close callback function () { echo "stream closed\n"; }); 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()