-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Description
Description
Hello,
I mostly use Symfony components in a standalone way (outside Symfony Framework) and sometimes find quite hard to handle the fileLinkFormat
globally.
error-handler
symfony/src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php
Lines 55 to 58 in 42d43b7
$fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? null; | |
$this->fileLinkFormat = \is_string($fileLinkFormat) | |
? (ErrorRendererInterface::IDE_LINK_FORMATS[$fileLinkFormat] ?? $fileLinkFormat ?: false) | |
: ($fileLinkFormat ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: false); |
Although it has been easier to setup since SYMFONY_IDE
env var was introduced, this works perfectly if you don't use a container (Docker, etc.). But the link format described here to handle path mapping won't work with the only SYMFONY_IDE
env var set. In such case, you will have to set a custom exception handler overriding the default one here:
symfony/src/Symfony/Component/ErrorHandler/ErrorHandler.php
Lines 647 to 662 in 42d43b7
private function renderException(\Throwable $exception): void | |
{ | |
$renderer = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliErrorRenderer() : new HtmlErrorRenderer($this->debug); | |
$exception = $renderer->render($exception); | |
if (!headers_sent()) { | |
http_response_code($exception->getStatusCode()); | |
foreach ($exception->getHeaders() as $name => $value) { | |
header($name.': '.$value, false); | |
} | |
} | |
echo $exception->getAsString(); | |
} |
Just to provide a default new FileLinkFormatter()
object to the HtmlErrorRenderer
renderer.
$handler->setExceptionHandler(function renderException(\Throwable $exception) use($debug): void
{
$renderer = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliErrorRenderer() : new HtmlErrorRenderer(
$debug,
null,
new FileLinkFormatter() // <-- HERE
);
$exception = $renderer->render($exception);
if (!headers_sent()) {
http_response_code($exception->getStatusCode());
foreach ($exception->getHeaders() as $name => $value) {
header($name.': '.$value, false);
}
}
echo $exception->getAsString();
});
And this will only work for Html errors since the CliErrorRenderer will use the var-dumper
component.
var-dumper
In a non Symfony app, the var-dumper
will almost always fall into the default case here:
symfony/src/Symfony/Component/VarDumper/VarDumper.php
Lines 91 to 92 in 42d43b7
default: | |
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? new CliDumper() : new HtmlDumper(); |
If you want your links to be fully functional (e.g. path mapping replacements working in a docker context), you'll have to override the handler:
$cloner = new VarCloner();
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
$dumper = in_array(PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
$dumper->setDisplayOptions(['fileLinkFormat' => new FileLinkFormatter()]);
// Configure VarDumper
VarDumper::setHandler(function ($var, ?string $label = null) use ($cloner, $dumper): void {
$var = $cloner->cloneVar($var);
if (null !== $label) {
$var = $var->withContext(['label' => $label]);
}
$dumper->dump($var);
});
It would nice to make fileLinkFormat more easily consistent/configurable when used outside Symfony.
Example
No response