Skip to content
This repository was archived by the owner on Dec 9, 2023. It is now read-only.

Commit 122391f

Browse files
committed
feature #19807 [FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | Fixed tickets | #14340 | License | MIT | Doc PR | symfony/symfony-docs#6944 `templating.helper.code.file_link_format` is a parameter that requires templating to be defined, but holds a concept that is used beyond templating borders. Let's make it a general parameter that can be injected easily when required. Commits ------- 1c4ca8c [FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links
2 parents 555e2aa + 027685d commit 122391f

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

ExceptionHandler.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ class ExceptionHandler
3737

3838
public function __construct($debug = true, $charset = null, $fileLinkFormat = null)
3939
{
40+
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
41+
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
42+
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
43+
$i = strpos($fileLinkFormat, '#', $i) ?: strlen($fileLinkFormat);
44+
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
45+
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
46+
}
4047
$this->debug = $debug;
4148
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
42-
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
49+
$this->fileLinkFormat = $fileLinkFormat;
4350
}
4451

4552
/**
@@ -82,14 +89,22 @@ public function setHandler(callable $handler = null)
8289
/**
8390
* Sets the format for links to source files.
8491
*
85-
* @param string $format The format for links to source files
92+
* @param string|array $format The format for links to source files
8693
*
8794
* @return string The previous file link format
8895
*/
89-
public function setFileLinkFormat($format)
96+
public function setFileLinkFormat($fileLinkFormat)
9097
{
9198
$old = $this->fileLinkFormat;
92-
$this->fileLinkFormat = $format;
99+
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
100+
$i = strpos($fileLinkFormat, '#') ?: strlen($fileLinkFormat);
101+
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
102+
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
103+
}
104+
$this->fileLinkFormat = $fileLinkFormat;
105+
if ($old) {
106+
$old = $old[0].($old[1] ? '#'.http_build_query($old[1], '', '&') : '');
107+
}
93108

94109
return $old;
95110
}
@@ -350,16 +365,21 @@ private function formatClass($class)
350365

351366
private function formatPath($path, $line)
352367
{
353-
$path = $this->escapeHtml($path);
354-
$file = preg_match('#[^/\\\\]*$#', $path, $file) ? $file[0] : $path;
368+
$file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path);
355369

356-
if ($linkFormat = $this->fileLinkFormat) {
357-
$link = strtr($this->escapeHtml($linkFormat), array('%f' => $path, '%l' => (int) $line));
370+
if ($fileLinkFormat = $this->fileLinkFormat) {
371+
foreach ($fileLinkFormat[1] as $k => $v) {
372+
if (0 === strpos($path, $k)) {
373+
$path = substr_replace($path, $v, 0, strlen($k));
374+
break;
375+
}
376+
}
377+
$link = strtr($fileLinkFormat[0], array('%f' => $path, '%l' => (int) $line));
358378

359-
return sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $link, $file, $line);
379+
return sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $this->escapeHtml($link), $file, $line);
360380
}
361381

362-
return sprintf(' in <a title="%s line %3$d">%s line %d</a>', $path, $file, $line);
382+
return sprintf(' in <a title="%s line %3$d">%s line %d</a>', $this->escapeHtml($path), $file, $line);
363383
}
364384

365385
/**

0 commit comments

Comments
 (0)