slick/fswatch

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.

v0.1.0 2024-05-07 00:38 UTC

This package is auto-updated.

Last update: 2024-12-17 18:27:00 UTC


README

Latest Version Software License Build Status Quality Score Contributor Covenant Total Downloads

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.

Install

Via Composer

composer require slick/fswatch

Usage

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 as a watcher

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);

Testing

phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email slick.framework@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.