Example #1
0
<?php

use React\Stream\Stream;
use React\EventLoop\Factory;
use Clue\React\Tar\Decoder;
use React\Stream\BufferedSink;
use Clue\Hexdump\Hexdump;
use React\EventLoop\StreamSelectLoop;
require __DIR__ . '/../vendor/autoload.php';
$in = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/fixtures/alice-bob.tar';
echo 'Reading file "' . $in . '" (pass as argument to example)' . PHP_EOL;
// using the default loop does *not* work for file I/O
//$loop = Factory::create();
$loop = new StreamSelectLoop();
$stream = new Stream(fopen($in, 'r'), $loop);
$decoder = new Decoder();
$decoder->on('entry', function ($header, $file) {
    static $i = 0;
    echo 'FILE #' . ++$i . PHP_EOL;
    echo 'Received entry headers:' . PHP_EOL;
    var_dump($header);
    BufferedSink::createPromise($file)->then(function ($contents) {
        echo 'Received entry contents (' . strlen($contents) . ' bytes)' . PHP_EOL;
        $d = new Hexdump();
        echo $d->dump($contents) . PHP_EOL . PHP_EOL;
    });
});
$decoder->on('error', function ($error) {
    echo 'ERROR: ' . $error . PHP_EOL;
});
$decoder->on('close', function () {
Example #2
0
// how it can be passed to a TAR decoder and how we can then pipe each
// individual file to the console output.
require __DIR__ . '/../vendor/autoload.php';
use React\EventLoop\Factory as LoopFactory;
use Clue\React\Docker\Factory;
use Clue\React\Tar\Decoder;
use React\Stream\ReadableStreamInterface;
use Clue\CaretNotation\Encoder;
$container = isset($argv[1]) ? $argv[1] : 'asd';
$file = isset($argv[2]) ? $argv[2] : '/etc/passwd';
echo 'Container "' . $container . '" dumping "' . $file . '" (pass as arguments to this example)' . PHP_EOL;
$loop = LoopFactory::create();
$factory = new Factory($loop);
$client = $factory->createClient();
$stream = $client->containerCopyStream($container, array('Resource' => $file));
$tar = new Decoder();
// use caret notation for any control characters expect \t, \r and \n
$caret = new Encoder("\t\r\n");
$tar->on('entry', function ($header, ReadableStreamInterface $file) use($caret) {
    // write each entry to the console output
    echo '########## ' . $caret->encode($header['filename']) . ' ##########' . PHP_EOL;
    $file->on('data', function ($chunk) use($caret) {
        echo $caret->encode($chunk);
    });
});
$tar->on('error', function ($e = null) {
    // should not be invoked, unless the stream is somehow interrupted
    echo 'ERROR processing tar stream' . PHP_EOL . $e;
});
$stream->on('error', function ($e = null) {
    // will be called if either parameter is invalid