nivpenso/react-file-watcher

基于reactphp的文件监视器包

v0.2.0 2021-03-07 20:26 UTC

This package is auto-updated.

Last update: 2024-09-04 17:03:51 UTC


README

react-watcher-php codecov

关于

此包基于 reactphp/event-loop,提供了一个易于使用的接口来设置文件系统监视器和检测更改。

更深入的了解

该包利用react-php的抽象,使用在环境中已安装的底层平台包创建一个高效的文件监视器

支持

如何安装

composer require nivpenso/react-file-watcher

如何使用

代码片段

use React\EventLoop\Factory;
use ReactFileWatcher\FileWatcherFactory;
use ReactFileWatcher\PathObjects\PathWatcher;

// create path to watch in the file system
$pathToWatch = new PathWatcher("/tmp/", true);

// creating the loop using ReactPHP.
$loop = Factory::create();

// creating the file watcher based on the loop.
$fileWatcher = FileWatcherFactory::create($loop);

// call the watch and execute the callback when detecting change event.
$fsevent = $fileWatcher->Watch([$pathToWatch], function($filename) {
    var_dump($filename);
    print PHP_EOL;
});

演示

此包附带一个演示,您可以自由运行它

在本地(Linux)运行演示

  1. 确保您的环境中已安装支持库之一(建议:ext-uv
  2. 运行以下命令
# install dependecies
composer install --no-dev
# run the demo
php ./demo/test.php

启动过程后,您可以在监视路径下更改文件(默认:/tmp),然后在终端中查看消息。

在Docker上运行演示

如果您没有准备好的Linux机器或设置的环境,可以使用Docker进行测试。

# In terminal A run this
docker-compose -f ./demo/docker-compose.yaml up

在容器的tmp文件夹内进行更改以查看更改检测

# In terminal B run this
docker ps # to get the container-id
docker exec -it <container-id> /bin/bash
echo "change" > /tmp/1

在第一个终端上,您应该看到进程检测到更改。

测试

此包附带测试。它使用令人惊叹的包 PEST。您可以在本地运行测试或使用提供的docker文件,该文件已经包含一个带有所有必需扩展(evuvlibeventevent)的已准备好的环境。

在本地使用PEST运行测试

composer install
./vendor/bin/pest

使用Docker运行测试

docker-compose -f ./tests/docker-compose.yaml up