Skip to content

Improve fileLinkFormat / SYMFONY_IDE configuration outside Symfony #50619

@nlemoine

Description

@nlemoine

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

$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:

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:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions