Skip to content

SimpleStream

A minimal PSR-7 StreamInterface over a plain PHP stream resource, so the framework can produce response bodies without depending on a third-party PSR-7 implementation.

Used for the bodies built by PsrResponseBuilder and PsrResponseAdapter; SimpleStream::fromString() is the common entry point and wraps content in a rewound php://temp handle.

Deliberately thin: SimpleStream::getSize() always answers null rather than stat’ing the handle, and SimpleStream::__toString() rewinds first and returns an empty string on any failure, since PHP forbids throwing from string conversion. Every other operation on a detached stream throws RuntimeException. Constructing with a non-resource does not fail — a fresh php://temp handle is substituted.

class SimpleStream implements StreamInterface

ImplementsStreamInterface
SourceHttp/SimpleStream.php

public function __construct(resource $resource): mixed

ParameterTypeDescription
$resourceresource

Returns mixed

MethodDescription
__toString(): stringReads all data from the stream into a string, from the beginning to end.
close(): voidCloses the underlying handle when one is still open; a detached or already closed stream is a no-op.
[`detach(): resourcenull`](#detach)
eof(): boolReports whether the handle has reached end-of-file.
fromString(string $content): SimpleStreamWraps $content in a new read/write php://temp handle, rewound to the start so the content can be read back immediately.
getContents(): stringReads everything remaining from the current position onwards, without rewinding first.
getMetadata(mixed $key = null): mixedReturns the handle’s stream metadata, or a single entry from it.
getSize(): ?intAlways returns null: the size of the wrapped handle is never determined.
isReadable(): boolReports readability by inspecting the handle’s open mode for a reading flag.
isSeekable(): boolReports the handle’s seekability as declared by its stream metadata.
isWritable(): boolReports writability by inspecting the handle’s open mode for a writing flag.
read(mixed $length): stringReads up to $length bytes from the current position; a non-positive length yields an empty string.
rewind(): voidSeeks back to the start of the stream.
seek(mixed $offset, mixed $whence = SEEK_SET): voidMoves the handle position.
tell(): intReturns the current handle position.
write(mixed $string): intWrites to the handle at its current position and returns the byte count written.

public function __toString(): string

Reads all data from the stream into a string, from the beginning to end.

This method MUST attempt to seek to the beginning of the stream before reading data and read the stream until the end is reached.

Warning: This could attempt to load a large amount of data into memory.

This method MUST NOT raise an exception in order to conform with PHP’s string casting operations.

Returns string

public function close(): void

Closes the underlying handle when one is still open; a detached or already closed stream is a no-op.

public function detach(): resource|null

Releases the underlying handle to the caller and leaves this stream detached.

Every subsequent operation other than close() and __toString() throws RuntimeException, since there is no resource left to act on.

Returns resource``|``null — the handle, or null when the stream was already detached

public function eof(): bool

Reports whether the handle has reached end-of-file.

Returns bool

ThrowsWhen
RuntimeExceptionwhen the stream is detached

public static function fromString(string $content): SimpleStream

Wraps $content in a new read/write php://temp handle, rewound to the start so the content can be read back immediately.

ParameterTypeDescription
$contentstring

Returns SimpleStream

ThrowsWhen
RuntimeExceptionwhen the temporary handle cannot be opened

public function getContents(): string

Reads everything remaining from the current position onwards, without rewinding first.

Returns string

ThrowsWhen
RuntimeExceptionwhen the stream is detached, or the read fails

public function getMetadata(mixed $key = null): mixed

Returns the handle’s stream metadata, or a single entry from it.

With no key the whole stream_get_meta_data() array is returned; with a key that the metadata does not contain, null is returned.

ParameterTypeDescription
$keymixed

Returns mixed

ThrowsWhen
RuntimeExceptionwhen the stream is detached

public function getSize(): ?int

Always returns null: the size of the wrapped handle is never determined.

Returns ?``int

public function isReadable(): bool

Reports readability by inspecting the handle’s open mode for a reading flag.

Returns bool

ThrowsWhen
RuntimeExceptionwhen the stream is detached

public function isSeekable(): bool

Reports the handle’s seekability as declared by its stream metadata.

Returns bool

ThrowsWhen
RuntimeExceptionwhen the stream is detached

public function isWritable(): bool

Reports writability by inspecting the handle’s open mode for a writing flag.

Returns bool

ThrowsWhen
RuntimeExceptionwhen the stream is detached

public function read(mixed $length): string

Reads up to $length bytes from the current position; a non-positive length yields an empty string.

ParameterTypeDescription
$lengthmixed

Returns string

ThrowsWhen
RuntimeExceptionwhen the stream is detached, or the read fails

public function rewind(): void

Seeks back to the start of the stream.

ThrowsWhen
RuntimeExceptionwhen the stream is detached, or the seek fails

public function seek(mixed $offset, mixed $whence = SEEK_SET): void

Moves the handle position.

ParameterTypeDescription
$offsetmixed
$whencemixed
ThrowsWhen
RuntimeExceptionwhen the stream is detached, or the seek fails

public function tell(): int

Returns the current handle position.

Returns int

ThrowsWhen
RuntimeExceptionwhen the stream is detached, or the position cannot be read

public function write(mixed $string): int

Writes to the handle at its current position and returns the byte count written.

ParameterTypeDescription
$stringmixed

Returns int

ThrowsWhen
RuntimeExceptionwhen the stream is detached, not opened for writing, or the write fails