8000 bug #28967 [TwigBridge] Remove $rootDir argument in CodeExtension (r… · symfony/symfony@dd31ca8 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd31ca8

Browse files
committed
bug #28967 [TwigBridge] Remove $rootDir argument in CodeExtension (ro0NL)
This PR was merged into the 4.2-dev branch. Discussion ---------- [TwigBridge] Remove $rootDir argument in CodeExtension | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | symfony/symfony-docs#10547 (comment) | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Forgotten in #28890 cc @javiereguiluz Commits ------- f894724 [TwigBridge] Remove $rootDir argument in CodeExtension
2 parents 9c8d099 + f894724 commit dd31ca8

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/Symfony/Bridge/Twig/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;

src/Symfony/Bridge/Twig/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
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@
8484
<service id="twig.extension.code" class="Symfony\Bridge\Twig\Extension\CodeExtension">
8585
<tag name="twig.extension" />
8686
<argument type="service" id="debug.file_link_formatter" on-invalid="ignore" />
87-
<a 57AE rgument>%kernel.root_dir%</argument>
88-
<argument>%kernel.charset%</argument>
8987
<argument>%kernel.project_dir%</argument>
88+
<argument>%kernel.charset%</argument>
9089
</service>
9190

9291
<service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension">

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/config": "~4.2",
21-
"symfony/twig-bridge": "^3.4.3|^4.0.3",
21+
"symfony/twig-bridge": "^4.2",
2222
"symfony/http-foundation": "~4.1",
2323
"symfony/http-kernel": "~4.1",
2424
"symfony/polyfill-ctype": "~1.8",
@@ -33,6 +33,7 @@
3333
"symfony/form": "~3.4|~4.0",
3434
"symfony/routing": "~3.4|~4.0",
3535
"symfony/templating": "~3.4|~4.0",
36+
"symfony/translation": "^4.2",
3637
"symfony/yaml": "~3.4|~4.0",
3738
"symfony/framework-bundle": "~4.1",
3839
"symfony/web-link": "~3.4|~4.0",

0 commit comments

Comments
 (0)
0