Decoder
extends EventEmitter
in package
implements
ReadableStreamInterface
The Decoder / Parser reads from a plain stream and emits data objects for each JSON element
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
- $assoc : mixed
- $buffer : mixed
- $closed : mixed
- $depth : mixed
- $input : mixed
- $maxlength : int
- $options : mixed
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.
Properties
$listeners
protected
mixed
$listeners
= []
$onceListeners
protected
mixed
$onceListeners
= []
$assoc
private
mixed
$assoc
$buffer
private
mixed
$buffer
= ''
$closed
private
mixed
$closed
= false
$depth
private
mixed
$depth
$input
private
mixed
$input
$maxlength
private
int
$maxlength
$options
private
mixed
$options
Methods
__construct()
public
__construct(ReadableStreamInterface $input[, bool $assoc = false ][, int $depth = 512 ][, int $options = 0 ][, int $maxlength = 65536 ]) : mixed
Parameters
- $input : ReadableStreamInterface
- $assoc : bool = false
- $depth : int = 512
- $options : int = 0
-
(requires PHP 5.4+)
- $maxlength : int = 65536
Tags
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
boollisteners()
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.