企鹅/fswatch

使用 fswatch 监控文件变化的库

v1.0.1 2023-05-14 04:00 UTC

This package is auto-updated.

Last update: 2024-09-16 16:54:16 UTC


README

此软件包提供文件监控功能。

安装

当前版本仅支持 fswatch,因此您需要先安装 fswatch。

# MacOS
brew install fswatch

# Linux
wget https://github.com/emcrisostomo/fswatch/releases/download/{VERSION}/fswatch-{VERSION}.tar.gz
tar -xzvf fswatch-{VERSION}.tar.gz
cd fswatch-{VERSION} && ./configure && make && sudo make install && sudo ldconfig

# Composer
composer require penguin/fswatch

用法

<?php

use Penguin\Component\FsWatch\FsWatch;

require __DIR__ . '/vendor/autoload.php';

(new FsWatch(__DIR__))
    ->usePolling() // use poll_monitor (default is inotify in Linux, FSEvents in MacOS)
    // ignore files json, txt with regex
    ->ignore('.*\.json')
    ->ignore('.*\.txt')
    ->onChange(function (string $path) {
        echo "onChange {$path}";
    });

方法

  • onChange(callable $callback(string $path, Process $process))
  • onAdd(callable $callback(string $path, Process $process))
  • onUnlink(callable $callback(string $path, Process $process))
  • onAddDir(callable $callback(string $path, Process $process))
  • onError(callable $callback, Process $process)
  • onAny(callable $callback(int $eventCode, string $path, Process $process)):当没有注册其他事件时执行此事件
  • usePolling():使用轮询监控。轮询监控在所有平台上都可用,仅依赖于可用的 CPU 和内存来执行其任务。
  • oneEvent():在接收到第一组事件后退出 fswatch
  • multiEvent():在接收到事件后不要退出 fswatch
  • ignore(string $regex):排除与正则表达式匹配的路径
  • unWatch(string ...$paths)
  • addWatch(string ...$paths)