phprtc / watcher
Swoole File Watcher
0.1.3
2023-03-23 12:53 UTC
Requires
- php: ^8.1
- ext-inotify: *
- evenement/evenement: ^3.0
Requires (Dev)
- openswoole/ide-helper: ^4.11
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
- symfony/var-dumper: ^6.2
This package is auto-updated.
Last update: 2024-10-23 16:25:42 UTC
README
PHP-based file system changes watcher implemented using Swoole & Inotify.
Installation
composer require phprtc/watcher ^0.1 --dev
Usage
Basic Usage
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; require 'vendor/autoload.php'; Watcher::create() ->addPath(__DIR__ . '/app') ->addPath(__DIR__ . '/views') ->onChange(function (EventInfo $eventInfo) { echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL; }) ->watch();
Any Event
Listens to any event on given path
Be careful using this method.
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; require 'vendor/autoload.php'; Watcher::create() ->addPath(__DIR__ . '/app') ->onAny(function (EventInfo $eventInfo) { echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n"; }) ->watch();
Ignoring Path
Ignore files using regular expression
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; require 'vendor/autoload.php'; Watcher::create() ->addPath(__DIR__ . '/app') ->ignore(__DIR__ . '/test1/t/*') // Ignore files in "/test1/t/" ->ignore([ __DIR__ . '/test1/t/.*(\.php$)', // Ignore files that end with "php" in "/test1/t/" __DIR__ . '/test1/t/.*(\.js)', // Ignore files that end with "js" in "/test1/t/" ]) ->onChange(function (EventInfo $eventInfo) { echo date('H:i:s') . " - {$eventInfo->getName()} {$eventInfo->getWatchedItem()->getFullPath()}\n"; }) ->watch();
Filter
-
Make sure that the file whose event is being fired should not end with provided characters.
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; require 'vendor/autoload.php'; Watcher::create() ->addPath(__DIR__ . '/app') ->fileShouldNotEndWith(['.php']) ->onChange(function (EventInfo $eventInfo) { echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL; }) ->watch();
-
Only listen to event with file name that matches given extension(s).
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; require 'vendor/autoload.php'; Watcher::create() ->addPath(__DIR__ . '/app') ->addExtension('php') ->onChange(function (EventInfo $eventInfo) { echo $eventInfo->getWatchedItem()->getFullPath() . PHP_EOL; }) ->watch();
Stopping Watcher
use RTC\Watcher\Watcher; use RTC\Watcher\Watching\EventInfo; use Swoole\Timer; require 'vendor/autoload.php'; $watcher = Watcher::create() ->addPath(__DIR__) ->onCreate(function (EventInfo $eventInfo) { echo date('H:i:s') . ": CREATED - {$eventInfo->getWatchedItem()->getFullPath()}\n"; }); Timer::after(1000, fn() => $watcher->stop()); // Stop watching after 1 second $watcher->start(); touch(__DIR__ . '/auto-created.txt'); unlink(__DIR__ . '/auto-created.txt');
Swoole Server Integration
use Swoole\Http\Request; use Swoole\Http\Response; use Swoole\Http\Server; use RTC\Watcher\Watcher; require 'vendor/autoload.php'; $server = new Server('0.0.0.0', 9000); $server->on('request', function (Request $request, Response $response) { $response->end('Hello world'); }); $server->on('start', function (Server $server) { echo "Server started at http://0.0.0.0:9000\n"; Watcher::create() ->addPath(__DIR__ . '/app') ->addPath(__DIR__ . '/views') ->onChange(fn() => $server->reload()) ->watch(); }); $server->start();