8000 [TwigBridge] Remove $rootDir argument in CodeExtension by ro0NL · Pull Request #28967 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge] Remove $rootDir argument in CodeExtension #28967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author
@ro0NL ro0NL Oct 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument was added in 4.2, so safe to remove technically.

We slightly change the behavior, only in formatFile. IMHO that's fine.

{
$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;
}

/**
Expand Down Expand Up @@ -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)) {
8000 $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] ?? ''));
}
}

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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');
}
}
3 changes: 1 addition & 2 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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="ig 8000 nore" />
<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">
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
0