From ad39900e9af8dfdadfbe128441dc94c4f10dd6db Mon Sep 17 00:00:00 2001 From: MK Date: Sat, 7 Dec 2024 22:49:28 -0500 Subject: [PATCH] use FrankenPHP's built-in file watcher --- .../InstallsFrankenPhpDependencies.php | 2 +- src/Commands/StartFrankenPhpCommand.php | 36 +++++++++++++++++++ src/Commands/stubs/Caddyfile | 6 +++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Commands/Concerns/InstallsFrankenPhpDependencies.php b/src/Commands/Concerns/InstallsFrankenPhpDependencies.php index 70d38ce18..45156dcad 100644 --- a/src/Commands/Concerns/InstallsFrankenPhpDependencies.php +++ b/src/Commands/Concerns/InstallsFrankenPhpDependencies.php @@ -22,7 +22,7 @@ trait InstallsFrankenPhpDependencies * * @var string */ - protected $requiredFrankenPhpVersion = '1.1.0'; + protected $requiredFrankenPhpVersion = '1.3.0'; /** * Ensure the FrankenPHP's Caddyfile and worker script are installed. diff --git a/src/Commands/StartFrankenPhpCommand.php b/src/Commands/StartFrankenPhpCommand.php index 9eab4eb92..899ec6457 100644 --- a/src/Commands/StartFrankenPhpCommand.php +++ b/src/Commands/StartFrankenPhpCommand.php @@ -103,7 +103,9 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve 'CADDY_SERVER_LOGGER' => 'json', 'CADDY_SERVER_SERVER_NAME' => $serverName, 'CADDY_SERVER_WORKER_COUNT' => $this->workerCount() ?: '', + 'CADDY_SERVER_WORKER_DIRECTIVE' => $this->workerCount() ? "num {$this->workerCount()}" : '', 'CADDY_SERVER_EXTRA_DIRECTIVES' => $this->buildMercureConfig(), + 'CADDY_SERVER_WATCH_DIRECTIVES' => $this->buildWatchConfig(), ])); $server = $process->start(); @@ -189,6 +191,25 @@ protected function buildMercureConfig() return "$config\n\t\t}"; } + /** + * Generate the file watcher configuration snippet to include in the Caddyfile. + * + * @return string + */ + protected function buildWatchConfig() + { + if (! $this->option('watch')) { + return ''; + } + + // If paths are not specified, fall back to FrankenPHP's default watcher pattern + if (empty($paths = config('octane.watch'))) { + return "\t\twatch"; + } + + return collect($paths)->map(fn ($path) => "\t\twatch ".base_path($path))->join("\n"); + } + /** * Get the maximum number of seconds that workers should be allowed to execute a single request. * @@ -348,4 +369,19 @@ protected function stopServer() '--server' => 'frankenphp', ]); } + + /** + * Always return a no-op object, because FrankenPHP has native + * watcher support, so there is no need for an external watcher. + */ + protected function startServerWatcher() + { + return new class + { + public function __call($method, $parameters) + { + return null; + } + }; + } } diff --git a/src/Commands/stubs/Caddyfile b/src/Commands/stubs/Caddyfile index 82508830d..b542097ab 100644 --- a/src/Commands/stubs/Caddyfile +++ b/src/Commands/stubs/Caddyfile @@ -4,7 +4,11 @@ admin {$CADDY_SERVER_ADMIN_HOST}:{$CADDY_SERVER_ADMIN_PORT} frankenphp { - worker "{$APP_PUBLIC_PATH}/frankenphp-worker.php" {$CADDY_SERVER_WORKER_COUNT} + worker { + file "{$APP_PUBLIC_PATH}/frankenphp-worker.php" + {$CADDY_SERVER_WORKER_DIRECTIVE} + {$CADDY_SERVER_WATCH_DIRECTIVES} + } } }