Skip to content
docsv0.9.1

CallbackStream

A response body that is PRODUCED at emit time by a callback, so a handler can stream (greenhouse evidence/0472). The runtime's ordinary emission materializes a body — `echo (string) $body` — which buffers the whole response before a byte reaches the client. That is correct for a normal page and fatal for `text/event-stream`: an SSE feed must write and flush events over the life of the connection. A `CallbackStream` carries a callback INSTEAD of bytes; {@see ResponseEmitter} recognizes it and runs the callback (which writes to the output and flushes) rather than stringifying it. The callback owns the loop and the flushing; this class is only the marker that carries it through the PSR-7 response. It is deliberately not a readable/materializable stream: stringifying it yields an empty string, so an app still on the buffered emission path (not the {@see ResponseEmitter}) serves nothing rather than a broken half-response — a loud "you must emit this with the streaming emitter", not a silent corruption.

CallbackStream::__construct()

public function __construct(Closure $callback):

Parameters

Parameters of __construct()
NameTypeDescription
$callback\Closure(): voidWrites the body to the output (echo + flush), owning any loop.

CallbackStream::callback()

public function callback(): callable

The callback {@see ResponseEmitter} invokes to stream the body.

CallbackStream::__toString()

public function __toString(): string

Not materializable: the body only exists when streamed by the emitter.

CallbackStream::close()

public function close(): void

No underlying resource to close.

CallbackStream::detach()

public function detach():

No underlying resource to detach.

CallbackStream::getSize()

public function getSize(): ?int

Unknown until streamed, so unknown by contract.

CallbackStream::tell()

public function tell(): int

Nothing to point into.

CallbackStream::eof()

public function eof(): bool

Always at the end: there is no buffer to read from.

CallbackStream::isSeekable()

public function isSeekable(): bool

A callback body cannot be rewound.

CallbackStream::seek()

public function seek(int $offset, int $whence = Milpa\Runtime\Http\SEEK_SET): void

Not seekable: the body is produced once, by the emitter.

Parameters

Parameters of seek()
NameTypeDescription
$offsetint
$whenceint

CallbackStream::rewind()

public function rewind(): void

Not seekable: the body is produced once, by the emitter.

CallbackStream::isWritable()

public function isWritable(): bool

The content comes from the callback, not from writes to this stream.

CallbackStream::write()

public function write(string $string): int

Not writable: the content is produced by the callback.

Parameters

Parameters of write()
NameTypeDescription
$stringstring

CallbackStream::isReadable()

public function isReadable(): bool

There is nothing to read: the body is streamed by the emitter, not read here.

CallbackStream::read()

public function read(int $length): string

Nothing to read: the body only exists when streamed by the emitter.

Parameters

Parameters of read()
NameTypeDescription
$lengthint

CallbackStream::getContents()

public function getContents(): string

Not materializable: the body only exists when streamed by the emitter.

CallbackStream::getMetadata()

public function getMetadata(?string $key = null):

No metadata: this carries a callback, not a stream resource.

Parameters

Parameters of getMetadata()
NameTypeDescription
$key(string | null)