phprtc/watcher

Swoole 文件监视器

0.1.3 2023-03-23 12:53 UTC

This package is auto-updated.

Last update: 2024-09-23 16:17:10 UTC


README

使用 Swoole 和 Inotify 实现的基于 PHP 的文件系统更改监视器。

安装

composer require phprtc/watcher ^0.1 --dev

用法

基本用法

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

任何事件

监听指定路径上的任何事件

使用此方法时请小心。

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

忽略路径

使用正则表达式忽略文件

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

过滤器

  • 确保触发事件文件不以提供的字符结尾。

    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();
  • 仅监听文件名与给定扩展名匹配的事件。

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

停止监视器

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 服务器集成

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