PHP code example of drewlabs / psr7-stream

1. Go to this page and download the library: Download drewlabs/psr7-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/ */

    

drewlabs / psr7-stream example snippets


// ...
use Drewlabs\Psr7Stream\Stream;

// ...

// Creates PHP built-in streams such as php://memory and php://temp
$stream = Stream::new('', 'wb+');


// ...
use Drewlabs\Psr7Stream\StreamFactory;

// ...

// Create a PSR7 stream factory instance
$factory = new StreamFactory();

// Creates stream from resources
$stream = $factory->createStreamFromResource(fopen(__DIR__ . '/../../examples/test.txt', 'rb'));


// ...
use Drewlabs\Psr7Stream\StreamFactory;

// ...

// Create a PSR7 stream factory instance
$factory = new StreamFactory();

// Creates stream from path
$stream = $factory->createStreamFromFile(__DIR__ . '/../../examples/test.txt');


use Drewlabs\Psr7Stream\StreamFactory as Factory;

$stream = Factory::stack();

// TO initialize the instance at contruction time
// The stream instance is initialized with 2 chunks
$stream = Factory::stack(Stream::new(''), Stream::new(__DIR__ . '/vendor/autoload.php'))

use Drewlabs\Psr7Stream\StreamFactory as Factory;

$stream = Factory::stack();

// Add a new stream instance
$stream->push(Stream::new('/path/to/resource'));

use Drewlabs\Psr7Stream\StreamFactory as Factory;

$stream = Factory::stack();

// Pop the last stream from the stack and return it
$s = $stream->pop();

use Drewlabs\Psr7Stream\StreamFactory as Factory;
use Drewlabs\Psr7Stream\Stream;
use Drewlabs\Psr7Stream\LazyStream;
//

$stream_source = 'Hello world...';

$stream = Factory::lazy(function() use (&$stream_source) {
    return Stream::new($stream_source);
});

// Or using constructor

$stream = new LazyStream(function() use (&$stream_source) {
    return Stream::new($stream_source);
});

use Drewlabs\Psr7Stream\CreatesStream;
use Drewlabs\Psr7Stream\Stream;

// CreateTextStream.php
class CreateTextStream implements CreatesStream
{
    /**
     * 
     * @var string
     * */
    private $source;

    public function __construct($source)
    {
        $this->source = $source;
    }

    public function createStream()
    {
        return Stream::new($this->source);
    }
}

// main.php
$stream = new LazyStream(new CreateTextStream('/path/to/resource'));

// Or using factory function
$stream = Factory::lazy(new CreateTextStream('/path/to/resource'));