steve-moretz/file-system-watcher
Composer 安装命令:
composer require steve-moretz/file-system-watcher
包简介
Watch changes in the file system using PHP
README 文档
README
This package allows you to react to all kinds of changes in the file system.
Here's how you can run code when a new file gets added.
use Spatie\Watcher\Watch; Watch::path($directory) ->onFileCreated(function (string $newFilePath) { // do something... }) ->start();
Support
Php 7 | 8
Installation
You can install the package via composer:
composer require spatie/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 Spatie\Watcher\Watch; 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.
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 Spatie\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 path
Watching multiple paths
You can pass multiple paths to the paths method.
use Spatie\Watcher\Watch; Watch::paths($directory, $anotherDirectory);
Performing multiple tasks
You can call onAnyChange, 'onFileCreated', ... multiple times. All given closures will be performed
use Spatie\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. 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 Spatie\Watcher\Watch; Watch::path($directory) ->shouldContinue(function () { // return true or false }) // ...
Change the speed of watcher
By default, the changes are tracked every 0.5 seconds, however you could change that.
use Spatie\Watcher\Watch; Watch::path($directory) ->setIntervalTime(100) //microseconds // ...
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
Parts of this package are inspired by Laravel Octane
License
The MIT License (MIT). Please see License File for more information.
steve-moretz/file-system-watcher 适用场景与选型建议
steve-moretz/file-system-watcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.35k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「file-system-watcher」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 steve-moretz/file-system-watcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 steve-moretz/file-system-watcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 steve-moretz/file-system-watcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Watch changes in the file system using PHP
Receive webhooks in Laravel apps
File System Watcher made with ReactPHP EventLoop and ChildProcess
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
统计信息
- 总下载量: 1.35k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-16