phpactor/amp-fswatch

Async Filesystem Watcher for Amphp

dev-master / 0.1.x-dev 2020-03-29 17:18 UTC

This package is auto-updated.

Last update: 2020-03-29 17:18:38 UTC


README

Build Status

This is an Amp library for asynchronously monitor paths on your file system changes using various stategues.

It's been created to trigger code indexing in Phpactor.

Usage

In general:

use Amp\Loop;
use Phpactor\AmpFsWatch\ModifiedFile;
use Phpactor\AmpFsWatch\Watcher\Fallback\FallbackWatcher;
use Phpactor\AmpFsWatch\Watcher\Find\FindWatcher;
use Phpactor\AmpFsWatch\Watcher\FsWatch\FsWatchWatcher;
use Phpactor\AmpFsWatch\Watcher\Inotify\InotifyWatcher;

Loop::run(function () use () {
    $watcher = new FallbackWatcher([
        new InotifyWatcher(),
        new FsWatchWatcher(),
        new FindWatcher(),
    ], $logger);

    $process = yield $watcher->watch([
        'src',
    ]);

    while (null !== $file = yield $process->wait()) {
        fwrite(STDOUT, sprintf('%s (%s)', $file->path(), $file->type()));
    }
});

Inotify

Use the Linux inotifywait binary to monitor for changes.

use Phpactor\AmpFsWatch\Watcher\Inotify\InotifyWatcher;

$watcher = new InotifyWatcher($logger);
// ...

Fswatch

FsWatch is a cross-platform (Linux,Mac,Windows) file watching utility which will automatically use the platforms native functionality when possible.

use Phpactor\AmpFsWatch\Watcher\FsWatch\FsWatchWatcher;

$watcher = new FsWatchWatcher($logger);
// ...

Find

Use the POSIX find binary (Linux and Mac) to poll for file changes.

Poll for changes every second:

use Phpactor\AmpFsWatch\Watcher\Find\FindWatcher;

$watcher = new FindWatcher(1000, $logger);
// ...

Arguments:

  • 0: Milliseconds between polls

Fallback

The fallback watcher will automatically select the first supported watcher on the current system:

use Phpactor\AmpFsWatch\Watcher\Fallback\FallbackWatcher;

$watcher = new FallbackWatcher(
    [
        new InotifyWatcher($logger),
        new FindWatcher(500, $logger)
    ]
    $logger
);
// ...