zenstruck/stream

Object wrapper for PHP resources.

Fund package maintenance!
kbond

v1.2.0 2023-02-15 16:25 UTC

This package is auto-updated.

Last update: 2024-04-15 19:04:06 UTC


README

CI Status codecov

Object wrapper for PHP resources.

Installation

composer require zenstruck/stream

API

Create Stream Object

use Zenstruck\Stream;

// wrap
$stream = Stream::wrap($resource);
$stream = Stream::wrap('string');
$stream = Stream::wrap($stream);

// open
$stream = Stream::open('some/file.txt', 'r');

// php://memory
$stream = Stream::inMemory();

// php://output
$stream = Stream::inOutput();

// \tmpfile()
$stream = Stream::tempFile();

// autoclose on $stream __destruct
$stream->autoClose(); // Stream

Use Stream Object

/** @var \Zenstruck\Stream $stream */

// metadata
$stream->id(); // int - the resource id
$stream->type(); // string - the resource type
$stream->metadata(); // array - the resources metadata
$stream->metadata('wrapper_type'); // mixed - specific resource metadata key
$stream->uri(); // string - shortcut for `$stream->metadata('uri')`

// read
$stream->get(); // resource - the raw, wrapped resource
$stream->contents(); // string - the contents of the resource (auto-rewound)

// write
$stream->write($resource); // self - write another resource to the stream
$stream->write('string'); // self - write a string to the stream
$stream->write($anotherStream); // self - write another \Zenstruck\Stream instance to the stream

// manipulate
$stream->close(); // no-return - close the resource (if open)
$stream->rewind(); // self - rewind the stream