innmind / file-watch
当文件更改时执行代码的库
4.0.0
2024-03-10 10:42 UTC
Requires
- php: ~8.2
- innmind/server-control: ~5.0
- innmind/time-continuum: ~3.0
- innmind/time-warp: ~3.0
- innmind/url: ~4.0
- psr/log: ~3.0
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- phpunit/phpunit: ~10.2
- vimeo/psalm: ~5.12
README
一个小工具,每次修改文件(或文件夹)时都会执行代码。
安装
composer require innmind/file-watch
使用方法
use Innmind\FileWatch\{ Factory, Stop, }; use Innmind\Server\Control\ServerFactory; use Innmind\TimeWarp\Halt\Usleep; use Innmind\TimeContinuum\Earth\Clock; use Innmind\Stream\Streams; use Innmind\Url\Path; use Innmind\Immutable\Either; $watch = Factory::build( ServerFactory::build( new Clock, Streams::fromAmbientAuthority(), new Usleep, )->processes(), new Usleep, ); $count = $watch(Path::of('/to/some/file/or/folder'))(0, function(int $count, Continuation $continuation): Continuation { // this function is called every time the file is modified ++$count; if ($count === 42) { // This will stop watching the folder for changes and return the count return $continuation->stop($count); } // This will instruct to continue watching for changes and the value will be // passed to this callable the next time it's called return $continuation->continue($count); })->match( static fn(int $count) => $count, // always 42 as it's the stopping value static fn() => throw new \RuntimeException('Failed to watch for changes'), );
警告
由于 tail
和 stat
的工作方式,单个更改可能导致函数被多次调用。