[TwigBridge] Remove $rootDir argument in CodeExtension

This commit is contained in:
Roland Franssen 2018-10-24 13:30:46 +02:00
parent a6e4d4abef
commit f8947242a9
4 changed files with 15 additions and 16 deletions

View File

@ -23,21 +23,19 @@ use Twig\TwigFilter;
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 @@ class CodeExtension extends AbstractExtension
$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('<abbr title="%s%2$s">%s</abbr>%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('<abbr title="%s%2$s">%s</abbr>%s', $this->projectDir, $rel[0], '/'.($rel[1] ?? ''));
}
}
@ -214,8 +211,10 @@ class CodeExtension extends AbstractExtension
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;

View File

@ -25,7 +25,7 @@ class CodeExtensionTest extends TestCase
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 @@ class CodeExtensionTest extends TestCase
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');
}
}

View File

@ -84,9 +84,8 @@
<service id="twig.extension.code" class="Symfony\Bridge\Twig\Extension\CodeExtension">
<tag name="twig.extension" />
<argument type="service" id="debug.file_link_formatter" on-invalid="ignore" />
<argument>%kernel.root_dir%</argument>
<argument>%kernel.charset%</argument>
<argument>%kernel.project_dir%</argument>
<argument>%kernel.charset%</argument>
</service>
<service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension">

View File

@ -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",