mirror of
https://github.com/dunglas/frankenphp.git
synced 2025-12-24 13:38:11 +08:00
This patch brings hot reloading capabilities to PHP apps: in development, the browser will automatically refresh the page when any source file changes! It's similar to HMR in JavaScript. It is built on top of [the watcher mechanism](https://frankenphp.dev/docs/config/#watching-for-file-changes) and of the [Mercure](https://frankenphp.dev/docs/mercure/) integration. Each time a watched file is modified, a Mercure update is sent, giving the ability to the client to reload the page, or part of the page (assets, images...). Here is an example implementation: ```caddyfile root ./public mercure { subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY} anonymous } php_server { hot_reload } ``` ```php <?php header('Content-Type: text/html'); ?> <!DOCTYPE html> <html lang="en"> <head> <title>Test</title> <script> const es = new EventSource('<?=$_SERVER['FRANKENPHP_HOT_RELOAD']?>'); es.onmessage = () => location.reload(); </script> </head> <body> Hello ``` I plan to create a helper JS library to handle more advanced cases (reloading CSS, JS, etc), similar to [HotWire Spark](https://github.com/hotwired/spark). Be sure to attend my SymfonyCon to learn more! There is still room for improvement: - Provide an option to only trigger the update without reloading the worker for some files (ex, images, JS, CSS...) - Support classic mode (currently, only the worker mode is supported) - Don't reload all workers when only the files used by one change However, this PR is working as-is and can be merged as a first step. This patch heavily refactors the watcher module. Maybe it will be possible to extract it as a standalone library at some point (would be useful to add a similar feature but not tight to PHP as a Caddy module). --------- Signed-off-by: Kévin Dunglas <kevin@dunglas.fr> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package frankenphp
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/dunglas/frankenphp/internal/state"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestScaleARegularThreadUpAndDown(t *testing.T) {
|
|
t.Cleanup(Shutdown)
|
|
|
|
assert.NoError(t, Init(
|
|
WithNumThreads(1),
|
|
WithMaxThreads(2),
|
|
WithLogger(slog.New(slog.NewTextHandler(io.Discard, nil))),
|
|
))
|
|
|
|
autoScaledThread := phpThreads[1]
|
|
|
|
// scale up
|
|
scaleRegularThread()
|
|
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
|
|
assert.IsType(t, ®ularThread{}, autoScaledThread.handler)
|
|
|
|
// on down-scale, the thread will be marked as inactive
|
|
setLongWaitTime(t, autoScaledThread)
|
|
deactivateThreads()
|
|
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
|
|
}
|
|
|
|
func TestScaleAWorkerThreadUpAndDown(t *testing.T) {
|
|
t.Cleanup(Shutdown)
|
|
|
|
workerName := "worker1"
|
|
workerPath := testDataPath + "/transition-worker-1.php"
|
|
assert.NoError(t, Init(
|
|
WithNumThreads(2),
|
|
WithMaxThreads(3),
|
|
WithWorkers(workerName, workerPath, 1,
|
|
WithWorkerEnv(map[string]string{}),
|
|
WithWorkerWatchMode([]string{}),
|
|
WithWorkerMaxFailures(0),
|
|
),
|
|
WithLogger(slog.New(slog.NewTextHandler(io.Discard, nil))),
|
|
))
|
|
|
|
autoScaledThread := phpThreads[2]
|
|
|
|
// scale up
|
|
scaleWorkerThread(getWorkerByPath(workerPath))
|
|
assert.Equal(t, state.Ready, autoScaledThread.state.Get())
|
|
|
|
// on down-scale, the thread will be marked as inactive
|
|
setLongWaitTime(t, autoScaledThread)
|
|
deactivateThreads()
|
|
assert.IsType(t, &inactiveThread{}, autoScaledThread.handler)
|
|
}
|
|
|
|
func setLongWaitTime(t *testing.T, thread *phpThread) {
|
|
t.Helper()
|
|
|
|
thread.state.SetWaitTime(time.Now().Add(-time.Hour))
|
|
}
|