public function open() { $this->opening = true; $file = $this->filesystem->file($this->getRandomFilename()); return $file->exists()->then(function () { return $this->open(); }, function () use($file) { return $file->create()->open('cw')->then(function ($stream) { $this->open = true; $this->opening = false; $this->stream = $stream; }); }); }
public function testCreateNameNParentFromFilename() { $node = new File('/foo/bar/baz/rabbit/kitten/index.php', Filesystem::createFromAdapter($this->getMock('React\\Filesystem\\Eio\\Adapter', [], [$this->getMock('React\\EventLoop\\StreamSelectLoop')]))); foreach ([['index.php', '/foo/bar/baz/rabbit/kitten/index.php'], ['kitten', '/foo/bar/baz/rabbit/kitten/'], ['rabbit', '/foo/bar/baz/rabbit/'], ['baz', '/foo/bar/baz/'], ['bar', '/foo/bar/'], ['foo', '/foo/'], ['', '/']] as $names) { $this->assertSame($names[0], $node->getName()); $this->assertSame($names[1], $node->getPath()); $node = $node->getParent(); } }
public function testSetFilesystemAndInvoker() { $loop = $this->getMock('React\\EventLoop\\StreamSelectLoop'); $adapter = $this->getMock('React\\Filesystem\\Eio\\Adapter', [], [$loop]); $invoker = new InstantInvoker($adapter); $adapter->expects($this->at(0))->method('setFilesystem')->with($this->isInstanceOf('React\\Filesystem\\Filesystem')); $adapter->expects($this->at(1))->method('setInvoker')->with($invoker); Filesystem::createFromAdapter($adapter)->setInvoker($invoker); }
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 __invoke(Request $request, Response $response) { if ($request->getPath() == self::SSE_PATH) { $this->handleSse($request, $response); return; } if ($request->getPath() == self::LOOKED_PATH) { $this->handleLookup($response, $request->getQuery()['host']); return; } $this->files->then(function (\SplObjectStorage $files) use($request, $response) { foreach ($files as $file) { if ($file->getPath() == WEBROOT . $request->getPath()) { $this->handleFile($file, $response); return; } } $this->handleFile($this->filesystem->file(WEBROOT . DIRECTORY_SEPARATOR . '404.txt'), $response); return; }); }
public function testDetectUnknown() { $callbackFired = false; $adapter = $this->getMock('React\\Filesystem\\Eio\\Adapter', ['stat'], [$this->getMock('React\\EventLoop\\StreamSelectLoop')]); $adapter->expects($this->any())->method('stat')->with('foo.bar')->will($this->returnValue(new FulfilledPromise(['mode' => 0x3000]))); $filesystem = Filesystem::createFromAdapter($adapter); (new ModeTypeDetector($filesystem))->detect(['path' => 'foo.bar'])->otherwise(function ($result) use(&$callbackFired) { $this->assertSame(null, $result); $callbackFired = true; }); $this->assertTrue($callbackFired); }
public function testExecute() { $filesystem = $this->getMock('React\\Filesystem\\Eio\\Adapter', [], [$this->getMock('React\\EventLoop\\LoopInterface')]); $node = $this->getMock('React\\Filesystem\\Node\\Directory', ['ls', 'chmod'], ['foo.bar', Filesystem::createFromAdapter($filesystem)]); $promise = $this->getMock('React\\Promise\\PromiseInterface'); $node->expects($this->once())->method('ls')->with()->will($this->returnValue($promise)); $fileDent = $this->getMock('React\\Filesystem\\Node\\File', ['chmod'], ['foo', Filesystem::createFromAdapter($filesystem)]); $node->expects($this->once())->method('chmod')->with(123)->will($this->returnValue(new FulfilledPromise())); $directoryDent = $this->getMock('React\\Filesystem\\Node\\Directory', ['chmodRecursive'], ['foo', Filesystem::createFromAdapter($filesystem)]); $directoryDent->expects($this->once())->method('chmodRecursive')->with(123)->will($this->returnValue(new FulfilledPromise())); $finalPromise = $this->getMock('React\\Promise\\PromiseInterface'); $node->expects($this->once())->method('chmod')->with(123)->will($this->returnValue($finalPromise)); $dents = [$fileDent, $directoryDent]; $promise->expects($this->once())->method('then')->with($this->isType('callable'))->will($this->returnCallback(function ($resolveCb) use($dents) { return $resolveCb($dents); })); $this->assertInstanceOf('React\\Promise\\PromiseInterface', (new RecursiveInvoker($node))->execute('chmod', [123])); }
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(__DIR__)->stat()->then(function ($data) { foreach ($data as $key => $value) { echo $key, ': ', $value, 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 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();
<?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();
<?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 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(); $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();
public function testCopyStreamingABC() { $adapter = $this->getMock('React\\Filesystem\\Eio\\Adapter', ['stat', 'mkdir'], [$this->getMock('React\\EventLoop\\StreamSelectLoop')]); $filesystem = Filesystem::createFromAdapter($adapter); $adapter->expects($this->at(0))->method('stat')->with('bar.foo/foo.bar/')->will($this->returnValue(new RejectedPromise())); $adapter->expects($this->at(1))->method('stat')->with('bar.foo/')->will($this->returnValue(new FulfilledPromise())); $adapter->expects($this->at(3))->method('stat')->with('bar.foo/foo.bar/')->will($this->returnValue(new FulfilledPromise())); $adapter->expects($this->any())->method('mkdir')->with($this->isType('string'))->will($this->returnValue(new FulfilledPromise())); $directoryFrom = $this->getMock('React\\Filesystem\\Node\\Directory', ['lsStreaming'], ['foo.bar', $filesystem]); $stream = new ObjectStream(); $directoryTo = new Directory('bar.foo', $filesystem); $directoryFrom->expects($this->once())->method('lsStreaming')->with()->will($this->returnValue($stream)); $returnedStream = $directoryFrom->copyStreaming($directoryTo); $this->assertInstanceOf('React\\Filesystem\\ObjectStream', $returnedStream); $file = $this->getMock('React\\Filesystem\\Node\\File', ['copyStreaming'], ['foo.bar', $filesystem]); $fileStream = new ObjectStream(); $file->expects($this->once())->method('copyStreaming')->with($directoryTo)->will($this->returnValue($fileStream)); $stream->emit('data', [$file]); $directory = $this->getMock('React\\Filesystem\\Node\\Directory', ['copyStreaming'], ['foo.bar', $filesystem]); $directoryStream = new ObjectStream(); $directory->expects($this->once())->method('copyStreaming')->with($this->isInstanceOf('React\\Filesystem\\Node\\Directory'))->will($this->returnValue($directoryStream)); $stream->emit('data', [$directory]); $directoryStream->end($directory); $stream->end(); }
/** * @dataProvider providerToString */ public function test__toString($in, $out) { $nodeClass = $this->getNodeClass(); $this->assertSame($out, (string) new $nodeClass($in, Filesystem::createFromAdapter($this->getMock('React\\Filesystem\\Eio\\Adapter', [], [$this->getMock('React\\EventLoop\\StreamSelectLoop')])))); }
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();
<?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();
<?php /** * The package league/flysystem-webdav is required for this example */ use League\Flysystem\Filesystem as Flysystem; use League\Flysystem\WebDAV\WebDAVAdapter; use React\EventLoop\Factory; use React\Filesystem\Filesystem; use Sabre\DAV\Client; use WyriHaximus\React\Filesystem\Flysystem\FlysystemAdapter; require dirname(__DIR__) . '/vendor/autoload.php'; $loop = Factory::create(); $loop->futureTick(function () use($loop) { $adapter = new WebDAVAdapter(new Client(['baseUri' => 'https://YOURSTACK.stackstorage.com/remote.php/webdav', 'userName' => 'USERNAME', 'password' => 'PASSWORD'])); $adapter->setPathPrefix('/remote.php/webdav'); $flysystem = new Flysystem($adapter); $filesystem = Filesystem::createFromAdapter(new FlysystemAdapter($loop, [], $flysystem)); $filesystem->dir('')->ls()->then(function ($nodes) { foreach ($nodes as $node) { echo $node->getPath(), PHP_EOL; } }, function ($e) { var_export($e); }); }); $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 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();
/** * @expectedException UnexpectedValueException */ public function testCopyStreamingUnknownNode() { $filesystem = $this->getMock('React\\Filesystem\\Eio\\Adapter', [], [$this->getMock('React\\EventLoop\\StreamSelectLoop')]); (new File('foo.bar', Filesystem::createFromAdapter($filesystem)))->copyStreaming(new UnknownNodeType()); }
<?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();