Opinionated abstraction around PHP streams implementing PSR-7 StreamInterface. It aims to improve working with files or remote streams in unified way.
Heavily inspired by guzzle/psr7.
Via Composer
$ composer require digitalcz/streamsAll streams implement DigitalCz\Streams\StreamInterface, which extends PSR-7
Psr\Http\Message\StreamInterface and adds a few convenience methods
(copy(), void-returning close()/seek(), …).
A wrapper around any PHP stream resource. Use Stream::from() to build one from
a string, a resource or another PSR-7 stream — the data is held in php://temp.
useDigitalCz\Streams\Stream;
// From a string$stream = Stream::from('Hello world');
echo$stream->getContents(); // "Hello world"// From an existing resource$stream = Stream::from(fopen('php://memory', 'rb+'));
// From another PSR-7 stream$stream = Stream::from($psrStream);
// Wrapping a resource directly (optionally with a known size)$stream = newStream(fopen('data.bin', 'rb'));
// Copy another stream into this one (returns bytes written)$bytes = $stream->copy($otherStream);A stream backed by a real file on disk. Adds getPath() and delete().
useDigitalCz\Streams\File;
$file = newFile('/path/to/file.txt'); // opened with mode "rb+" by defaultecho$file->getPath();
// Build a file from a string / resource / PSR-7 stream.// A string that is an existing path opens that file, otherwise it is the content.$file = File::from('contents written to a temp file');
$temp = File::temp(); // empty file in the system temp dir$file->delete(); // close and unlinkLike File, but backed by tmpfile() — the underlying file is removed
automatically once the stream handle is closed.
useDigitalCz\Streams\TempFile;
$temp = TempFile::from('temporary content');
echo$temp->getPath();
$temp->close(); // file is deleted on closeWraps a non-seekable / one-shot source stream and buffers what it reads into
php://temp, so the source can be re-read and seeked. It is read-only.
useDigitalCz\Streams\BufferedStream;
$buffered = newBufferedStream($nonSeekableSource);
$head = $buffered->read(1024);
$buffered->rewind(); // works even if the source could not seek$all = $buffered->getContents();Turns a StreamInterface back into a native PHP stream resource, usable with
any function that expects one (fread, fgets, stream_copy_to_stream, …).
useDigitalCz\Streams\StreamWrapper;
$resource = StreamWrapper::from($stream);
$line = fgets($resource);Please see CHANGELOG for more information on what has changed recently.
$ composer csfix # fix codestyle
$ composer checks # run all checks # or separately
$ composer tests # run phpunit
$ composer phpstan # run phpstan
$ composer cs # run codesnifferPlease see CONTRIBUTING for details.
If you discover any security related issues, please email devs@digital.cz instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.