Skip to content

Commit 1a54a6f

Browse files
committed
Show relative path of the template and improving panel view
1 parent fded3bb commit 1a54a6f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Extension/CodeExtension.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ class CodeExtension extends AbstractExtension
2525
private $fileLinkFormat;
2626
private $rootDir;
2727
private $charset;
28+
private $projectDir;
2829

2930
/**
3031
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
3132
* @param string $rootDir The project root directory
3233
* @param string $charset The charset
3334
*/
34-
public function __construct($fileLinkFormat, string $rootDir, string $charset)
35+
public function __construct($fileLinkFormat, string $rootDir, string $charset, string $projectDir = null)
3536
{
3637
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
3738
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
3839
$this->charset = $charset;
40+
$this->projectDir = $projectDir;
3941
}
4042

4143
/**
@@ -53,6 +55,7 @@ public function getFilters()
5355
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
5456
new TwigFilter('format_log_message', array($this, 'formatLogMessage'), array('is_safe' => array('html'))),
5557
new TwigFilter('file_link', array($this, 'getFileLink')),
58+
new TwigFilter('file_relative', array($this, 'getFileRelative')),
5659
);
5760
}
5861

@@ -209,6 +212,15 @@ public function getFileLink($file, $line)
209212
return false;
210213
}
211214

215+
public function getFileRelative(string $file): ?string
216+
{
217+
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
218+
return ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
219+
}
220+
221+
return null;
222+
}
223+
212224
public function formatFileFromText($text)
213225
{
214226
return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {

Tests/Extension/CodeExtensionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function testFormatFile()
2323
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
2424
}
2525

26+
public function testFileRelative()
27+
{
28+
$this->assertEquals('path/to/file.ext', $this->getExtension()->getFileRelative('/root/path/to/file.ext'));
29+
}
30+
2631
/**
2732
* @dataProvider getClassNameProvider
2833
*/
@@ -64,6 +69,6 @@ public function testGetName()
6469

6570
protected function getExtension()
6671
{
67-
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8');
72+
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8', '/root');
6873
}
6974
}

0 commit comments

Comments
 (0)