public function testDetectUnknown() { $callbackFired = false; $filesystem = Filesystem::create($this->getMock('React\\EventLoop\\StreamSelectLoop')); (new ConstTypeDetector($filesystem))->detect(['type' => 123])->otherwise(function ($result) use(&$callbackFired) { $this->assertSame(null, $result); $callbackFired = true; }); $this->assertTrue($callbackFired); }
public function testGetContents() { $this->assertInstanceOf('React\\Promise\\PromiseInterface', Filesystem::create($this->getMock('React\\EventLoop\\StreamSelectLoop'))->getContents('foo.bar')); }
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->dir(__DIR__)->ls()->then(function (\SplObjectStorage $list) { foreach ($list as $node) { echo $node->getPath(), PHP_EOL; } }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require 'vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); $dir = \React\Filesystem\Filesystem::create($loop)->dir(dirname(__DIR__)); $dir->lsRecursive()->then(function (\SplObjectStorage $list) { $phpFiles = new RegexIterator($list, '/.*?.php$/'); foreach ($phpFiles as $node) { if (strpos($node->getPath(), 'vendor') !== false) { continue; } echo $node->getPath(), PHP_EOL; } }); $loop->run();
define('WEBROOT', __DIR__ . DIRECTORY_SEPARATOR . 'webroot'); use Clue\React\Sse\BufferedChannel; use GuzzleHttp\Client; use League\Event\Emitter; use React\EventLoop\Factory; use React\Filesystem\Filesystem; use React\Http\Server as HttpServer; use React\Socket\Server as SocketServer; use WyriHaximus\React\Examples\HostnameAnalyzer\Listeners\ChannelListener; use WyriHaximus\React\Examples\HostnameAnalyzer\Listeners\DnsListener; use WyriHaximus\React\Examples\HostnameAnalyzer\Listeners\GeoListener; use WyriHaximus\React\Examples\HostnameAnalyzer\Listeners\TitleListener; use WyriHaximus\React\Examples\HostnameAnalyzer\ResponseHandler; use WyriHaximus\React\RingPHP\HttpClientAdapter; require 'vendor/autoload.php'; $loop = Factory::create(); $socket = new SocketServer($loop); $http = new HttpServer($socket, $loop); $filesystem = Filesystem::create($loop); $dns = (new \React\Dns\Resolver\Factory())->createCached('8.8.8.8', $loop); $guzzle = new Client(['handler' => new HttpClientAdapter($loop, null, $dns)]); $channel = new BufferedChannel(); $emitter = new Emitter(); $emitter->useListenerProvider(new TitleListener($emitter, $guzzle)); $emitter->useListenerProvider(new DnsListener($emitter, $dns)); $emitter->useListenerProvider(new GeoListener($emitter, $guzzle)); $emitter->useListenerProvider(new ChannelListener($emitter, $channel)); $files = $filesystem->dir(WEBROOT)->ls(); $http->on('request', new ResponseHandler($files, $filesystem, $emitter, $channel)); $socket->listen(1337); $loop->run();
public function test_react_3() { $loop = \React\EventLoop\Factory::create(); $filesystem = \React\Filesystem\Filesystem::create($loop); }
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->dir(dirname(__DIR__))->lsRecursive()->then(function (\SplObjectStorage $list) { foreach ($list as $node) { echo $node->getPath(), PHP_EOL; } }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->dir(dirname(__DIR__))->size()->then(function ($size) { echo 'Directory "' . dirname(__DIR__) . '" contains ' . $size['directories'] . ' directories, ' . $size['files'] . ' files and is ' . $size['size'] . ' bytes in size', PHP_EOL; }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
function formatBytes($bytes, $precision = 2) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= 1 << 10 * $pow; return round($bytes, $precision) . ' ' . $units[$pow]; } use React\Filesystem\Node\File; use React\Filesystem\Node\NodeInterface; require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); $i = 0; $dir = \React\Filesystem\Filesystem::create($loop, ['open_file_limit' => 8])->dir(dirname(__DIR__)); $stream = $dir->lsRecursiveStreaming(); $stream->on('data', function (NodeInterface $node) use(&$i) { if ($node instanceof File) { $node->getContents()->then(function ($contents) use($node, &$i) { echo $node->getPath(), ': ', formatBytes(strlen($contents)), PHP_EOL; $i++; }, function ($e) { var_export($e->getMessage()); }); return; } echo $node->getPath(), PHP_EOL; $i++; }); $stream->on('end', function () use(&$i) {
<?php use React\Filesystem\Node\NodeInterface; require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); $filesystem = \React\Filesystem\Filesystem::create($loop); $from = $filesystem->dir(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'vendor'); $to = $filesystem->dir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'react_filesystem_file_to_file_copy' . DIRECTORY_SEPARATOR . uniqid()); echo 'From: ', $from->getPath(), PHP_EOL; echo 'To: ', $to->getPath(), PHP_EOL; $to->createRecursive()->then(function () use($from, $to) { $i = 0; $stream = $from->copyStreaming($to); $stream->on('data', function (NodeInterface $node) use(&$i) { echo $node->getPath(), PHP_EOL; $i++; }); $stream->on('end', function () use(&$i) { echo 'Copied ', $i, ' nodes', PHP_EOL; }); }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->file(__FILE__)->getContents()->then(function ($contents) { echo $contents, PHP_EOL; }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); $timesFunction = function ($times) { $nextLine = "\r\n\t"; echo 'File "' . __FILE__ . '":'; echo $nextLine; echo 'Access timestamp: ' . $times['atime']; echo $nextLine; echo 'Creation timestamp: ' . $times['ctime']; echo $nextLine; echo 'Modified timestamp: ' . $times['mtime']; echo "\r\n"; }; $file = \React\Filesystem\Filesystem::create($loop)->file(__FILE__); $file->time()->then($timesFunction)->then(function () use($file) { return $file->touch(); })->then(function () use($file) { return $file->time(); })->then($timesFunction)->then(null, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->file(__FILE__)->stat()->then(function ($data) { foreach ($data as $key => $value) { echo $key, ': ', $value, PHP_EOL; } }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); $fileName = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'react_filesystem_file_touch_' . uniqid(); $file = \React\Filesystem\Filesystem::create($loop)->file($fileName); $file->create()->then(function () use($file, $fileName) { echo 'File "' . $fileName . '" created.', PHP_EOL; return $file->stat(); })->then(function ($data) use($file) { echo 'stat data: ', PHP_EOL; foreach ($data as $key => $value) { echo "\t", $key, ': ', $value, PHP_EOL; } return $file->remove(); })->then(function () { echo 'File removed', PHP_EOL; echo 'Done!', PHP_EOL; }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
public function __construct(array $options) { $this->filesystem = Filesystem::create($options['loop']); }
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->file(__FILE__)->time()->then(function ($times) { $nextLine = "\r\n\t"; echo 'File "' . __FILE__ . '":'; echo $nextLine; echo 'Access timestamp: ' . $times['atime']; echo $nextLine; echo 'Creation timestamp: ' . $times['ctime']; echo $nextLine; echo 'Modified timestamp: ' . $times['mtime']; echo "\r\n"; }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->dir(__DIR__)->stat()->then(function ($data) { foreach ($data as $key => $value) { echo $key, ': ', $value, PHP_EOL; } }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();
<?php require dirname(__DIR__) . '/vendor/autoload.php'; $loop = \React\EventLoop\Factory::create(); \React\Filesystem\Filesystem::create($loop)->file(__FILE__)->size()->then(function ($size) { echo 'File "' . __FILE__ . '" is ' . $size . ' bytes', PHP_EOL; }, function ($e) { echo $e->getMessage(), PHP_EOL; }); $loop->run();