|
| 1 | +<?php |
| 2 | +// this example shows how the containerCopy() call returns a TAR stream, |
| 3 | +// how it can be passed to a TAR decoder and how we can then pipe each |
| 4 | +// individual file to the console output. |
| 5 | + |
| 6 | +require __DIR__ . '/../vendor/autoload.php'; |
| 7 | + |
| 8 | +use React\EventLoop\Factory as LoopFactory; |
| 9 | +use Clue\React\Docker\Factory; |
| 10 | +use Clue\React\Tar\Decoder; |
| 11 | +use React\Stream\ReadableStreamInterface; |
| 12 | + |
| 13 | +$container = isset($argv[1]) ? $argv[1] : 'asd'; |
| 14 | +$file = isset($argv[2]) ? $argv[2] : '/etc/passwd'; |
| 15 | +echo 'Container "' . $container . '" dumping "' . $file . '" (pass as arguments to this example)' . PHP_EOL; |
| 16 | + |
| 17 | +$loop = LoopFactory::create(); |
| 18 | + |
| 19 | +$factory = new Factory($loop); |
| 20 | +$client = $factory->createClient(); |
| 21 | + |
| 22 | +$stream = $client->containerCopyStream($container, array('Resource' => $file)); |
| 23 | + |
| 24 | +$tar = new Decoder(); |
| 25 | + |
| 26 | +$tar->on('entry', function ($header, ReadableStreamInterface $file) { |
| 27 | + // write each entry to the console output |
| 28 | + echo '########## ' . $header['filename'] . ' ##########' . PHP_EOL; |
| 29 | + $file->on('data', function ($chunk) { |
| 30 | + echo $chunk; |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +$tar->on('error', function ($e = null) { |
| 35 | + // should not be invoked, unless the stream is somehow interrupted |
| 36 | + echo 'ERROR processing tar stream' . PHP_EOL . $e; |
| 37 | +}); |
| 38 | +$stream->on('error', function ($e = null) { |
| 39 | + // will be called if either parameter is invalid |
| 40 | + echo 'ERROR requesting stream' . PHP_EOL . $e; |
| 41 | +}); |
| 42 | + |
| 43 | +$stream->pipe($tar); |
| 44 | + |
| 45 | +$loop->run(); |
0 commit comments