A simple directory and file watcher that was made using PHP.
- PHP >= 8.1
composer require cydrickn/php-watcher
To use this package you just need to initialize the watcher and call the tick function
<?php
require_once './vendor/autoload.php';
use \Cydrickn\PHPWatcher\Adapters\SwooleTimerAdapter;
$watcher = new \Cydrickn\PHPWatcher\Watcher(
new SwooleTimerAdapter(),
[__DIR__],
[__DIR__ . '/vendor/'],
function (array $changes) {
echo json_encode($changes) . PHP_EOL;
}
);
$watcher->tick();
Currently, we have 3 premade adapters:
If you are using Swoole/Openswoole then best choice to use this adapter
If not using Swoole/Openswoole then you can go for this one, it uses PHP's do-while and sleep
This is same as the version 1 where it will automatically use the SwooleTimerAdapter, if available or fallback to NativeAdapter. This might be useful in a mixed environment.
If you don't want to use any pre-made adapter you can create your own adapter. Example:
<?php
namespace Your\Namespace;
use Cydrickn\PHPWatcher\Adapters\AdapterInterface;
class YourAdapter implements AdapterInterface
{
public function tick(callable $handler, int $interval = 1000)
{
// Implement your own logic
// Also make sure to call the handler to trigger the checking of changed files
// e.g. call_user_func($handler);
}
}