Skip to content

Commit a7de18b

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 e068619 + f36f415 commit a7de18b

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Extension/CodeExtension.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ class CodeExtension extends \Twig_Extension
2525
/**
2626
* Constructor.
2727
*
28-
* @param string $fileLinkFormat The format for links to source files
29-
* @param string $rootDir The project root directory
30-
* @param string $charset The charset
28+
* @param string|array $fileLinkFormat The format for links to source files
29+
* @param string $rootDir The project root directory
30+
* @param string $charset The charset
3131
*/
3232
public function __construct($fileLinkFormat, $rootDir, $charset)
3333
{
34-
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
34+
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
35+
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
36+
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
37+
$i = strpos($fileLinkFormat, '#', $i) ?: strlen($fileLinkFormat);
38+
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
39+
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
40+
}
41+
$this->fileLinkFormat = $fileLinkFormat;
3542
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
3643
$this->charset = $charset;
3744
}
@@ -190,8 +197,15 @@ public function formatFile($file, $line, $text = null)
190197
*/
191198
public function getFileLink($file, $line)
192199
{
193-
if ($this->fileLinkFormat && is_file($file)) {
194-
return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
200+
if ($this->fileLinkFormat && file_exists($file)) {
201+
foreach ($this->fileLinkFormat[1] as $k => $v) {
202+
if (0 === strpos($file, $k)) {
203+
$file = substr_replace($file, $v, 0, strlen($k));
204+
break;
205+
}
206+
}
207+
208+
return strtr($this->fileLinkFormat[0], array('%f' => $file, '%l' => $line));
195209
}
196210

197211
return false;

Tests/Extension/CodeExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CodeExtensionTest extends \PHPUnit_Framework_TestCase
1919

2020
public function testFormatFile()
2121
{
22-
$expected = sprintf('<a href="txmt://open?url=file://%s&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', __FILE__, __FILE__);
22+
$expected = sprintf('<a href="proto://foobar%s#&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', substr(__FILE__, 5), __FILE__);
2323
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
2424
}
2525

@@ -64,6 +64,6 @@ public function testGetName()
6464

6565
protected function getExtension()
6666
{
67-
return new CodeExtension('txmt://open?url=file://%f&line=%l', '/root', 'UTF-8');
67+
return new CodeExtension('proto://%f#&line=%l#'.substr(__FILE__, 0, 5).'=foobar', '/root', 'UTF-8');
6868
}
6969
}

0 commit comments

Comments
 (0)