Skip to content

Commit f36f415

Browse files
[FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links
1 parent 2c60af4 commit f36f415

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)