As discussed in https://github.com/php/frankenphp/discussions/1961,
there is no real way to pass a severity/level to any log handler offered
by PHP that would make it to the FrankenPHP layer. This new function
allows applications embedding FrankenPHP to integrate PHP logging into
the application itself, thus offering a more streamlined experience.
---------
Co-authored-by: Quentin Burgess <qutn.burgess@gmail.com>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
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>
* add metrics
* change how counting works
* also replace dots
* check that metrics exist
* rename NullMetrics to nullMetrics
* update go.sum
* register collectors only once
* add tests
* add tests for metrics and fix bugs
* keep old metrics around for test
* properly reset during shutdown
* use the same method as frankenphp
* Revert "keep old metrics around for test"
This reverts commit 1f0df6f6bdaebf32aec346f068d6f42a0b5f4007.
* change to require.NoError
* compile regex only once
* remove name sanitizer
* use require
* parameterize host port because security software sucks
* remove need for renaming workers
* increase number of threads and add tests
* fix where frankenphp configuration was bleeding into later tests
* adds basic docs for metrics
* Add caddy metrics link
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
* Fix typos
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
* address feedback
* change comment to be much more "dangerous"
---------
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>