|
1 | | -Watcher for Hypervel |
| 1 | +File Watcher for Hypervel |
2 | 2 | === |
3 | 3 |
|
4 | 4 | [](https://deepwiki.com/hypervel/watcher) |
| 5 | + |
| 6 | +A file watcher with pluggable drivers and restart strategies for Hypervel. Detects file changes using coroutine-native drivers and triggers configurable restart actions. |
| 7 | + |
| 8 | +## Configuration |
| 9 | + |
| 10 | +```php |
| 11 | +// config/watcher.php |
| 12 | +return [ |
| 13 | + 'driver' => ScanFileDriver::class, |
| 14 | + 'scan_interval' => 2000, |
| 15 | + |
| 16 | + 'watch' => [ |
| 17 | + 'app/**/*.php', |
| 18 | + 'config/**/*.php', |
| 19 | + '.env', |
| 20 | + ], |
| 21 | + |
| 22 | + 'bin' => PHP_BINARY, |
| 23 | + 'command' => 'artisan serve', |
| 24 | +]; |
| 25 | +``` |
| 26 | + |
| 27 | +### Watch Paths |
| 28 | + |
| 29 | +Each entry in the `watch` array can be: |
| 30 | + |
| 31 | +- **A directory name** — `'app'` watches all files recursively |
| 32 | +- **A glob pattern** — `'config/**/*.php'` watches only matching files |
| 33 | +- **A specific file** — `'.env'` or `'composer.json'` |
| 34 | + |
| 35 | +Glob patterns support `*` (single directory segment), `**` (recursive), `?` (single character), and `{a,b}` (alternation), powered by Symfony Finder's glob engine. |
| 36 | + |
| 37 | +### Drivers |
| 38 | + |
| 39 | +| Driver | Description | |
| 40 | +|--------|-------------| |
| 41 | +| `ScanFileDriver` | Cross-platform, polls files using MD5 checksums | |
| 42 | +| `FindDriver` | Uses `find -mmin` (Linux) or `gfind` (macOS via Homebrew) | |
| 43 | +| `FindNewerDriver` | Uses `find -newer` with a reference file for comparison | |
| 44 | +| `FswatchDriver` | Uses `fswatch` (macOS native or Linux via `apt`/`brew`) | |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +```bash |
| 49 | +# Watch and restart server on file changes |
| 50 | +php artisan watch |
| 51 | + |
| 52 | +# Watch additional paths beyond config |
| 53 | +php artisan watch --path=routes --path=database/**/*.php |
| 54 | + |
| 55 | +# Watch without restarting (detect changes only) |
| 56 | +php artisan watch --no-restart |
| 57 | +``` |
| 58 | + |
| 59 | +## Architecture |
| 60 | + |
| 61 | +The watcher separates three concerns: |
| 62 | + |
| 63 | +- **`Option`** — Parses watch configuration into typed `WatchPath` objects |
| 64 | +- **Drivers** (`DriverInterface`) — Detect file changes and push paths to a Channel |
| 65 | +- **Restart Strategies** (`RestartStrategy`) — Define what happens when changes are detected |
| 66 | + |
| 67 | +### Restart Strategies |
| 68 | + |
| 69 | +The `RestartStrategy` interface enables different packages to reuse the file watching infrastructure: |
| 70 | + |
| 71 | +```php |
| 72 | +interface RestartStrategy |
| 73 | +{ |
| 74 | + public function start(): void; |
| 75 | + public function restart(): void; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +The built-in `ServerRestartStrategy` handles Swoole server restart via PID file. Other packages (e.g., Horizon) can implement their own strategy to restart different process types. |
0 commit comments