Documentation

ReadableResourceStream extends EventEmitter
in package
implements ReadableStreamInterface

FinalYes

Table of Contents

Interfaces

ReadableStreamInterface
The `ReadableStreamInterface` is responsible for providing an interface for read-only streams and the readable side of duplex streams.

Properties

$listeners  : mixed
$onceListeners  : mixed
$bufferSize  : int
Controls the maximum buffer size in bytes to read at once from the stream.
$closed  : mixed
$listening  : mixed
$loop  : LoopInterface
$stream  : resource

Methods

__construct()  : mixed
close()  : void
Closes the stream (forcefully).
emit()  : mixed
isReadable()  : bool
Checks whether this stream is in a readable state (not closed already).
listeners()  : array<string|int, mixed>
on()  : mixed
once()  : mixed
pause()  : void
Pauses reading incoming data events.
pipe()  : WritableStreamInterface
Pipes all the data from this readable source into the given writable destination.
removeAllListeners()  : mixed
removeListener()  : mixed
resume()  : void
Resumes reading incoming data events.
isLegacyPipe()  : bool
Returns whether this is a pipe resource in a legacy environment

Properties

$bufferSize

Controls the maximum buffer size in bytes to read at once from the stream.

private int $bufferSize

This value SHOULD NOT be changed unless you know what you're doing.

This can be a positive number which means that up to X bytes will be read at once from the underlying stream resource. Note that the actual number of bytes read may be lower if the stream resource has less than X bytes currently available.

This can be -1 which means read everything available from the underlying stream resource. This should read until the stream resource is not readable anymore (i.e. underlying buffer drained), note that this does not neccessarily mean it reached EOF.

Methods

__construct()

public __construct(resource $stream[, LoopInterface|null $loop = null ][, int|null $readChunkSize = null ]) : mixed
Parameters
$stream : resource
$loop : LoopInterface|null = null
$readChunkSize : int|null = null

close()

Closes the stream (forcefully).

public close() : void

This method can be used to (forcefully) close the stream.

$stream->close();

Once the stream is closed, it SHOULD emit a close event. Note that this event SHOULD NOT be emitted more than once, in particular if this method is called multiple times.

After calling this method, the stream MUST switch into a non-readable mode, see also isReadable(). This means that no further data or end events SHOULD be emitted.

$stream->close();
assert($stream->isReadable() === false);

$stream->on('data', assertNeverCalled());
$stream->on('end', assertNeverCalled());

If this stream is a DuplexStreamInterface, you should also notice how the writable side of the stream also implements a close() method. In other words, after calling this method, the stream MUST switch into non-writable AND non-readable mode, see also isWritable(). Note that this method should not be confused with the end() method.

emit()

public emit(mixed $event[, array<string|int, mixed> $arguments = [] ]) : mixed
Parameters
$event : mixed
$arguments : array<string|int, mixed> = []

isReadable()

Checks whether this stream is in a readable state (not closed already).

public isReadable() : bool

This method can be used to check if the stream still accepts incoming data events or if it is ended or closed already. Once the stream is non-readable, no further data or end events SHOULD be emitted.

assert($stream->isReadable() === false);

$stream->on('data', assertNeverCalled());
$stream->on('end', assertNeverCalled());

A successfully opened stream always MUST start in readable mode.

Once the stream ends or closes, it MUST switch to non-readable mode. This can happen any time, explicitly through close() or implicitly due to a remote close or an unrecoverable transmission error. Once a stream has switched to non-readable mode, it MUST NOT transition back to readable mode.

If this stream is a DuplexStreamInterface, you should also notice how the writable side of the stream also implements an isWritable() method. Unless this is a half-open duplex stream, they SHOULD usually have the same return value.

Return values
bool

listeners()

public listeners([mixed $event = null ]) : array<string|int, mixed>
Parameters
$event : mixed = null
Return values
array<string|int, mixed>

on()

public on(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

once()

public once(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

pause()

Pauses reading incoming data events.

public pause() : void

Removes the data source file descriptor from the event loop. This allows you to throttle incoming data.

Unless otherwise noted, a successfully opened stream SHOULD NOT start in paused state.

Once the stream is paused, no futher data or end events SHOULD be emitted.

$stream->pause();

$stream->on('data', assertShouldNeverCalled());
$stream->on('end', assertShouldNeverCalled());

This method is advisory-only, though generally not recommended, the stream MAY continue emitting data events.

You can continue processing events by calling resume() again.

Note that both methods can be called any number of times, in particular calling pause() more than once SHOULD NOT have any effect.

pipe()

Pipes all the data from this readable source into the given writable destination.

public pipe(WritableStreamInterface $dest[, array<string|int, mixed> $options = array() ]) : WritableStreamInterface

Automatically sends all incoming data to the destination. Automatically throttles the source based on what the destination can handle.

$source->pipe($dest);

Similarly, you can also pipe an instance implementing DuplexStreamInterface into itself in order to write back all the data that is received. This may be a useful feature for a TCP/IP echo service:

$connection->pipe($connection);

This method returns the destination stream as-is, which can be used to set up chains of piped streams:

$source->pipe($decodeGzip)->pipe($filterBadWords)->pipe($dest);

By default, this will call end() on the destination stream once the source stream emits an end event. This can be disabled like this:

$source->pipe($dest, array('end' => false));

Note that this only applies to the end event. If an error or explicit close event happens on the source stream, you'll have to manually close the destination stream:

$source->pipe($dest);
$source->on('close', function () use ($dest) {
    $dest->end('BYE!');
});

If the source stream is not readable (closed state), then this is a NO-OP.

$source->close();
$source->pipe($dest); // NO-OP

If the destinantion stream is not writable (closed state), then this will simply throttle (pause) the source stream:

$dest->close();
$source->pipe($dest); // calls $source->pause()

Similarly, if the destination stream is closed while the pipe is still active, it will also throttle (pause) the source stream:

$source->pipe($dest);
$dest->close(); // calls $source->pause()

Once the pipe is set up successfully, the destination stream MUST emit a pipe event with this source stream an event argument.

Parameters
$dest : WritableStreamInterface
$options : array<string|int, mixed> = array()
Return values
WritableStreamInterface

$dest stream as-is

removeAllListeners()

public removeAllListeners([mixed $event = null ]) : mixed
Parameters
$event : mixed = null

removeListener()

public removeListener(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

resume()

Resumes reading incoming data events.

public resume() : void

Re-attach the data source after a previous pause().

$stream->pause();

Loop::addTimer(1.0, function () use ($stream) {
    $stream->resume();
});

Note that both methods can be called any number of times, in particular calling resume() without a prior pause() SHOULD NOT have any effect.

isLegacyPipe()

Returns whether this is a pipe resource in a legacy environment

private isLegacyPipe(resource $resource) : bool

This works around a legacy PHP bug (#61019) that was fixed in PHP 5.4.28+ and PHP 5.5.12+ and newer.

Parameters
$resource : resource
Tags
link
https://github.com/reactphp/child-process/issues/40
codeCoverageIgnore
Return values
bool

        
On this page

Search results