innmind/stream

Wrapper to PHP stream functions

2.0.0 2019-12-13 21:04 UTC

This package is auto-updated.

Last update: 2020-02-13 21:27:12 UTC


README

develop
codecov
Build Status

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.