innmind/stream

Wrapper to PHP stream functions

2.1.1 2020-06-16 18:53 UTC

This package is auto-updated.

Last update: 2020-09-18 15:38:01 UTC


README

Build Status codecov Type Coverage

Simple wrapper to work with resource streams.

Installation

composer require innmind/stream

Usage

File handling:

use Innmind\Stream\Readable\Stream;
use Innmind\Url\Path;

$file = Stream::open(Path::of('/some/path/to/a/file'));

while (!$file->end()) {
    echo $file->readLine()->toString();
}

$file->close();

Socket handling:

use Innmind\Stream\{
    Stream\Bidirectional,
    Watch\Select,
};
use Innmind\TimeContinuum\Earth\ElapsedPeriod;

$socket = new Bidirectional(stream_socket_client('unix:///path/to/socket.sock'));
$select = (new Select(new ElapsedPeriod(60 * 1000))) //select with a 1 minute timeout
    ->forRead($socket);

do {
    $ready = $select();

    if ($ready->toRead()->contains($socket)) {
        $socket->write(
            $socket->read()->toUpper()
        );
    }
} while (true);

This example will listen for messages sent from the socket unix:///path/to/socket.sock and will send it back in upper case.