PHPackages                             ryx16/file-watcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ryx16/file-watcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ryx16/file-watcher
==================

1.0(2y ago)07Apache-2.0PHP

Since Jun 24Pushed 1y agoCompare

[ Source](https://github.com/ryx16/file-watcher)[ Packagist](https://packagist.org/packages/ryx16/file-watcher)[ RSS](/packages/ryx16-file-watcher/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

file-watcher
============

[](#file-watcher)

spl+ forked from easy-swoole/file-watcher

使用规则
----

[](#使用规则)

### WatchRule

[](#watchrule)

监控目录:

```
$watchRule = new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT."/App");
```

指定忽略目录：

```
/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setIgnorePaths([EASYSWOOLE_ROOT."/App/Api", EASYSWOOLE_ROOT."/App/Admin"]);
```

指定忽略文件：

```
/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setIgnoreFiles([EASYSWOOLE_ROOT."/App/Api/Teacher.php", EASYSWOOLE_ROOT."/App/Admin/Teacher.php"]);
```

指定匹配后缀：

```
/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setType($watchRule::SCAN_TYPE_SUFFIX_MATCH);
//$watchRule->setType($watchRule::SCAN_TYPE_IGNORE_SUFFIX);
$watchRule->setSuffix(['php']);
```

> type为SCAN\_TYPE\_SUFFIX\_MATCH时，只匹配后缀必须在suffix规则数组的文件。 type为SCAN\_TYPE\_IGNORE\_SUFFIX时，会忽略掉后缀在suffix规则数组的文件。

### FileWatcher

[](#filewatcher)

设置监控程序:

```
$fileWatcher = new \EasySwoole\FileWatcher\FileWatcher();
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\Inotify::class);
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\FileScanner::class);
```

> 二选一 不调用此方法 存在inotify扩展默认为Inotify::class反之FileScanner::class

增加监控规则：

```
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(__DIR__));
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT. '/App'));
```

> 可进行多次调用 对不同目录设置不同的规则

设置异常回调：

```
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setOnException(function (\Throwable $throwable){

});
```

设置检测周期(默认`1000ms`)：

```
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setCheckInterval(1000);
```

设置触发回调(文件有变化)：

```
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setOnChange(function (array $list, \EasySwoole\FileWatcher\WatchRule $rule){
    // list为变化的文件列表
});
```

启动(swoole服务中使用)：

```
/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
/**@var \Swoole\Server $server **/
$fileWatcher->attachServer($server);
```

EasySwoole中用于热重启
----------------

[](#easyswoole中用于热重启)

例如在`Easyswoole`开发模式中，我们希望当有代码变动的时候，实现Server重启，示例代码如下：

```
namespace EasySwoole\EasySwoole;

use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FileWatcher\FileWatcher;
use EasySwoole\FileWatcher\WatchRule;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        $watcher = new FileWatcher();
        $rule = new WatchRule(EASYSWOOLE_ROOT."/App");
        $watcher->addRule($rule);
        $watcher->setOnChange(function (){
            Logger::getInstance()->info('file change ,reload!!!');
            ServerManager::getInstance()->getSwooleServer()->reload();
        });
        $watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
    }
}
```

> 注意，reload仅仅针对Worker进程加载的代码有效

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1058d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8477444e29035f795e42c3021aa3bf37fc0c8852f5e9f0d733fa3a20af8c5ee6?d=identicon)[ryx16](/maintainers/ryx16)

---

Top Contributors

[![kiss291323003](https://avatars.githubusercontent.com/u/24490609?v=4)](https://github.com/kiss291323003 "kiss291323003 (8 commits)")[![Player626](https://avatars.githubusercontent.com/u/44792981?v=4)](https://github.com/Player626 "Player626 (4 commits)")[![ryx16](https://avatars.githubusercontent.com/u/25482353?v=4)](https://github.com/ryx16 "ryx16 (3 commits)")

### Embed Badge

![Health badge](/badges/ryx16-file-watcher/health.svg)

```
[![Health](https://phpackages.com/badges/ryx16-file-watcher/health.svg)](https://phpackages.com/packages/ryx16-file-watcher)
```

###  Alternatives

[easyswoole/verifycode

21102.4k4](/packages/easyswoole-verifycode)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
