concerto/directory-monitor

Library for monitoring a directory for changes using Inotify.

Installs: 716

Dependents: 0

Suggesters: 0

Security: 0

Stars: 5

Watchers: 3

Forks: 1

Type:concerto-component

v0.1 2014-05-02 10:56 UTC

This package is not auto-updated.

Last update: 2020-08-07 17:58:27 UTC


README

Library for monitoring a directory for changes using Inotify.

Build Status

Install

The recommended way to install Directory Monitor is through composer.

{
    "require": {
        "concerto/directory-monitor": "0.*"
    }
}

Usage

Create an instance of RecursiveMonitor and listen to the events you need:

use Concerto\DirectoryMonitor\RecursiveMonitor;
use React\EventLoop\Factory as EventLoopFactory;

$loop = EventLoopFactory::create();
$monitor = new RecursiveMonitor($loop, __DIR__);

// Fired on any Inotify event:
$monitor->on('notice', function($path, $root) {
	echo "Notice: {$path} in {$root}\n";
});

$monitor->on('create', function($path, $root) {
	echo "Created: {$path} in {$root}\n";
});

$monitor->on('delete', function($path, $root) {
	echo "Deleted: {$path} in {$root}\n";
});

$monitor->on('modify', function($path, $root) {
	echo "Modified: {$path} in {$root}\n";
});

$monitor->on('write', function($path, $root) {
	echo "Wrote: {$path} in {$root}\n";
});

$loop->run();

You can also ignore files using regular expressions:

// Ignore hidden files:
$monitor->ignore('/\.');

// Ignore the temp folder:
$monitor->ignore('^/tmp');

// Ignore all .cache files:
$monitor->ignore('\.cache$');

And you can notice previously ignored files:

// Notice compiled templates:
$monitor->notice('^/tmp/templates');

The expression is applied to the full path of each item relative to the root directory being monitored. For example, if your directory is /root/path and an event was caused by /root/path/some/file then the expression would be executed against the path /some/file.