8000 [Debug][WebProfilerBundle] Fix setting file link format by nicolas-grekas · Pull Request #27074 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Debug][WebProfilerBundle] Fix setting file link format #27074

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

Merged
merged 2 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ public function formatFile($file, $line, $text = null)
}
}

$text = "$text at line $line";
if (0 < $line) {
$text .= ' at line '.$line;
}

if (false !== $link = $this->getFileLink($file, $line)) {
return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset), $text);
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class FrameworkBundle extends Bundle
{
public function boot()
{
if (!ini_get('xdebug.file_link_format') && !get_cfg_var('xdebug.file_link_format')) {
ini_set('xdebug.file_link_format', $this->container->getParameter('debug.file_link_format'));
}
ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);

if ($this->container->hasParameter('kernel.trusted_proxies')) {
Expand Down
< 8000 tr data-hunk="34803e9894b8800aedd2bc727528f9feaef48cb74d2aab4f08db9f393b8c8379" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\WebProfilerBundle\Controller;

use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -30,11 +31,12 @@ class ExceptionController
protected $debug;
protected $profiler;

public function __construct(Profiler $profiler = null, Environment $twig, $debug)
public function __construct(Profiler $profiler = null, Environment $twig, $debug, FileLinkFormatter $fileLinkFormat = null)
{
$this->profiler = $profiler;
$this->twig = $twig;
$this->debug = $debug;
$this->fileLinkFormat = $fileLinkFormat;
}

/**
Expand All @@ -58,7 +60,7 @@ public function showAction($token)
$template = $this->getTemplate();

if (!$this->twig->getLoader()->exists($template)) {
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset(), $this->fileLinkFormat);

return new Response($handler->getContent($exception), 200, array('Content-Type' => 'text/html'));
}
Expand Down Expand Up @@ -98,7 +100,7 @@ public function cssAction($token)
$template = $this->getTemplate();

if (!$this->templateExists($template)) {
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset());
$handler = new ExceptionHandler($this->debug, $this->twig->getCharset(), $this->fileLinkFormat);

return new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<argument type="service" id="profiler" on-invalid="null" />
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
<argument type="service" id="debug.file_link_formatter" />
</service>

<service id="web_profiler.csp.handler" class="Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% block body %}
<div class="header">
<h1>{{ file }} <small>line {{ line }}</small></h1> 8000
<h1>{{ file }}{% if 0 < line %} <small>line {{ line }}</small>{% endif %}</h1>
<a class="doc" href="https://symfony.com/doc/{{ constant('Symfony\\Component\\HttpKernel\\Kernel::VERSION') }}/reference/configuration/framework.html#ide" rel="help">Open in your IDE?</a>
</div>
<div class="source">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class WebProfilerExtensionTest extends TestCase
public static function assertSaneContainer(Container $container, $message = '', $knownPrivates = array())
{
$errors = array();
$knownPrivates[] = 'debug.file_link_formatter.url_format';
foreach ($container->getServiceIds() as $id) {
if (in_array($id, $knownPrivates, true)) { // to be removed in 4.0
continue;
Expand Down
26 changes: 21 additions & 5 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct($debug = true, $charset = null, $fileLinkFormat = nu
{
$this->debug = $debug;
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->fileLinkFormat = $fileLinkFormat;
}

/**
Expand Down Expand Up @@ -355,13 +355,29 @@ private function formatClass($class)
private function formatPath($path, $line)
{
$file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path);
$fmt = $this->fileLinkFormat;
$fmt = $this->fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');

if (!$fmt) {
return sprintf('<span class="block trace-file-path">in <a title="%s%3$s"><strong>%s</strong>%s</a></span>', $this->escapeHtml($path), $file, 0 < $line ? ' line '.$line : '');
}

if (\is_string($fmt)) {
$i = strpos($f = $fmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fmt = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);

for ($i = 1; isset($fmt[$i]); ++$i) {
if (0 === strpos($path, $k = $fmt[$i++])) {
$path = substr_replace($path, $fmt[$i], 0, strlen($k));
break;
}
}

if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $path, '%l' => $line)) : $fmt->format($path, $line)) {
return sprintf('<span class="block trace-file-path">in <a href="%s" title="Go to source">%s (line %d)</a></span>', $this->escapeHtml($link), $file, $line);
$link = strtr($fmt[0], array('%f' => $path, '%l' => $line));
} else {
$link = $fmt->format($path, $line);
}

return sprintf('<span class="block trace-file-path">in <a title="%s line %3$d"><strong>%s</strong> (line %d)</a></span>', $this->escapeHtml($path), $file, $line);
return sprintf('<span class="block trace-file-path">in <a href="%s" title="Go to source"><strong>%s</string>%s</a></span>', $this->escapeHtml($link), $file, 0 < $line ? ' line '.$line : '');
}

/**
Expand Down
0