Index

Stream

MMO\core\server · 1 symbol · generated 30 Sep 2026

class

Stream

Stream.php:13

PHP stream implementation.

class MMO\core\server\Stream implements Psr\Http\Message\StreamInterface, MMO\core\interfaces\iRenderable, MMO\core\interfaces\iSerializable
Namespace
MMO\core\server
Implements
StreamInterface, iRenderable, iSerializable

Index

stream
close()StreamInterface::close Detaches and closes the underlying resource -- except STDIN/STDOUT/STDERR when running from the CLI, which are left open since they're process-wide and shared (closing them h…
debugName()
detach()StreamInterface::detach
Empty() staticGet an empty stream WARNING: don't write in it as this instance is shared by whomever call this function
eof()StreamInterface::eof
File($filename, $mode) staticGet a Stream on a file
getContents()StreamInterface::getContents Reads from the current position to the end (unlike render, does NOT rewind first or restore position).
getContentType($single)NOTE: the return type used to be declared string|array even though this method can return false (when the stream has no usable URI) -- harmless in practice (PHP's weak typing silently coerces t…
getMetadata($key)StreamInterface::getMetadata
getResource()
getSize()StreamInterface::getSize
getUri()
isReadable()StreamInterface::isReadable
isSeekable()StreamInterface::isSeekable
isWritable()StreamInterface::isWritable
read($length)StreamInterface::read
render($opts)
rewind()StreamInterface::rewind Shorthand for seek(0).
seek($offset, $whence)StreamInterface::seek
serialize($path, $serializer)
STDERR() static
STDIN() static
STDOUT() static
tell()StreamInterface::tell
TEMP() staticGet a new stream backed by php://temp (memory, spilling to a temp file past ~2MB -- unlike Text, which is always pure memory).
Text($text) staticGet a Stream on a text
unserialize($data, $path, $instances)
write($text)StreamInterface::write Writes the full string, looping over fwrite (which can write fewer bytes than requested in one call, e.g.
__construct($stream)Constructor
__destruct()Destructor. Closes the stream
__toString()StreamInterface::__toString

Properties1

stream

protected $stream = null

Methods31

close()

Stream.php:230

StreamInterface::close Detaches and closes the underlying resource -- except STDIN/STDOUT/STDERR when running from the CLI, which are left open since they're process-wide and shared (closing them here would break anything else still using them, e.g. later STDOUT calls).

function close(): void
returnsvoid

debugName()

Stream.php:32
function debugName(): string
returnsstringthe underlying resource's filename (basename only), or the class name if the stream is detached.

detach()

Stream.php:242

StreamInterface::detach

function detach(): mixed
returnsmixedthe raw resource (now detached from this Stream instance -- the caller owns it and must close it themselves), or null if the stream was already detached.

Empty()

staticStream.php:185

Get an empty stream WARNING: don't write in it as this instance is shared by whomever call this function

static function Empty(): self
returnsself

eof()

Stream.php:297

StreamInterface::eof

function eof(): bool
returnsbooltrue if the stream is detached, or if its read pointer is at the end.

File($filename, $mode)

staticStream.php:153

Get a Stream on a file

static function File(string $filename, string $mode = 'w+'): self
$filenamestring
$mode = 'w+'stringfopen
returnsself
throwsif the file cannot be oppened

getContents()

Stream.php:323

StreamInterface::getContents Reads from the current position to the end (unlike render, does NOT rewind first or restore position).

function getContents(): string
returnsstringthe remaining stream content from the current position onward.
throwsif the stream is detached, or reading fails.

getContentType($single)

Stream.php:62

NOTE: the return type used to be declared string|array even though this method can return false (when the stream has no usable URI) -- harmless in practice (PHP's weak typing silently coerces that false into an empty string for a string-compatible return type, and every real caller checks the result loosely, e.g.

function getContentType($single = true): array|string|false`!$content`, which treats `''` the same as `false`), but misleading: a caller checking `=== false` specifically (as the literal `return false` suggests was intended) would never match. Widened the return type to make the actual contract explicit.
$single = true
returnsarray | string | false

getMetadata($key)

Stream.php:216

StreamInterface::getMetadata

function getMetadata($key = null): mixed
See
stream_get_meta_data
$key = nulla specific metadata key (e.g. 'uri', 'mode', 'seekable'), or null for all of it.
returnsmixedthe requested value (null if $key doesn't exist), the full metadata array if $key is null, or ([]/null respectively) if the stream is detached.

getResource()

Stream.php:40
function getResource()
returnsthe raw underlying PHP stream resource (null if detach was called).

getSize()

Stream.php:257

StreamInterface::getSize

function getSize(): ?int
returnsnull | intthe stream's size in bytes, or null if it can't be determined (e.g. a pipe, or a detached stream).

getUri()

Stream.php:48
function getUri(): string
returnsstringthe underlying resource's full path/URI (e.g. /tmp/foo.txt, php://memory), or '' if it has none.
Stream::File('/tmp/foo.txt')->getUri(); // '/tmp/foo.txt'

isReadable()

Stream.php:271

StreamInterface::isReadable

function isReadable(): bool
returnsbooltrue if the stream's open mode (fopen) starts with r or has + as its second character.
Stream::File($f, 'a')->isReadable(); // false -- append-only, not readable

isSeekable()

Stream.php:289

StreamInterface::isSeekable

function isSeekable(): bool
returnsbooltrue if the underlying resource supports seek (e.g. a file or php://memory; false for pipes/sockets).

isWritable()

Stream.php:280

StreamInterface::isWritable

function isWritable(): bool
returnsbooltrue if the stream's open mode (fopen) is anything other than plain r/rb/rt.

read($length)

Stream.php:366

StreamInterface::read

function read(int $length): string
$lengthintmaximum number of bytes to read (may return fewer, e.g. near EOF).
returnsstringup to $length bytes read from the current position ('' if $length is 0).
throwsif $length is negative.
throwsif the stream is non-readable (isReadable), or reading fails.
Stream::Text('Hello, World!')->read(5); // 'Hello'

render($opts)

Stream.php:83
function render(array $opts = []): string
See
iRenderable::render Reads the full stream content, restoring the original position afterward (if seekable) so the stream can still be read again normally.
$opts = []arrayunused.
returnsstringthe stream's full content, or an error message string if reading fails.
Stream::Text('hi')->render(); // 'hi'

rewind()

Stream.php:339

StreamInterface::rewind Shorthand for seek(0).

function rewind(): void
returnsvoid
throwsif the stream is not seekable, or the seek fails.

seek($offset, $whence)

Stream.php:350

StreamInterface::seek

function seek(int $offset, int $whence = SEEK_SET): void
See
fseek
$offsetintthe position to seek to (interpreted per $whence).
$whence = SEEK_SETint\SEEK_SET (from the start, default), \SEEK_CUR (from the current position), or \SEEK_END (from the end).
returnsvoid
throwsif the stream is not seekable (isSeekable), or the seek fails.

serialize($path, $serializer)

Stream.php:108
function serialize(string $path, callable $serializer): array
See
iSerializable::serialize Serializes as the resource's URI and open mode, so unserialize can reopen an equivalent stream -- NOTE: the actual stream content is NOT captured (a php://memory stream, for example, cannot be reopened by URI and will come back empty).
$pathstringunused.
$serializercallableunused.
returnsarray[uri, mode, '$' => static::class].

STDERR()

staticStream.php:140
static function STDERR(): self
returnsselfa shared instance wrapping the process's STDERR (or php://stderr when not running from the CLI).

STDIN()

staticStream.php:133
static function STDIN(): self
returnsselfa shared instance wrapping the process's STDIN (or php://input when not running from the CLI).

STDOUT()

staticStream.php:126
static function STDOUT(): self
returnsselfa shared instance wrapping the process's STDOUT (or php://output when not running from the CLI).

tell()

Stream.php:306

StreamInterface::tell

function tell(): int
returnsintthe current read/write pointer position, in bytes from the start.
throwsif the stream is detached, or the position can't be determined.

TEMP()

staticStream.php:177

Get a new stream backed by php://temp (memory, spilling to a temp file past ~2MB -- unlike Text, which is always pure memory).

static function TEMP(): self
returnsself

write($text)

Stream.php:385

StreamInterface::write Writes the full string, looping over fwrite (which can write fewer bytes than requested in one call, e.g. on a full buffer) until everything has been written.

function write(string $text): int
$textstring
returnsintthe number of bytes written (always strlen($text) on success).
throwsif the stream is non-writable (isWritable), or a write call fails.

__construct($stream)

Stream.php:23

Constructor

function __construct($stream)
$streamthe resource to use as a StreamInterface. If string, will be converted to a stream in memory
throwsif the stream is not a resource

__destruct()

Stream.php:197

Destructor. Closes the stream

function __destruct()

__toString()

Stream.php:205

StreamInterface::__toString

function __toString(): string
returnsstringthe stream's full content (via render), or '' if reading throws.