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>
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package frankenphp
|
|
|
|
import (
|
|
"io"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWorkersExtension(t *testing.T) {
|
|
t.Cleanup(Shutdown)
|
|
|
|
readyWorkers := 0
|
|
shutdownWorkers := 0
|
|
serverStarts := 0
|
|
serverShutDowns := 0
|
|
|
|
externalWorkers, o := WithExtensionWorkers(
|
|
"extensionWorkers",
|
|
"testdata/worker.php",
|
|
1,
|
|
WithWorkerOnReady(func(id int) {
|
|
readyWorkers++
|
|
}),
|
|
WithWorkerOnShutdown(func(id int) {
|
|
serverShutDowns++
|
|
}),
|
|
WithWorkerOnServerStartup(func() {
|
|
serverStarts++
|
|
}),
|
|
WithWorkerOnServerShutdown(func() {
|
|
shutdownWorkers++
|
|
}),
|
|
)
|
|
|
|
require.NoError(t, Init(o))
|
|
t.Cleanup(func() {
|
|
Shutdown()
|
|
assert.Equal(t, 1, shutdownWorkers, "Worker shutdown hook should have been called")
|
|
assert.Equal(t, 1, serverShutDowns, "Server shutdown hook should have been called")
|
|
})
|
|
|
|
assert.Equal(t, readyWorkers, 1, "Worker thread should have called onReady()")
|
|
assert.Equal(t, serverStarts, 1, "Server start hook should have been called")
|
|
assert.Equal(t, externalWorkers.NumThreads(), 1, "NumThreads() should report 1 thread")
|
|
|
|
// Create a test request
|
|
req := httptest.NewRequest("GET", "https://example.com/test/?foo=bar", nil)
|
|
req.Header.Set("X-Test-Header", "test-value")
|
|
w := httptest.NewRecorder()
|
|
|
|
// Inject the request into the worker through the extension
|
|
err := externalWorkers.SendRequest(w, req)
|
|
assert.NoError(t, err, "Sending request should not produce an error")
|
|
|
|
resp := w.Result()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
// The worker.php script should output information about the request
|
|
// We're just checking that we got a response, not the specific content
|
|
assert.NotEmpty(t, body, "Response body should not be empty")
|
|
assert.Contains(t, string(body), "Requests handled: 0", "Response body should contain request information")
|
|
}
|
|
|
|
func TestWorkerExtensionSendMessage(t *testing.T) {
|
|
externalWorker, o := WithExtensionWorkers("extensionWorkers", "testdata/message-worker.php", 1)
|
|
|
|
err := Init(o)
|
|
require.NoError(t, err)
|
|
t.Cleanup(Shutdown)
|
|
|
|
ret, err := externalWorker.SendMessage(t.Context(), "Hello Workers", nil)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "received message: Hello Workers", ret)
|
|
}
|
|
|
|
func TestErrorIf2WorkersHaveSameName(t *testing.T) {
|
|
_, o1 := WithExtensionWorkers("duplicateWorker", "testdata/worker.php", 1)
|
|
_, o2 := WithExtensionWorkers("duplicateWorker", "testdata/worker2.php", 1)
|
|
|
|
t.Cleanup(Shutdown)
|
|
require.Error(t, Init(o1, o2))
|
|
}
|