Skip to content

Use FrankenPHP's built-in file watcher #971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Commands/Concerns/InstallsFrankenPhpDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions src/Commands/StartFrankenPhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}" : '',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having CADDY_SERVER_WORKER_DIRECTIVE in addition to the previous CADDY_SERVER_WORKER_COUNT allows for backwards compatibility if users have their own previous version of the Caddyfile.

The new variable is needed because the num directive must have a value provided.

'CADDY_SERVER_EXTRA_DIRECTIVES' => $this->buildMercureConfig(),
'CADDY_SERVER_WATCH_DIRECTIVES' => $this->buildWatchConfig(),
]));

$server = $process->start();
Expand Down Expand Up @@ -189,6 +191,41 @@ protected function buildMercureConfig()
return "$config\n\t\t}";
}

/**
* Always return a no-op object, because FrankenPHP has native watcher support.
*
* @return object
*/
protected function startServerWatcher()
{
return new class
{
public function __call($method, $parameters)
{
return null;
}
};
}

/**
* 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.
*
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/stubs/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
}
}

Expand Down