2017 © Pedro Peláez
 

library stream

Wrapper to PHP stream functions

image

innmind/stream

Wrapper to PHP stream functions

  • Saturday, March 3, 2018
  • by Baptouuuu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,446 Installations
  • PHP
  • 9 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 12 Versions
  • 13 % Grown

The README.md

Stream

Build Status codecov Type Coverage, (*1)

Simple wrapper to work with resource streams., (*2)

Installation

composer require innmind/stream

Usage

File handling:, (*3)

use Innmind\Stream\Streams;
use Innmind\Url\Path;

$file = Streams::of()->readable()->open(Path::of('/some/path/to/a/file'));

while (!$file->end()) {
    echo $file->readLine()->match(
        static fn($line) => $line->toString(),
        static fn() => throw new \Exception('failed to read the stream'),
    );
}

$file->close()->match(
    static fn() => null, // closed correctly
    static fn() => throw new \Exception('failed to close the stream'),
);

Socket handling:, (*4)

use Innmind\Stream\{
    Stream\Bidirectional,
    Streams,
};
use Innmind\TimeContinuum\Earth\ElapsedPeriod;
use Innmind\Immutable\Either;

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

do {
    $socket = $select()
        ->filter(static fn($ready) => $ready->toRead()->contains($socket))
        ->flatMap(static fn() => $socket->read())
        ->map(static fn($data) => $data->toUpper())
        ->match(
            static fn($data) => $socket->write($data),
            static fn() => Either::right($socket), // no data to send
        )
        ->match(
            static fn($socket) => $socket, // data sent back
            static fn($error) => throw new \Exception(\get_class($error)),
        );
} 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., (*5)

The Versions

03/03 2018

dev-master

9999999-dev http://github.com/Innmind/Stream

Wrapper to PHP stream functions

  Sources   Download

MIT

The Requires

 

The Development Requires

stream

03/03 2018
03/03 2018

dev-develop

dev-develop http://github.com/Innmind/Stream

Wrapper to PHP stream functions

  Sources   Download

MIT

The Requires

 

The Development Requires

stream

17/02 2018
17/02 2018
17/09 2017
31/07 2017
23/07 2017
22/07 2017
22/07 2017
22/07 2017
22/07 2017