sblazheev / filesystem
Asynchronous filesystem abstraction.
Requires
- php: >=5.4.0
- evenement/evenement: ^3.0 || ^2.0
- react/event-loop: ^1.0 || ^0.5 || ^0.4
- react/promise: ~2.2
- react/promise-stream: ^1.1
- react/stream: ^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4
- wyrihaximus/react-child-process-pool: ^1.3
Requires (Dev)
- clue/block-react: ^1.1
- phpunit/phpunit: ^6.0 || ^5.0 || ^4.8
Suggests
- ext-eio: ^1.2
This package is auto-updated.
Last update: 2025-03-25 08:45:52 UTC
README
ReactPHP's evented asynchronous, non-blocking filesystem access library.
Table of Contents
Introduction
react/filesystem
is a package to power your application with asynchronous, non-blocking filesystem access. Asynchronous access is enabled by various adapters described below.
Adapters
- ChildProcessAdapter - Adapter using child processes to perform IO actions (default adapter if no extensions are installed)
- EioAdapter - Adapter using
ext-eio
Examples
Adding examples here over time.
Creating filesystem object
<?php $loop = \React\EventLoop\Factory::create(); $filesystem = \React\Filesystem\Filesystem::create($loop);
File object
<?php $loop = \React\EventLoop\Factory::create(); $filesystem = \React\Filesystem\Filesystem::create($loop); $file = $filesystem->file(__FILE__); // Returns a \React\Filesystem\Node\FileInterface compatible object
Reading files
$filesystem->getContents('test.txt')->then(function($contents) { });
Which is a convenience method for:
$filesystem->file('test.txt')->open('r')->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(); });
Writing files
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'); });
Directory object
<?php $loop = \React\EventLoop\Factory::create(); $filesystem = \React\Filesystem\Filesystem::create($loop); $dir = $filesystem->dir(__DIR__); // Returns a \React\Filesystem\Node\DirectoryInterface compatible object
List contents
$filesystem->dir(__DIR__)->ls()->then(function (array $list) { foreach ($list as $node) { echo $node->getPath(), PHP_EOL; } });
Install
The recommended way to install this library is through Composer. New to Composer?
This will install the latest supported version:
$ composer require react/filesystem:^0.1.1
License
React/Filesystem is released under the MIT license.