Evented filesystem access utilizing EIO.
Filesystem WIP for EIO, keep in mind that this can be very unstable at times and is not stable by a long shot!
- ChildProcessAdapter - Adapter using child processes to perform IO actions (default adapter if no extensions are installed)
- EioAdapter - Adapter using
ext-eio
Adding examples here over time.
<?php
$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
<?php
$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
$file = $filesystem->file(__FILE__); // Returns a \React\Filesystem\Node\FileInterface compatible object
$filesystem->getContents('test.txt')->then(function($contents) {
});
Which is a convenience method for:
$filesystem->open('test.txt')->then(function($stream) {
return React\Stream\BufferedSink::createPromise($stream);
})->then(function($contents) {
});
Which in it's turn is a convenience method for:
$filesystem->file('test.txt')->open('r')->then(function ($stream) use ($node) {
$buffer = '';
$deferred = new \React\Promise\Deferred();
$stream->on('data', function ($data) use (&$buffer) {
$buffer += $data;
});
$stream->on('end', function ($data) use ($stream, $deferred, &$buffer) {
$stream->close();
$deferred->resolve(&$buffer);
});
return $deferred->promise();
});
Open a file for writing (w
flag) and write abcde
to test.txt
and close it. Create it (c
flag) when it doesn't exists and truncate it (t
flag) when it does.
$filesystem->file('test.txt')->open('cwt')->then(function ($stream) {
$stream->write('a');
$stream->write('b');
$stream->write('c');
$stream->write('d');
$stream->end('e');
});
<?php
$loop = \React\EventLoop\Factory::create();
$filesystem = \React\Filesystem\Filesystem::create($loop);
$dir = $filesystem->dir(__DIR__); // Returns a \React\Filesystem\Node\DirectoryInterface compatible object
$filesystem->dir(__DIR__)->ls()->then(function (\SplObjectStorage $list) {
foreach ($list as $node) {
echo $node->getPath(), PHP_EOL;
}
});
React/Filesystem is released under the MIT license.