0 follower

Final Class Yiisoft\VarDumper\Handler\StreamHandler

InheritanceYiisoft\VarDumper\Handler\StreamHandler
ImplementsYiisoft\VarDumper\HandlerInterface

Uses stream ({@link https://www.php.net/manual/en/intro.stream.php}) for writing variable's data. Requires "sockets" PHP extension when {@see StreamHandler::$uri} is a {@see Socket} instance.

Constants

Hide inherited constants

Constant Value Description Defined By
SOCKET_PROTOCOLS [ 'udp', 'udg', 'tcp', 'unix', ] Yiisoft\VarDumper\Handler\StreamHandler

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( mixed|resource|string $uri 'udp://127.0.0.1:8890' )
$uri mixed|resource|string

                public function __construct(
    mixed $uri = 'udp://127.0.0.1:8890'
) {
    if (!is_string($uri) && !is_resource($uri) && !$uri instanceof Socket) {
        throw new InvalidArgumentException(
            sprintf(
                'Argument $uri must be either a string, a resource or a Socket instance, "%s" given.',
                get_debug_type($uri)
            )
        );
    }
    $this->uri = $uri;
}

            
__destruct() public method

public mixed __destruct ( )

                public function __destruct()
{
    if (!is_string($this->uri) || !is_resource($this->stream)) {
        return;
    }
    fclose($this->stream);
}

            
handle() public method

Encodes {@param $variable} with {@see self::$encoder} and sends the result to the stream.

public void handle ( mixed $variable, integer $depth, boolean $highlight false )
$variable mixed
$depth integer
$highlight boolean

                public function handle(mixed $variable, int $depth, bool $highlight = false): void
{
    $data = ($this->encoder ?? '\json_encode')($variable);
    if (!is_string($data)) {
        throw new RuntimeException(
            sprintf(
                'Encoder must return a string, "%s" returned.',
                get_debug_type($data)
            )
        );
    }
    if (!is_resource($this->stream) && !$this->stream instanceof Socket) {
        $this->initializeStream();
    }
    if (!$this->writeToStream($data)) {
        $this->initializeStream();
        if (!$this->writeToStream($data)) {
            throw new RuntimeException('Cannot write a stream.');
        }
    }
}

            
withEncoder() public method

public Yiisoft\VarDumper\Handler\StreamHandler withEncoder ( callable $encoder )
$encoder callable

Encoder that will be used to encode variable before sending it to the stream.

                public function withEncoder(callable $encoder): static
{
    $new = clone $this;
    $new->encoder = $encoder;
    return $new;
}