diff --git a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php
index cd00f44d57b00..d9a6cccc41d9b 100644
--- a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php
+++ b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php
@@ -23,21 +23,19 @@
class CodeExtension extends AbstractExtension
{
private $fileLinkFormat;
- private $rootDir;
private $charset;
private $projectDir;
/**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
- * @param string $rootDir The project root directory
+ * @param string $projectDir The project directory
* @param string $charset The charset
*/
- public function __construct($fileLinkFormat, string $rootDir, string $charset, string $projectDir = null)
+ public function __construct($fileLinkFormat, string $projectDir, string $charset)
{
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
- $this->rootDir = str_replace('/', \DIRECTORY_SEPARATOR, \dirname($rootDir)).\DIRECTORY_SEPARATOR;
+ $this->projectDir = str_replace('\\', '/', $projectDir).'/';
$this->charset = $charset;
- $this->projectDir = $projectDir;
}
/**
@@ -176,11 +174,10 @@ public function formatFile($file, $line, $text = null)
$file = trim($file);
if (null === $text) {
- $text = str_replace('/', \DIRECTORY_SEPARATOR, $file);
- if (0 === strpos($text, $this->rootDir)) {
- $text = substr($text, \strlen($this->rootDir));
- $text = explode(\DIRECTORY_SEPARATOR, $text, 2);
- $text = sprintf('%s%s', $this->rootDir, $text[0], isset($text[1]) ? \DIRECTORY_SEPARATOR.$text[1] : '');
+ $text = $file;
+ if (null !== $rel = $this->getFileRelative($text)) {
+ $rel = explode('/', $rel, 2);
+ $text = sprintf('%s%s', $this->projectDir, $rel[0], '/'.($rel[1] ?? ''));
}
}
@@ -214,8 +211,10 @@ public function getFileLink($file, $line)
public function getFileRelative(string $file): ?string
{
+ $file = str_replace('\\', '/', $file);
+
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
- return ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
+ return ltrim(substr($file, \strlen($this->projectDir)), '/');
}
return null;
diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
index c152ba6d60137..2e95fe5845fb7 100644
--- a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
+++ b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
@@ -25,7 +25,7 @@ public function testFormatFile()
public function testFileRelative()
{
- $this->assertEquals('CodeExtensionTest.php', $this->getExtension()->getFileRelative(__FILE__));
+ $this->assertEquals('file.txt', $this->getExtension()->getFileRelative(\DIRECTORY_SEPARATOR.'project'.\DIRECTORY_SEPARATOR.'file.txt'));
}
/**
@@ -69,6 +69,6 @@ public function testGetName()
protected function getExtension()
{
- return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8', __DIR__);
+ return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), \DIRECTORY_SEPARATOR.'project', 'UTF-8');
}
}
diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
index 02175bc16da0c..8e29ae28d5c94 100644
--- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
+++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
@@ -84,9 +84,8 @@
- %kernel.root_dir%
- %kernel.charset%
%kernel.project_dir%
+ %kernel.charset%
diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json
index 2ff3a8ea9ee3a..d9526cf7d5411 100644
--- a/src/Symfony/Bundle/TwigBundle/composer.json
+++ b/src/Symfony/Bundle/TwigBundle/composer.json
@@ -18,7 +18,7 @@
"require": {
"php": "^7.1.3",
"symfony/config": "~4.2",
- "symfony/twig-bridge": "^3.4.3|^4.0.3",
+ "symfony/twig-bridge": "^4.2",
"symfony/http-foundation": "~4.1",
"symfony/http-kernel": "~4.1",
"symfony/polyfill-ctype": "~1.8",
@@ -33,6 +33,7 @@
"symfony/form": "~3.4|~4.0",
"symfony/routing": "~3.4|~4.0",
"symfony/templating": "~3.4|~4.0",
+ "symfony/translation": "^4.2",
"symfony/yaml": "~3.4|~4.0",
"symfony/framework-bundle": "~4.1",
"symfony/web-link": "~3.4|~4.0",