### Initialize Watchman Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Shows how to instantiate the WatchmanWatcher for file system monitoring. This watcher requires Watchman to be installed on the system and works across Linux, Mac, and Windows. ```PHP use Phpactor\AmpFsWatch\Watcher\Watchman\WatchmanWatcher; $watcher = new WatchmanWatcher($config, $logger); ``` -------------------------------- ### Monitor File System Changes with Amp FS Watch Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Demonstrates a comprehensive setup for monitoring file system changes using Amp FS Watch. It initializes a PSR logger, configures a WatcherConfig for a given path, and sets up a PatternMatchingWatcher that wraps a FallbackWatcher. The FallbackWatcher includes BufferedWatcher, InotifyWatcher, FindWatcher, PhpPollWatcher, and FsWatchWatcher. The example then yields the watcher process and continuously prints file change notifications. ```PHP Loop::run(function () use () { $logger = // create a PSR logger $config = new WatcherConfig([$path]); $watcher = new PatternMatchingWatcher( new FallbackWatcher([ new BufferedWatcher(new InotifyWatcher($config, $logger), 10), new FindWatcher($config, $logger), new PhpPollWatcher($config, $logger), new FsWatchWatcher($config, $logger) ], $logger), [ '/**/*.php' ], [] ); $process = yield $watcher->watch([$path]); while (null !== $file = yield $process->wait()) { fwrite(STDOUT, sprintf('[%s] %s (%s)' . "\n", date('Y-m-d H:i:s.u'), $file->path(), $file->type())); } }); ``` -------------------------------- ### PHP File Watcher: Proposed Promise-Based API Source: https://github.com/phpactor/amp-fswatch/blob/master/adr/0001-use-callback.md This example illustrates a proposed promise-based API for file watching. It involves monitoring paths and then yielding `wait()` on a stream to asynchronously retrieve `ModifiedFile` objects, addressing potential backlogs. ```php $stream = $watcher->monitor($paths); while (null !== $modifiedFile = yield $stream->wait()) { // do something } ``` -------------------------------- ### Initialize FsWatch Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Demonstrates how to use the FsWatchWatcher. This watcher is cross-platform and leverages native system capabilities when possible, though it is noted as unstable and not extensively tested. ```PHP use Phpactor\AmpFsWatch\Watcher\FsWatch\FsWatchWatcher; $watcher = new FsWatchWatcher($config, $logger); // ... ``` -------------------------------- ### Initialize Inotify Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Illustrates the creation of an InotifyWatcher instance. This watcher utilizes the Linux `inotifywait` binary for real-time file change detection, providing efficient monitoring on Linux systems. ```PHP use Phpactor\AmpFsWatch\Watcher\Inotify\InotifyWatcher; $watcher = new InotifyWatcher($config, $logger); // ... ``` -------------------------------- ### Initialize Fallback Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Illustrates the configuration of a FallbackWatcher. This watcher automatically selects the first supported watcher from a provided list based on the current system's capabilities, offering a robust solution for varying environments. ```PHP use Phpactor\AmpFsWatch\Watcher\Fallback\FallbackWatcher; $watcher = new FallbackWatcher( [ new InotifyWatcher($logger), new FindWatcher(500, $logger) ], $logger ); // ... ``` -------------------------------- ### Initialize Find Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Shows the instantiation of a FindWatcher. This watcher polls for file changes using the `find` binary, typically on Linux and Mac systems. It's important to note potential compatibility issues with non-GNU/BSD `find` variants due to the use of the `-newerxy` switch. ```PHP use Phpactor\AmpFsWatch\Watcher\Find\FindWatcher; $watcher = new FindWatcher($config, $logger); // ... ``` -------------------------------- ### Initialize PHP Poll Watcher Source: https://github.com/phpactor/amp-fswatch/blob/master/README.md Explains how to set up the PhpPollWatcher. This is the most compatible option as it works on all environments, but it is also the slowest and most resource-intensive due to its pure PHP implementation. ```PHP use Phpactor\AmpFsWatch\Watcher\Find\FindWatcher; $watcher = new PhpPollWatcher($config, $logger); // ... ``` -------------------------------- ### PHP File Watcher: Proposed Emitter/Iterator API Source: https://github.com/phpactor/amp-fswatch/blob/master/adr/0001-use-callback.md This snippet presents another alternative API design using an emitter or iterator pattern. It shows how to iterate over modified files by yielding `advance()` and then retrieving the current `ModifiedFile`. ```php $iterator = $watcher->watch($paths); while (yield $iterator->advance()) { $modifiedFile = $iterator->getCurrent(); // do something } ``` -------------------------------- ### PHP File Watcher: Callback-Based API Usage Source: https://github.com/phpactor/amp-fswatch/blob/master/adr/0001-use-callback.md This snippet demonstrates the existing public API for file watching. It shows how to register a callback function that is executed whenever a file is modified, receiving a `ModifiedFile` object as an argument. ```php $watcher->monitor($paths, function (ModifiedFile $file) { // do something }); ``` -------------------------------- ### PHP File Watcher with Promises (Improved) Source: https://github.com/phpactor/amp-fswatch/blob/master/adr/0002-do-not-use-callback.md Refactored implementation of the file watcher that yields promises for modified files. This approach uses `yield $watcher->wait()` to retrieve files, making it more suitable and easier to integrate into Amp projects. ```php while (null !== $file = yield $watcher->wait()) $job = $this->indexer->getJob($file->path()); foreach ($job->generator() as $file) { yield new Delayed(1); } }); ``` -------------------------------- ### PHP File Watcher with Callback (Deprecated) Source: https://github.com/phpactor/amp-fswatch/blob/master/adr/0002-do-not-use-callback.md Initial implementation of a file watcher using a callback function to handle modified files. This approach was deemed 'odd' and not properly integrated with co-routines, leading to a decision to refactor. ```php $this->watcher->watch($this->paths, function (ModifiedFile $file) { asyncCall(function () use ($file) { $job = $this->indexer->getJob($file->path()); foreach ($job->generator() as $file) { yield new Delayed(1); } }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.