Skip to main content

File Monitor Plugin

A directory watcher that requests a server reload when matching files are created, modified, deleted, or moved into a watched directory.

File Monitor selects the watcher for the current operating system automatically:

PlatformWatcher
LinuxNative Inotify watcher, accessed through PHP FFI.
macOSNative FSEvents watcher, accessed through PHP FFI.
Other platformsPolling watcher that rescans the matching files for changes.

Installation

$ composer require phpstreamserver/file-monitor

Usage Example

server.php
<?php

require __DIR__ . '/vendor/autoload.php';

use PHPStreamServer\Core\Server;
use PHPStreamServer\Core\Worker\SupervisedWorker;
use PHPStreamServer\Plugin\FileMonitor\FileMonitorPlugin;
use PHPStreamServer\Plugin\FileMonitor\WatchRule;

$server = new Server();

$server->addPlugin(
new FileMonitorPlugin(
new WatchRule(glob: __DIR__ . '/src/**/*.php', invalidateOpcache: true),
),
);

$server->addWorker(
new SupervisedWorker(
name: 'Worker process',
onStart: function (SupervisedWorker $worker): void {
$worker->logger->notice("Worker process has started");
},
),
);

exit($server->run());

Plugin Configuration

🧩 FileMonitorPlugin

Plugin class: FileMonitorPlugin

ParameterTypeDefaultDescription
watchRulesWatchRule ...zero argumentsOptional. WatchRule configurations specifying which files to monitor.

WatchRule Configuration

WatchRule specifies an absolute glob of files to monitor. Use a globstar directory segment (**) followed by a file pattern to monitor files recursively.

📂 WatchRule

Class: WatchRule

ParameterTypeDefaultDescription
globstringrequiredAbsolute glob of files to monitor, for example /src/**/*.php or /*.php.
invalidateOpcacheboolfalseOptional. Whether to invalidate all scripts cached by OPCache.