kusabi/stream

PHP的PSR-7和PSR-17符合的流包装库

1.0.7 2020-10-30 13:45 UTC

This package is auto-updated.

Last update: 2024-08-29 05:36:21 UTC


README

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

PSR-7和PSR-17符合的流库实现

安装

使用composer简单安装。

composer require kusabi/stream

或者只需将其添加到您的composer.json文件中

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

使用流

Stream类是对流资源的一个非常基本的包装。

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

使用流工厂

流工厂也可以用来创建Stream实例。

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