slick/fswatch
is a simple library that sums the total size of all files in a given directory allowing you to verify if its contents have changed.
This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification.
Via Composer
composer require slick/fschange
First you need to create a directory snapshot to be able to compare it with any other changes later on:
use Slick\FsWatch\Directory;
$dir = new Directory('/path/to/directory');
// can be stored in any cache or temporary memory to be checked later
$snapshot = $dir->snapshot();
file_put_contents('/some/cache/file', serialize($snapshot));
Now you can verify if directory contents have changed:
use Slick\FsWatch\Directory;
$dir = new Directory('/path/to/directory');
$snapshot = unserialize(file_get_contents('/some/cache/file'))
if ($dir->hasChanged($snapshot)) { //directory contents have changed
$changes = $dir->snapshot()->compareTo($snapshot);
// do your logic
}
Using the same principle above, you can have a daemon like script that will execute a given callback whenever a file changes or is added to a given directory, recursively.
use Slick\FsWatch\Directory;
use Slick\FsWatch\Watcher;
$dir = new Directory('/path/to/directory');
$watcher = new Watcher($dir, function (Directory $dir) => {
// do your logic on file change
});
// This will run until Ctrl + C (SIGINT) is pressed.
// You can pass an expression of callable to determine the end of the execution
$watcher->watch(Watcher::SIGINT);
phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email slick.framework@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.