kusabi/stream

A PSR-7 and PSR-17 conforming stream wrapper library for PHP

1.0.7 2020-10-30 13:45 UTC

This package is auto-updated.

Last update: 2024-11-29 06:47:03 UTC


README

Tests codecov Licence Badge Release Badge Tag Badge Issues Badge Code Size

An implementation of a PSR-7 & PSR-17 conforming Stream library

Installation

Installation is simple using composer.

composer require kusabi/stream

Or simply add it to your composer.json file

{
    "require": {
        "kusabi/stream": "^1.0"
    }
}

Using streams

The Stream class is a very basic wrapper around a stream resource.

use Kusabi\Stream\Stream;

// Instantiate a Uri instance
$stream = new Stream(fopen('php://stdin', 'r'));

// Fetch the properties of the Stream
$stream->getContents(); // Get everything from the current pointer to the end of the stream
$stream->getSize(); // Get the size of the stream in bytes
$stream->isSeekable();
$stream->isReadable();
$stream->isWritable();
$stream->seek($offset, $whence = SEEK_SET); // Move the pointer around in the stream
$stream->tell(); // Where is the pointer in the stream
$stream->rewind(); // Set the pointer to the beginning of the stream
$stream->read($length); // Read the next $length character from the stream
$stream->write($string); // Write data into the stream. Returns the number of bytes written
$stream->getMetadata($key = null); // Get all the metadata, or a particular key
$stream->getStat($key = null); // Get all the fstat entries, or a particular key
$stream->isLocal(); // Determine if the stream url is local using `stream_is_local()`
$stream->getLine($length = null, $ending = "\n"); // Fetch a line up to a length or delimiter (which ever comes first)
$stream->pipe(Stream $stream); // Copy the contents of one stream into another
(string) $stream; // Rewind and get all the contents from the stream

Using the stream factory

The Stream factory can be used to create the Stream instance too.

use Kusabi\Stream\StreamFactory;

// Instantiate a Uri instance
$factory = new StreamFactory();
$stream = $factory->createStream('temp resource with data in it');
$stream = $factory->createStreamFromFile('file.txt');
$stream = $factory->createStreamFromResource(fopen('php://stdin', 'r'));