Skip to content

Commit eba643e

Browse files
[Debug][WebProfilerBundle] Fix setting file link format
1 parent e1ca89b commit eba643e

File tree

7 files changed

+35
-10
lines changed

7 files changed

+35
-10
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public function formatFile($file, $line, $text = null)
181181
}
182182
}
183183

184-
$text = "$text at line $line";
184+
if (0 < $line) {
185+
$text .= ' at line '.$line;
186+
}
185187

186188
if (false !== $link = $this->getFileLink($file, $line)) {
187189
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);

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class FrameworkBundle extends Bundle
6161
{
6262
public function boot()
6363
{
64+
if (!ini_get('xdebug.file_link_format') && !get_cfg_var('xdebug.file_link_format')) {
65+
ini_set('xdebug.file_link_format', $this->container->getParameter('debug.file_link_format'));
66+
}
6467
ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
6568

6669
if ($this->container->hasParameter('kernel.trusted_proxies')) {

src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Controller;
1313

14+
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
1415
use Symfony\Component\HttpKernel\Profiler\Profiler;
1516
use Symfony\Component\Debug\ExceptionHandler;
1617
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -30,11 +31,12 @@ class ExceptionController
3031
protected $debug;
3132
protected $profiler;
3233

33-
public function __construct(Profiler $profiler = null, Environment $twig, $debug)
34+
public function __construct(Profiler $profiler = null, Environment $twig, $debug, FileLinkFormatter $fileLinkFormat = null)
3435
{
3536
$this->profiler = $profiler;
3637
$this->twig = $twig;
3738
$this->debug = $debug;
39+
$this->fileLinkFormat = $fileLinkFormat;
3840
}
3941

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

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

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

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

103105
return new Response($handler->getStylesheet($exception), 200, array('Content-Type' => 'text/css'));
104106
}

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<argument type="service" id="profiler" on-invalid="null" />
2828
<argument type="service" id="twig" />
2929
<argument>%kernel.debug%</argument>
30+
<argument type="service" id="debug.file_link_formatter" />
3031
</service>
3132

3233
<service id="web_profiler.csp.handler" class="Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
{% block body %}
1010
<div class="header">
11-
<h1>{{ file }} <small>line {{ line }}</small></h1>
11+
<h1>{{ file }}{% if 0 < line %} <small>line {{ line }}</small>{% endif %}</h1>
1212
<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>
1313
</div>
1414
<div class="source">

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class WebProfilerExtensionTest extends TestCase
3131
public static function assertSaneContainer(Container $container, $message = '', $knownPrivates = array())
3232
{
3333
$errors = array();
34+
$knownPrivates[] = 'debug.file_link_formatter.url_format';
3435
foreach ($container->getServiceIds() as $id) {
3536
if (in_array($id, $knownPrivates, true)) { // to be removed in 4.0
3637
continue;

src/Symfony/Component/Debug/ExceptionHandler.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct($debug = true, $charset = null, $fileLinkFormat = nu
4040
{
4141
$this->debug = $debug;
4242
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
43-
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
43+
$this->fileLinkFormat = $fileLinkFormat;
4444
}
4545

4646
/**
@@ -355,13 +355,29 @@ private function formatClass($class)
355355
private function formatPath($path, $line)
356356
{
357357
$file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path);
358-
$fmt = $this->fileLinkFormat;
358+
$fmt = $this->fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
359+
360+
if (!$fmt) {
361+
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 : '');
362+
}
363+
364+
if (\is_string($fmt)) {
365+
$i = strpos($f = $fmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
366+
$fmt = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
367+
368+
for ($i = 1; isset($fmt[$i]); ++$i) {
369+
if (0 === strpos($path, $k = $fmt[$i++])) {
370+
$path = substr_replace($path, $fmt[$i], 0, strlen($k));
371+
break;
372+
}
373+
}
359374

360-
if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $path, '%l' => $line)) : $fmt->format($path, $line)) {
361-
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);
375+
$link = strtr($fmt[0], array('%f' => $path, '%l' => $line));
376+
} else {
377+
$link = $fmt->format($path, $line);
362378
}
363379

364-
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);
380+
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 : '');
365381
}
366382

367383
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy