kuyoto / psr7-streams
Provides PSR-7 stream decorators.
Requires
- php: ^7.2
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16.3
- phpunit/phpunit: ^7.5 || ^8.0
This package is auto-updated.
Last update: 2024-10-27 20:56:01 UTC
README
This repository provides several PSR-7 stream decorators.
Installation
The recommnended way to install this library is through composer:
composer require kuyoto/psr7-streams
Stream Decorators
This library consists of the following Psr\Http\Message\StreamInterface
impementations:
ConcatenatedStream
Kuyoto\Psr7\Stream\ConcatenatedStream
Reads from multiple streams, one after the other.
use Kuyoto\Psr7\Stream\StringStream; use Kuyoto\Psr7\Stream\ConcatenatedStream; $a = new StringStream('foo'); $b = new StringStream('bar'); $composed = new ConcatenatedStream([$a, $b]); echo $composed; // foobar.
NoSeekStream
Kuyoto\Psr7\Stream\NoSeekStream
It wraps a stream and does not allow seeking.
use Kuyoto\Psr7\Stream\StringStream; use Kuyoto\Psr7\Stream\NoSeekStream; $stream = new StringStream('foo'); $noSeek = new NoSeekStream($stream); echo $noSeek->read(3); // foo var_export($noSeek->isSeekable()); // false $noSeek->seek(0); var_export($noSeek->read(3)); // NULL
NullStream
Kuyoto\Psr7\Stream\NullStream
NullStream does not store any data written to it.
use Kuyoto\Psr7\Stream\NullStream; $stream = new NullStream(); echo $stream->read(3); // '' var_export($stream->getSize()); // NULL
StringStream
Kuyoto\Psr7\Stream\StringStream
StringStream allows instantiation of a stream from a the provided string.
use Kuyoto\Psr7\Stream\StringStream; $stream = new StringStream('foo'); echo $stream->read(2); // fo var_export($stream->getSize()); // 3
Implementing Stream Decorators
Creating a stream decorator is very easy thanks to the Kuyoto\Psr7\Stream\StreamDecorator
. This decorator provides methods that implement Psr\Http\Message\StreamInterface
by proxying to an underlying stream. Just use
the StreamDecoratorTrait
and implement your custom methods.
Testing
composer test
License
The package is an open-sourced software licensed under the MIT License.