Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fileLinkFormat / SYMFONY_IDE configuration outside Symfony #50619

Closed
nlemoine opened this issue Jun 9, 2023 · 5 comments · Fixed by #50734
Closed

Improve fileLinkFormat / SYMFONY_IDE configuration outside Symfony #50619

nlemoine opened this issue Jun 9, 2023 · 5 comments · Fixed by #50734

Comments

@nlemoine
Copy link
Contributor

nlemoine commented Jun 9, 2023

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

@ro0NL
Copy link
Contributor

ro0NL commented Jun 9, 2023

path mapping won't work with the only SYMFONY_IDE env var set

are you sure? it works for me in framework context, but it should work everywhere IMHO

we havent configured framework.ide either since #44575

@nlemoine
Copy link
Contributor Author

nlemoine commented Jun 9, 2023

Just tested and confirm path mapping won't work in standalone mode:

<?php

$_ENV['SYMFONY_IDE'] = 'vscode://file/%f:%l&/user/name/sites/issue-50619/>/projects/my_project/'; # Don't work :(
// $_ENV['SYMFONY_IDE'] = 'vscode://file/%f:%l'; # Works fine :)
// $_ENV['SYMFONY_IDE'] = 'vscode'; # Works fine :)

use Symfony\Component\ErrorHandler\Debug;

require_once 'vendor/autoload.php';

Debug::enable();

trigger_error('This is an error', E_USER_ERROR);

// Getting link
// vscode://file//user/name/sites/issue-50619/index.php:13&/user/name/sites/issue-50619/>/projects/my_project/

but it should work everywhere IMHO

That's what I think too 🙂

@ro0NL

This comment was marked as resolved.

@ro0NL
Copy link
Contributor

ro0NL commented Jun 9, 2023

the issue is in HtmlErrorRenderer

fix:

- $fileLinkFormat ??= $_ENV['SYMFONY_IDE'] ?? $_SERVER['SYMFONY_IDE'] ?? null;
+ $fileLinkFormat ??= new FileLinkFormatter();

@nlemoine
Copy link
Contributor Author

nlemoine commented Jun 9, 2023

This one resolves the issue with the error-handler in a non cli context only.

Maybe the same kind of logic could be applied to the var-dumper component?

nlemoine added a commit to nlemoine/symfony that referenced this issue Jun 21, 2023
- Avoid repeating file link format guessing (logic is already in FileLinkFormatter class)
- Always set a fileLinkFormat to a FileLinkFormatter object to handle path mappings properly
nlemoine added a commit to nlemoine/symfony that referenced this issue Jun 21, 2023
- Avoid repeating file link format guessing (logic is already in FileLinkFormatter class)
- Always set a fileLinkFormat to a FileLinkFormatter object to handle path mappings properly
nlemoine added a commit to nlemoine/symfony that referenced this issue Jun 21, 2023
- Avoid repeating file link format guessing (logic is already in FileLinkFormatter class)
- Always set a fileLinkFormat to a FileLinkFormatter object to handle path mappings properly
nlemoine added a commit to nlemoine/symfony that referenced this issue Jun 21, 2023
- Avoid repeating file link format guessing (logic is already in FileLinkFormatter class)
- Always set a fileLinkFormat to a FileLinkFormatter object to handle path mappings properly
@fabpot fabpot closed this as completed in d30597d Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants