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.
Synopsis
Section titled “Synopsis”class SimpleStream implements StreamInterface
| Implements | StreamInterface |
| Source | Http/SimpleStream.php |
Constructor
Section titled “Constructor”__construct()
Section titled “__construct()”public function __construct(resource $resource): mixed
| Parameter | Type | Description |
|---|---|---|
$resource | resource |
Returns mixed
Methods
Section titled “Methods”| Method | Description |
|---|---|
__toString(): string | Reads all data from the stream into a string, from the beginning to end. |
close(): void | Closes the underlying handle when one is still open; a detached or already closed stream is a no-op. |
| [`detach(): resource | null`](#detach) |
eof(): bool | Reports whether the handle has reached end-of-file. |
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. |
getContents(): string | Reads everything remaining from the current position onwards, without rewinding first. |
getMetadata(mixed $key = null): mixed | Returns the handle’s stream metadata, or a single entry from it. |
getSize(): ?int | Always returns null: the size of the wrapped handle is never determined. |
isReadable(): bool | Reports readability by inspecting the handle’s open mode for a reading flag. |
isSeekable(): bool | Reports the handle’s seekability as declared by its stream metadata. |
isWritable(): bool | Reports writability by inspecting the handle’s open mode for a writing flag. |
read(mixed $length): string | Reads up to $length bytes from the current position; a non-positive length yields an empty string. |
rewind(): void | Seeks back to the start of the stream. |
seek(mixed $offset, mixed $whence = SEEK_SET): void | Moves the handle position. |
tell(): int | Returns the current handle position. |
write(mixed $string): int | Writes to the handle at its current position and returns the byte count written. |
__toString()
Section titled “__toString()”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
close()
Section titled “close()”public function close(): void
Closes the underlying handle when one is still open; a detached or already closed stream is a no-op.
detach()
Section titled “detach()”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
| Throws | When |
|---|---|
RuntimeException | when the stream is detached |
fromString()
Section titled “fromString()”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.
| Parameter | Type | Description |
|---|---|---|
$content | string |
Returns SimpleStream
| Throws | When |
|---|---|
RuntimeException | when the temporary handle cannot be opened |
getContents()
Section titled “getContents()”public function getContents(): string
Reads everything remaining from the current position onwards, without rewinding first.
Returns string
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, or the read fails |
getMetadata()
Section titled “getMetadata()”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.
| Parameter | Type | Description |
|---|---|---|
$key | mixed |
Returns mixed
| Throws | When |
|---|---|
RuntimeException | when the stream is detached |
getSize()
Section titled “getSize()”public function getSize(): ?int
Always returns null: the size of the wrapped handle is never determined.
Returns ?``int
isReadable()
Section titled “isReadable()”public function isReadable(): bool
Reports readability by inspecting the handle’s open mode for a reading flag.
Returns bool
| Throws | When |
|---|---|
RuntimeException | when the stream is detached |
isSeekable()
Section titled “isSeekable()”public function isSeekable(): bool
Reports the handle’s seekability as declared by its stream metadata.
Returns bool
| Throws | When |
|---|---|
RuntimeException | when the stream is detached |
isWritable()
Section titled “isWritable()”public function isWritable(): bool
Reports writability by inspecting the handle’s open mode for a writing flag.
Returns bool
| Throws | When |
|---|---|
RuntimeException | when the stream is detached |
read()
Section titled “read()”public function read(mixed $length): string
Reads up to $length bytes from the current position; a non-positive length yields an empty string.
| Parameter | Type | Description |
|---|---|---|
$length | mixed |
Returns string
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, or the read fails |
rewind()
Section titled “rewind()”public function rewind(): void
Seeks back to the start of the stream.
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, or the seek fails |
seek()
Section titled “seek()”public function seek(mixed $offset, mixed $whence = SEEK_SET): void
Moves the handle position.
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | |
$whence | mixed |
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, or the seek fails |
tell()
Section titled “tell()”public function tell(): int
Returns the current handle position.
Returns int
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, or the position cannot be read |
write()
Section titled “write()”public function write(mixed $string): int
Writes to the handle at its current position and returns the byte count written.
| Parameter | Type | Description |
|---|---|---|
$string | mixed |
Returns int
| Throws | When |
|---|---|
RuntimeException | when the stream is detached, not opened for writing, or the write fails |