maximal/file-system-watch

Simple poll-based synchronous file system watcher for PHP with no dependencies.

Installs: 406

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/maximal/file-system-watch

1.0.0 2023-09-18 15:47 UTC

This package is auto-updated.

Last update: 2025-09-18 21:00:07 UTC


README

This library is a simple poll-based synchronous file system watcher for PHP with no dependencies.

Usage Example

use Maximal\FileSystem\Watch\EventType;
use Maximal\FileSystem\Watch\Watcher;

Watcher::create(__DIR__)
	// Poll the file system every second (1’000’000 microseconds)
	->setPollInterval(1_000_000)
	->onFileAdded(static function (string $path) {
		echo 'File added: ', $path, PHP_EOL;
	})
	->onFileChanged(static function (string $path, array $stat) {
		echo 'File changed: ', $path, '    @ ';
		echo (new DateTime('@' . $stat['mtime']))->format('c'), PHP_EOL;
	})
	->onFileDeleted(static function (string $path) {
		echo 'File deleted: ', $path, PHP_EOL;
	})
	->onDirectoryAdded(static function (string $path) {
		echo 'Directory added: ', $path, PHP_EOL;
	})
	->onDirectoryChanged(static function (string $path, array $stat) {
		echo 'Directory changed: ', $path, '    @ ';
		echo (new DateTime('@' . $stat['mtime']))->format('c'), PHP_EOL;
	})
	->onDirectoryDeleted(static function (string $path) {
		echo 'Directory deleted: ', $path, PHP_EOL;
	})
	// General event handler
	->onAnyEvent(static function (EventType $type, string $path, array $stat) {
		echo 'Event `', $type->value, '`: ', $path, '    @ ';
		echo (new DateTime('@' . $stat['mtime']))->format('c'), PHP_EOL;
	})
	->start();

Run it:

php  example.php  <directory>

Contact the author