stream-interop / impl
Reference implementations for the stream-interop/interface package.
Requires
- php: >=8.4
- stream-interop/interface: ^1.0
Requires (Dev)
- pds/composer-script-names: ^1.0
- pds/skeleton: ^1.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- pmjones/php-styler: 0.x@dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 20:25:16 UTC
README
Reference implementations of stream-interop/interface.
Installation
Install this package via Composer:
$ composer require stream-interop/impl
Implementations
All implementations throw a StreamException (a RuntimeException that implements StreamThrowable) on failure.
File Streams
ConsumableFileStream, ReadableFileStream, ReadWriteFileStream, and WritableFileStream encapsulate a resource you have already opened. They do not close it for you; whoever opens the resource closes it.
ReadonlyFileStream and ImmutableFileStream instead take a file name, URI, or open resource, and keep a copy of their own.
ConsumableFileStream
A forward-only readable stream; does not afford seeking or writing. Good for pipes, sockets, and remote streams.
$stream = new \StreamInterop\Impl\ConsumableFileStream(fopen('/path/to/file', 'rb')); $stream->read(2); // reads the first 2 bytes $stream->read(4); // reads the next 4 bytes $stream->getContents(); // reads all remaining bytes $stream->eof(); // true
ReadableFileStream
A readable stream that affords seeking and stringability.
$stream = new \StreamInterop\Impl\ReadableFileStream(fopen('/path/to/file', 'rb')); $stream->read(2); // reads the first 2 bytes $stream->read(4); // reads the next 4 bytes $stream->getContents(); // reads all remaining bytes $stream->eof(); // true $stream->__toString(); // reads the entire file; eof() is still true $stream->rewind(); // rewinds the pointer $stream->seek(7); // moves to byte 7 $stream->read(3); // reads the next 3 bytes
ReadonlyFileStream
Identical to the ReadableFileStream, but it makes a copy of the original resource to enforce readonly constraints. If the original resource is modified, the ReadonlyFileStream will not reflect those changes.
// create the origin file $file = '/path/to/file'; file_put_contents($file, 'foo bar'); // initialize a readonly stream from the file; // the readonly stream reads the file into memory. $stream = new \StreamInterop\Impl\ReadonlyFileStream($file); assert((string) $stream === file_get_contents($file)); // change the origin file file_put_contents($file, 'baz dib'); // the stream does not reflect the changes assert((string) $stream !== file_get_contents($file));
It accepts a file name or URI, or an open resource. It copies all of a
seekable resource, leaving that resource's pointer where it was, or the rest
of a non-seekable one. It opens 'php://input' directly instead of copying
it, and reads the request body through once on opening, so that seeking
works where PHP reads the body on demand. Clones get their own copy. Since it
opens its own copy, it also affords closing it.
ImmutableFileStream
Like the ReadonlyFileStream, but affords only idempotent reading (stringability) and getting its size; it cannot be read from, seeked, or closed.
$stream = new \StreamInterop\Impl\ImmutableFileStream('/path/to/file'); $stream->__toString(); // reads the entire file $stream->subString(4, 5); // reads 5 bytes starting at byte 4 $stream->subString(-4); // reads the last 4 bytes $stream->getSize(); // the size in bytes
To make one from a string, pass a data: URI:
$stream = new \StreamInterop\Impl\ImmutableFileStream( 'data://text/plain;base64,' . base64_encode('Hello World!') );
ReadWriteFileStream
A fully read+write stream that affords seeking and stringability.
$stream = new \StreamInterop\Impl\ReadWriteFileStream(fopen('/path/to/file', 'wb+')); $stream->write('Hello '); $stream->write('World!'); $stream->rewind(); $stream->read(2); // reads "He" $stream->read(4); // reads "llo " $stream->getContents(); // reads "World!" $stream->eof(); // true $stream->__toString(); // reads "Hello World!"
WritableFileStream
A unidirectional writable stream; does not afford seeking or reading. It
accepts resources opened in any writable mode (w, a, x, c, or +).
$stream = new \StreamInterop\Impl\WritableFileStream(fopen('/path/to/file', 'wb')); $stream->write('Hello World!');
Lazy Ghost File Objects
These streams open a resource themselves on a specified filename, and afford
closing it. They open the file on first use (including isOpen(),
isClosed(), $metadata, and getSize()), not when constructed. If the file
cannot be opened, those status checks report neither open nor closed instead
of throwing, and reading $resource throws a StreamException. Calling
close() before first use closes the stream without opening the file.
ReadableFile
A readable lazy-opening stream that affords seeking and stringability. When
serialized, it keeps only the filename (and whether it was closed), and
reopens the file on first use after unserializing. For 'php://input', it
reads the request body through once on opening, so that seeking works where
PHP reads the body on demand.
// construct with a file name, not an open resource $stream = new \StreamInterop\Impl\ReadableFile('/path/to/file'); $stream->read(2); // opens the file, then reads the first 2 bytes $stream->read(4); // reads the next 4 bytes $stream->getContents(); // reads all remaining bytes $stream->eof(); // true $stream->__toString(); // reads the entire file $stream->rewind(); // rewinds the pointer $stream->seek(7); // moves to byte 7 $stream->read(3); // reads the next 3 bytes $stream->close(); // closes the file
ReadWriteFile
A fully read+write lazy-opening stream that affords seeking and stringability. It opens an existing file without truncating it, and does not create a missing one. Writing overwrites the file from the pointer, so a longer existing file keeps its remaining bytes. Like ReadableFile, it serializes to its filename.
// construct with the name of an existing, empty file, not an open resource $stream = new \StreamInterop\Impl\ReadWriteFile('/path/to/file'); $stream->write('Hello World!'); $stream->rewind(); $stream->read(2); // reads "He" $stream->read(4); // reads "llo " $stream->getContents(); // reads "World!" $stream->eof(); // true $stream->__toString(); // reads "Hello World!"
WritableFile
A write-only lazy-opening stream. It opens the file without truncating it, and
truncates it on the first write, so status checks and cloning leave an
existing file alone. It cannot be serialized, since reopening the file would
truncate it again. It does not truncate php:// targets such as
php://stdout or php://output, which fopen() in w mode leaves alone.
On device files such as /dev/null and /dev/stdout, it truncates as
fopen() in w mode would, which differs between systems.
Writing to its $resource directly does not truncate the file. A later
write() truncates only if the pointer is back at the start, so after a
rewind() of $resource it discards what was written directly, and after an
fseek() away from the start the first write() leaves the file as it is.
It needs a target it can write in c mode and then truncate.
compress.zlib:// and compress.bzip2:// do not open in that mode, so a
write to one throws, and compress.zlib:// leaves an empty file behind.
php://memory and php://temp open in it read-only, so they report neither
open nor closed, and a write throws. A userland wrapper without
stream_truncate() opens, but every write throws and leaves the file as it
was. For those, use a WritableFileStream on a resource opened in w mode.
// construct with a file name, not an open resource $stream = new \StreamInterop\Impl\WritableFile('/path/to/file'); $stream->isOpen(); // true; opens the file, but leaves it untouched $stream->write('Hello'); // truncates the file, then writes $stream->close();
Non-Resource Streams
StringStream
A fully read+write stream that affords seeking, stringability, and closing,
backed by a string instead of a resource. It behaves as if it encapsulates a
php://memory resource (its $metadata matches that of php://memory,
except that, as in 1.0, it reports an empty wrapper_type and wrapper_data,
and no uri): writing overwrites at the pointer, writing past the end fills
the gap with null bytes, and eof() is true only after a read asks for more
than remains. As with php://memory, a gap that does not fit in the memory
limit is a fatal error.
// construct with string data, not a resource or file name $stream = new \StreamInterop\Impl\StringStream('Hello'); $stream->read(2); // reads 'He' $stream->getContents(); // reads 'llo' $stream->write(' World!'); $stream->rewind(); $stream->getContents(); // reads 'Hello World!' $stream->rewind(); $stream->write('J'); $stream->__toString(); // 'Jello World!'