phasync\Psr\BufferedStream class

Documentation

PSR-7 StreamInterface

Designed for returning a response which has not been completed yet. A coroutine can continue appending to the stream. Once the stream buffer reaches 2 MB, the stream is converted to a disk backed tempfile.

This is not suitable for streaming unlimited amounts of data; use the UnbufferedStream class or a ComposableStream instead - which won't be seekable and won't provide a stream length.

Usage:

$s = new BufferedStream();

// Create a coroutine which appends to the stream
phasync::go(function() use ($s) {
    // Append chunks as much as you need
    $s->append("A chunk");
    // Signal that the stream is complete
    $s->end();
});

// Return the stream
return $response->withStream($s);

By default, content up to 2 MB is buffered in memory, after which content will be moved to a temporary disk file.

Inheritance

Implements: Psr\Http\Message\StreamInterface Stringable

Properties (10)

private string $buffer

The string buffer

private mixed $file

The file resource if the stream is file backed

private int $readOffset
private int $writeOffset
private ?int $endOffset
private int $bufferSize
private float $deadlockTimeout
private bool $closed
private bool $locked
private bool $detached

Methods (24)

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Documentation missing

Append more data to the stream

Inform that no more content will be appended to the stream,

Transition the in-memory buffer to a disk backed file.

Get a lock on the stream. This MUST be followed up by a call

Release the lock on the stream. This should only be called if

Documentation missing

Documentation missing

Documentation missing

Source

src/Psr/BufferedStream.php:41-418