CallbackStream implements Stringable, StreamInterface
FinalYes
A PSR-7 StreamInterface that executes a callback when read.
This enables true streaming with echo/flush() for SSE (Server-Sent Events). The callback is executed once when the stream is first read and can write directly to the output buffer (e.g. via echo + flush()).
Example usage for SSE:
$stream = new CallbackStream(function() {
echo "event: message\n";
echo "data: {\"hello\":\"world\"}\n\n";
flush();
});
Tags
Table of Contents
Interfaces
- Stringable
- StreamInterface
Properties
- $callback : mixed
- $called : bool
- $exception : Throwable|null
- $logger : LoggerInterface
Methods
- __construct() : mixed
- __toString() : string
- close() : void
- detach() : mixed
- eof() : bool
- getContents() : string
- getMetadata() : mixed
- getSize() : int|null
- isReadable() : bool
- isSeekable() : bool
- isWritable() : bool
- read() : string
- rewind() : void
- seek() : void
- tell() : int
- write() : int
- invoke() : void
Properties
$callback
private
mixed
$callback
$called
private
bool
$called
= false
$exception
private
Throwable|null
$exception
= null
$logger
private
LoggerInterface
$logger
= new NullLogger()
Methods
__construct()
public
__construct(callable(): void $callback[, LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
- $callback : callable(): void
-
The callback to execute when stream is read
- $logger : LoggerInterface = new NullLogger()
__toString()
public
__toString() : string
Return values
stringclose()
public
close() : void
detach()
public
detach() : mixed
eof()
public
eof() : bool
Return values
boolgetContents()
public
getContents() : string
Return values
stringgetMetadata()
public
getMetadata([mixed $key = null ]) : mixed
Parameters
- $key : mixed = null
getSize()
public
getSize() : int|null
Return values
int|nullisReadable()
public
isReadable() : bool
Return values
boolisSeekable()
public
isSeekable() : bool
Return values
boolisWritable()
public
isWritable() : bool
Return values
boolread()
public
read(mixed $length) : string
Parameters
- $length : mixed
Return values
stringrewind()
public
rewind() : void
seek()
public
seek(mixed $offset[, mixed $whence = SEEK_SET ]) : void
Parameters
- $offset : mixed
- $whence : mixed = SEEK_SET
tell()
public
tell() : int
Return values
intwrite()
public
write(mixed $string) : int
Parameters
- $string : mixed
Return values
intinvoke()
private
invoke() : void