Skip to content

Commit

Permalink
feature #19807 [FrameworkBundle] Add %debug.file_link_format% with re…
Browse files Browse the repository at this point in the history
…mapping for IDE links (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links

| Q             | A
| ------------- | ---
| Branch?       | master
| New feature?  | yes
| Tests pass?   | yes
| Fixed tickets | #14340
| License       | MIT
| Doc PR        | symfony/symfony-docs#6944

`templating.helper.code.file_link_format` is a parameter that requires templating to be defined, but holds a concept that is used beyond templating borders.
Let's make it a general parameter that can be injected easily when required.

Commits
-------

1c4ca8c [FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links
  • Loading branch information
fabpot committed Sep 14, 2016
2 parents e068619 + f36f415 commit a7de18b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ class CodeExtension extends \Twig_Extension
/**
* Constructor.
*
* @param string $fileLinkFormat The format for links to source files
* @param string $rootDir The project root directory
* @param string $charset The charset
* @param string|array $fileLinkFormat The format for links to source files
* @param string $rootDir The project root directory
* @param string $charset The charset
*/
public function __construct($fileLinkFormat, $rootDir, $charset)
{
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
}
$this->fileLinkFormat = $fileLinkFormat;
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
$this->charset = $charset;
}
Expand Down Expand Up @@ -190,8 +197,15 @@ public function formatFile($file, $line, $text = null)
*/
public function getFileLink($file, $line)
{
if ($this->fileLinkFormat && is_file($file)) {
return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
if ($this->fileLinkFormat && file_exists($file)) {
foreach ($this->fileLinkFormat[1] as $k => $v) {
if (0 === strpos($file, $k)) {
$file = substr_replace($file, $v, 0, strlen($k));
break;
}
}

return strtr($this->fileLinkFormat[0], array('%f' => $file, '%l' => $line));
}

return false;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Extension/CodeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CodeExtensionTest extends \PHPUnit_Framework_TestCase

public function testFormatFile()
{
$expected = sprintf('<a href="txmt://open?url=file://%s&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', __FILE__, __FILE__);
$expected = sprintf('<a href="proto://foobar%s#&amp;line=25" title="Click to open this file" class="file_link">%s at line 25</a>', substr(__FILE__, 5), __FILE__);
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
}

Expand Down Expand Up @@ -64,6 +64,6 @@ public function testGetName()

protected function getExtension()
{
return new CodeExtension('txmt://open?url=file://%f&line=%l', '/root', 'UTF-8');
return new CodeExtension('proto://%f#&line=%l#'.substr(__FILE__, 0, 5).'=foobar', '/root', 'UTF-8');
}
}

0 comments on commit a7de18b

Please sign in to comment.