Skip to content

Commit 8a59714

Browse files
committed
[TwigBridge] Remove $rootDir argument in CodeExtension
1 parent a4c1c7a commit 8a59714

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

Extension/CodeExtension.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,19 @@
2323
class CodeExtension extends AbstractExtension
2424
{
2525
private $fileLinkFormat;
26-
private $rootDir;
2726
private $charset;
2827
private $projectDir;
2928

3029
/**
3130
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
32-
* @param string $rootDir The project root directory
31+
* @param string $projectDir The project directory
3332
* @param string $charset The charset
3433
*/
35-
public function __construct($fileLinkFormat, string $rootDir, string $charset, string $projectDir = null)
34+
public function __construct($fileLinkFormat, string $projectDir, string $charset)
3635
{
3736
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
38-
$this->rootDir = str_replace('/', \DIRECTORY_SEPARATOR, \dirname($rootDir)).\DIRECTORY_SEPARATOR;
37+
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
3938
$this->charset = $charset;
40-
$this->projectDir = $projectDir;
4139
}
4240

4341
/**
@@ -176,11 +174,10 @@ public function formatFile($file, $line, $text = null)
176174
$file = trim($file);
177175

178176
if (null === $text) {
179-
$text = str_replace('/', \DIRECTORY_SEPARATOR, $file);
180-
if (0 === strpos($text, $this->rootDir)) {
181-
$text = substr($text, \strlen($this->rootDir));
182-
$text = explode(\DIRECTORY_SEPARATOR, $text, 2);
183-
$text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->rootDir, $text[0], isset($text[1]) ? \DIRECTORY_SEPARATOR.$text[1] : '');
177+
$text = $file;
178+
if (null !== $rel = $this->getFileRelative($text)) {
179+
$rel = explode('/', $rel, 2);
180+
$text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->projectDir, $rel[0], '/'.($rel[1] ?? ''));
184181
}
185182
}
186183

@@ -214,8 +211,10 @@ public function getFileLink($file, $line)
214211

215212
public function getFileRelative(string $file): ?string
216213
{
214+
$file = str_replace('\\', '/', $file);
215+
217216
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
218-
return ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
217+
return ltrim(substr($file, \strlen($this->projectDir)), '/');
219218
}
220219

221220
return null;

Tests/Extension/CodeExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testFormatFile()
2525

2626
public function testFileRelative()
2727
{
28-
$this->assertEquals('CodeExtensionTest.php', $this->getExtension()->getFileRelative(__FILE__));
28+
$this->assertEquals('file.txt', $this->getExtension()->getFileRelative(\DIRECTORY_SEPARATOR.'project'.\DIRECTORY_SEPARATOR.'file.txt'));
2929
}
3030

3131
/**
@@ -69,6 +69,6 @@ public function testGetName()
6969

7070
protected function getExtension()
7171
{
72-
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8', __DIR__);
72+
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), \DIRECTORY_SEPARATOR.'project', 'UTF-8');
7373
}
7474
}

0 commit comments

Comments
 (0)