phprtc/watcher
最新稳定版本:0.1.3
Composer 安装命令:
composer require phprtc/watcher
包简介
Swoole File Watcher
README 文档
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();
统计信息
- 总下载量: 425
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 83
- 点击次数: 2
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-05