myvon/reactphp-file-system-watcher
Composer 安装命令:
composer require myvon/reactphp-file-system-watcher
包简介
File System Watcher made with ReactPHP EventLoop and ChildProcess
README 文档
README
File System Watcher made with ReactPHP EventLoop and ChildProcess.
This is entirely based on spatie/file-system-watcher and adapted from symfony/process to react/child-process
Watch changes in the file system using PHP
This package allows you to react to all kinds of changes in the file system.
It use react/event-loop and react/child-process to run without blocking the rest of your code (see ReactPHP for more information).
Here's how you can run code when a new file gets added.
use Myvon\Watcher\Watch; Watch::path($directory) ->onFileCreated(function (string $newFilePath) { // do something... }) ->start();
Installation
You can install the package via composer:
composer require myvon/reactphp-file-system-watcher
In your project, you should have the JavaScript package chokidar installed. You can install it via npm
npm install chokidar
or Yarn
yarn add chokidar
Usage
Here's how you can start watching a directory and get notified of any changes.
use Myvon\Watcher\Watch; $watcher = Watch::path($directory) ->onAnyChange(function (string $type, string $path) { if ($type === Watch::EVENT_TYPE_FILE_CREATED) { echo "file {$path} was created"; } }) ->start();
You can pass as many directories as you like to path.
To start watching, call the start method.
To make sure that the watcher keeps watching in production, monitor the script or command that starts it with something like Supervisord.
Detected the type of change
The $type parameter of the closure you pass to onAnyChange can contain one of these values:
Watcher::EVENT_TYPE_FILE_CREATED: a file was createdWatcher::EVENT_TYPE_FILE_UPDATED: a file was updatedWatcher::EVENT_TYPE_FILE_DELETED: a file was deletedWatcher::EVENT_TYPE_DIRECTORY_CREATED: a directory was createdWatcher::EVENT_TYPE_DIRECTORY_DELETED: a directory was deleted
Listening for specific events
To handle file systems events of a certain type, you can make use of dedicated functions. Here's how you would listen for file creations only.
use Myvon\Watcher\Watch; Watch::path($directory) ->onFileCreated(function (string $newFilePath) { // do something... });
These are the related available methods:
onFileCreated(): accepts a closure that will get passed the new file pathonFileUpdated(): accepts a closure that will get passed the updated file pathonFileDeleted(): accepts a closure that will get passed the deleted file pathonDirectoryCreated(): accepts a closure that will get passed the created directory pathonDirectoryDeleted(): accepts a closure that will get passed the deleted directory pathonClose(): accepts a closure that will be called when watcher is stopped
Watching multiple paths
You can pass multiple paths to the paths method.
use Myvon\Watcher\Watch; Watch::paths($directory, $anotherDirectory);
Performing multiple tasks
You can call onAnyChange, 'onFileCreated', ... multiple times. All given closures will be performed
use Myvon\Watcher\Watch; Watch::path($directory) ->onFileCreated(function (string $newFilePath) { // do something on file creation... }) ->onFileCreated(function (string $newFilePath) { // do something else on file creation... }) ->onAnyChange(function (string $type, string $path) { // do something... }) ->onAnyChange(function (string $type, string $path) { // do something else... }) // ...
Stopping the watcher gracefully
By default, the watcher will continue indefinitely when started. There is two ways to gracefully stop the watcher :
- you can call
shouldContinueand pass it a closure. If the closure returns a falsy value, the watcher will stop. The given closure will be executed every 0.5 second.
use Myvon\Watcher\Watch; Watch::path($directory) ->shouldContinue(function () { // return true or false }) // ...
- you can call the
stopmethod anywhere in your code
use Myvon\Watcher\Watch; $watcher = Watch::path($directory); // ... $watcher->stop();
Change the speed of watcher
By default, the changes are tracked every 0.5 seconds, however you could change that.
use Myvon\Watcher\Watch; Watch::path($directory) ->setIntervalTime(0.1) //unit is seconds therefore -> 0.1s // ...rest of your methods
You can also specify the interval directly on the start.
use Myvon\Watcher\Watch; Watch::path($directory) ->start(null, 0.1); //unit is seconds therefore -> 0.1s // ...rest of your methods
Using another loop
By default, the watcher will use the default loop by calling Loop:get(). If needed, you can use another loop implemting LoopInterface interface of ReactPHP by passing it as the first argument of start:
use Myvon\Watcher\Watch; $loop = new MyCustomLoop(); Watch::path($directory) ->start($loop);
Notice: the watcher will register the needed timer but won't start the loop, don't forget to start it.
You also can look into test/WatchTest.php to see how it is used to have one loop by test
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
- All credits goes to Spatie for the original spatie/file-system-watcher library
License
The MIT License (MIT). Please see License File for more information.
myvon/reactphp-file-system-watcher 适用场景与选型建议
myvon/reactphp-file-system-watcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「reactphp」 「file-system-watcher」 「myvon」 「reactphp-file-system-watcher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 myvon/reactphp-file-system-watcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 myvon/reactphp-file-system-watcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 myvon/reactphp-file-system-watcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Watch changes in the file system using PHP
Simple, event-driven multicast UDP message client and server for ReactPHP.
RdKafka Consumer and Producer implementation with ReactPHP Event Loop
Watch changes in the file system using PHP
A decorator of the ReactPHP MySQL connection interface which augments its behaviour, e.g. providing binding of associative parameters.
Zenity allows you to build graphical desktop (GUI) applications in PHP, built on top of ReactPHP.
统计信息
- 总下载量: 1.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-12