承接 myvon/reactphp-file-system-watcher 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Latest Version on Packagist Tests Total Downloads

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 created
  • Watcher::EVENT_TYPE_FILE_UPDATED: a file was updated
  • Watcher::EVENT_TYPE_FILE_DELETED: a file was deleted
  • Watcher::EVENT_TYPE_DIRECTORY_CREATED: a directory was created
  • Watcher::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 path
  • onFileUpdated(): accepts a closure that will get passed the updated file path
  • onFileDeleted(): accepts a closure that will get passed the deleted file path
  • onDirectoryCreated(): accepts a closure that will get passed the created directory path
  • onDirectoryDeleted(): accepts a closure that will get passed the deleted directory path
  • onClose(): 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 shouldContinue and 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 stop method 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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 myvon/reactphp-file-system-watcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-12