Skip to content

Instantly share code, notes, and snippets.

@petsoukos

petsoukos/.php Secret

Last active September 27, 2019 14:00
Show Gist options
  • Save petsoukos/2cc5f7cc3181efbaab12d43fbe325644 to your computer and use it in GitHub Desktop.
Save petsoukos/2cc5f7cc3181efbaab12d43fbe325644 to your computer and use it in GitHub Desktop.
Sample for issue reactphp/filesystem
<?php
require __DIR__ . '/vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
$file_targets = [
'file1.xml',
'file2.xml',
'file3.xml',
'file4.xml',
'file5.xml',
'file6.xml',
'file7.xml',
'file8.xml',
'file9.xml',
'file10.xml'
];
foreach($file_targets as $index => $target)
{
$index += 1;
$file = $filesystem->file( __DIR__ . DS . 'downloads' . DS . $target );
$stream = \React\Promise\Stream\unwrapReadable( $file->open('r') );
$buffer = '';
$stream->on('data', function($data) use (&$buffer, $index, $target) {
$buffer .= $data;
echo str_repeat("\033[1A", $index), "Bytes loaded for file {$target}: ", strlen($buffer), str_repeat(PHP_EOL, $index);
});
$stream->on('error', function(Exception $e) use ($target, $index) {
echo str_repeat("\033[1A", $index), "Opening {$target} produced some error: ", $e->getMessage(), str_repeat(PHP_EOL, $index);
});
}
echo str_repeat(PHP_EOL, count($file_targets));
$loop->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment