yuloh / stream
A stream implementation for PSR-7
Installs: 1 811
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ~5.5|~7.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.3
- php-mock/php-mock: ^1.0
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.3
- slim/slim: ^3.4
- squizlabs/php_codesniffer: ~2.3
- zendframework/zend-diactoros: ^1.3
This package is auto-updated.
Last update: 2022-02-01 12:58:20 UTC
README
A PSR-7 stream implementation.
Introduction
This package provides an implementation of Psr\Http\Message\StreamInterface
for PSR-7. Sometimes you just need a stream but don't want to depend on an entire PSR-7 implementation.
Let's say you are writing a PSR-7 middleware, and it needs to set a new response body.
If you try to write the data with $response->getBody()->write()
, there might already be an existing body which is longer than what you want to write. The only way to make that work is to write padding to the end of the string, which is not ideal.
The only way to create a new response body is to instantiate a stream. A good way to do this is to require a factory object as a dependency, and use that to create the stream. The downside is you are adding another setup step that the user will have to configure.
This package lets you provide a default implementation, so your middleware will work without having to setup a stream factory. Since it's a separate package you don't need to pull in an entire PSR-7 implementation just for streams, and you won't end up with dependency conflicts with the user's PSR-7 implementation.
It also includes an interface and adapters for all of the common PSR-7 implementations, so the user doesn't need to set that up manually.
Install
Via Composer
$ composer require yuloh/stream
Usage
Factory
The simplest way to use this package is to use the StreamFactory
. The create
method will create a Stream
from a scalar, string, resource, or object (if it implements either JsonSerializable or __toString).
$stream = (new StreamFactory())->create('Hello world!');
Constructor
You can also create a stream directly. You will need to provide a valid resource as the only argument to the constructor.
use Yuloh\Stream\Stream; $resource = fopen('php://temp', 'r+'); $stream = new Stream($resource);
Allowing Different implementations
To allow the user to use their own stream, you should typehint against the StreamFactoryInterface
instead of using a concrete implementation. This package ships with adapters for all of the common implementations, so the user can easily use their own stream.
use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; use Yuloh\Stream\StreamFactoryInterface; use Yuloh\Stream\StreamFactory; class HelloMiddleware { public function __construct(StreamFactoryInterface $streamFactory = null) { $this->streamFactory = $streamFactory ?: new StreamFactory(); } public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { $stream = $this->streamFactory->create('Hello world!'); $response = $response->withBody($stream); return $next($request, $response); } }
use Yuloh\Stream\Adapters; // Usage with default implementation: new HelloMiddleware(); // Usage with Zend Diactoros: new HelloMiddleware(new Adapters\DiactorosStreamFactory()); // Usage with Guzzle PSR7: new HelloMiddleware(new Adapters\GuzzleStreamFactory()); // Usage With Slim Framework: new HelloMiddleware(new Adapters\SlimStreamFactory());
Testing
$ composer test
License
The MIT License (MIT). Please see License File for more information.