innmind / file-watch
Library to execute code when a file changes
5.0.0
2025-05-08 16:16 UTC
Requires
- php: ~8.2
- innmind/server-control: ~6.0
- innmind/time-continuum: ^4.1.1
- innmind/time-warp: ~4.0
- innmind/url: ~4.0
- psr/log: ~3.0
Requires (Dev)
- innmind/black-box: ~6.1
- innmind/coding-standard: ~2.0
- innmind/static-analysis: ^1.2.1
- dev-develop
- 5.0.0
- 4.0.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.2.0
- 2.1.0
- 2.0.0
- 1.1.1
- 1.1.0
- 1.0.0
- dev-master
- dev-cs
- dev-remove-interfaces
- dev-attempt
- dev-simplify-implementation
- dev-fix-file-watch
- dev-update-dependencies
- dev-reuse-workflows
- dev-static-analysis
- dev-blackbox-6
- dev-license-date-range
- dev-php-84-deprecation
This package is auto-updated.
Last update: 2025-05-08 16:17:53 UTC
README
Small tool to execute code every time a file (or folder) is modified.
Installation
composer require innmind/file-watch
Usage
use Innmind\FileWatch\{ Factory, Stop, }; use Innmind\Server\Control\ServerFactory; use Innmind\TimeWarp\Halt\Usleep; use Innmind\TimeContinuum\Clock; use Innmind\IO\IO; use Innmind\Url\Path; use Innmind\Immutable\Either; $watch = Factory::build( ServerFactory::build( Clock::live(), IO::fromAmbientAuthority(), Usleep::new(), )->processes(), Usleep::new(), ); $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(\Throwable $e) => throw $e, );
Warning
The function may be called multiple times for a single change due to the way tail
and stat
works.