PHP code example of yuloh / stream

1. Go to this page and download the library: Download yuloh/stream library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yuloh / stream example snippets


$stream   = (new StreamFactory())->create('Hello world!');

use Yuloh\Stream\Stream;

$resource = fopen('php://temp', 'r+');
$stream = new Stream($resource);

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());