@@ -25,17 +25,19 @@ class CodeExtension extends AbstractExtension
25
25
private $ fileLinkFormat ;
26
26
private $ rootDir ;
27
27
private $ charset ;
28
+ private $ projectDir ;
28
29
29
30
/**
30
31
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
31
32
* @param string $rootDir The project root directory
32
33
* @param string $charset The charset
33
34
*/
34
- public function __construct ($ fileLinkFormat , string $ rootDir , string $ charset )
35
+ public function __construct ($ fileLinkFormat , string $ rootDir , string $ charset, string $ projectDir = null )
35
36
{
36
37
$ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
37
38
$ this ->rootDir = str_replace ('/ ' , DIRECTORY_SEPARATOR , dirname ($ rootDir )).DIRECTORY_SEPARATOR ;
38
39
$ this ->charset = $ charset ;
40
+ $ this ->projectDir = $ projectDir ;
39
41
}
40
42
41
43
/**
@@ -53,6 +55,7 @@ public function getFilters()
53
55
new TwigFilter ('format_file_from_text ' , array ($ this , 'formatFileFromText ' ), array ('is_safe ' => array ('html ' ))),
54
56
new TwigFilter ('format_log_message ' , array ($ this , 'formatLogMessage ' ), array ('is_safe ' => array ('html ' ))),
55
57
new TwigFilter ('file_link ' , array ($ this , 'getFileLink ' )),
58
+ new TwigFilter ('file_relative ' , array ($ this , 'getFileRelative ' )),
56
59
);
57
60
}
58
61
@@ -209,6 +212,15 @@ public function getFileLink($file, $line)
209
212
return false ;
210
213
}
211
214
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
+
212
224
public function formatFileFromText ($ text )
213
225
{
214
226
return preg_replace_callback ('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s ' , function ($ match ) {
0 commit comments