Skip to content

Commit d432756

Browse files
[FrameworkBundle] Add %debug.file_link_format% with remapping for IDE links
1 parent dd96652 commit d432756

File tree

5 files changed

+39
-27
lines changed

5 files changed

+39
-27
lines changed

DependencyInjection/FrameworkExtension.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ public function load(array $configs, ContainerBuilder $container)
9090
$container->setParameter('kernel.trusted_proxies', $config['trusted_proxies']);
9191
$container->setParameter('kernel.default_locale', $config['default_locale']);
9292

93+
if (!$container->hasParameter('debug.file_link_format')) {
94+
if (!$container->hasParameter('templating.helper.code.file_link_format')) {
95+
$links = array(
96+
'textmate' => 'txmt://open?url=file://%%f&line=%%l',
97+
'macvim' => 'mvim://open?url=file://%%f&line=%%l',
98+
'emacs' => 'emacs://open?url=file://%%f&line=%%l',
99+
'sublime' => 'subl://open?url=file://%%f&line=%%l',
100+
);
101+
$ide = $config['ide'];
102+
103+
$container->setParameter('templating.helper.code.file_link_format', str_replace('%', '%%', ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')) ?: (isset($links[$ide]) ? $links[$ide] : $ide));
104+
}
105+
$container->setParameter('debug.file_link_format', '%templating.helper.code.file_link_format%');
106+
}
107+
93108
if (!empty($config['test'])) {
94109
$loader->load('test.xml');
95110
}
@@ -120,7 +135,7 @@ public function load(array $configs, ContainerBuilder $container)
120135
}
121136

122137
if ($this->isConfigEnabled($container, $config['templating'])) {
123-
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader);
138+
$this->registerTemplatingConfiguration($config['templating'], $container, $loader);
124139
}
125140

126141
$this->registerValidationConfiguration($config['validation'], $container, $loader);
@@ -431,11 +446,6 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
431446
}
432447

433448
$definition->replaceArgument(4, $debug);
434-
435-
if ($container->hasParameter('templating.helper.code.file_link_format')) {
436-
$definition->replaceArgument(5, '%templating.helper.code.file_link_format%');
437-
}
438-
439449
$definition->replaceArgument(6, $debug);
440450
}
441451

@@ -553,25 +563,13 @@ private function registerRequestConfiguration(array $config, ContainerBuilder $c
553563
* Loads the templating configuration.
554564
*
555565
* @param array $config A templating configuration array
556-
* @param string $ide
557566
* @param ContainerBuilder $container A ContainerBuilder instance
558567
* @param XmlFileLoader $loader An XmlFileLoader instance
559568
*/
560-
private function registerTemplatingConfiguration(array $config, $ide, ContainerBuilder $container, XmlFileLoader $loader)
569+
private function registerTemplatingConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
561570
{
562571
$loader->load('templating.xml');
563572

564-
if (!$container->hasParameter('templating.helper.code.file_link_format')) {
565-
$links = array(
566-
'textmate' => 'txmt://open?url=file://%%f&line=%%l',
567-
'macvim' => 'mvim://open?url=file://%%f&line=%%l',
568-
'emacs' => 'emacs://open?url=file://%%f&line=%%l',
569-
'sublime' => 'subl://open?url=file://%%f&line=%%l',
570-
);
571-
572-
$container->setParameter('templating.helper.code.file_link_format', str_replace('%', '%%', ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')) ?: (isset($links[$ide]) ? $links[$ide] : $ide));
573-
}
574-
575573
$container->setParameter('fragment.renderer.hinclude.global_template', $config['hinclude_default_template']);
576574

577575
if ($container->getParameter('kernel.debug')) {

Resources/config/debug_prod.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<argument>-1</argument><!-- Log levels map for enabled error levels -->
1818
<argument>%debug.error_handler.throw_at%</argument>
1919
<argument>true</argument>
20-
<argument>null</argument><!-- %templating.helper.code.file_link_format% -->
20+
<argument>%debug.file_link_format%</argument>
2121
<argument>true</argument>
2222
</service>
2323

Resources/config/templating_php.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
<service id="templating.helper.code" class="Symfony\Bundle\FrameworkBundle\Templating\Helper\CodeHelper">
4646
<tag name="templating.helper" alias="code" />
47-
<argument>%templating.helper.code.file_link_format%</argument>
47+
<argument>%debug.file_link_format%</argument>
4848
<argument>%kernel.root_dir%</argument>
4949
<argument>%kernel.charset%</argument>
5050
</service>

Templating/Helper/CodeHelper.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@ class CodeHelper extends Helper
2727
/**
2828
* Constructor.
2929
*
30-
* @param string $fileLinkFormat The format for links to source files
31-
* @param string $rootDir The project root directory
32-
* @param string $charset The charset
30+
* @param string|array $fileLinkFormat The format for links to source files
31+
* @param string $rootDir The project root directory
32+
* @param string $charset The charset
3333
*/
3434
public function __construct($fileLinkFormat, $rootDir, $charset)
3535
{
36-
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
36+
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
37+
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
38+
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
39+
$i = strpos($fileLinkFormat, '#', $i) ?: strlen($fileLinkFormat);
40+
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
41+
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
42+
}
43+
$this->fileLinkFormat = $fileLinkFormat;
3744
$this->rootDir = str_replace('\\', '/', $rootDir).'/';
3845
$this->charset = $charset;
3946
}
@@ -186,7 +193,14 @@ public function formatFile($file, $line, $text = null)
186193
public function getFileLink($file, $line)
187194
{
188195
if ($this->fileLinkFormat && is_file($file)) {
189-
return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
196+
foreach ($this->fileLinkFormat[1] as $k => $v) {
197+
if (0 === strpos($file, $k)) {
198+
$file = substr_replace($file, $v, 0, strlen($k));
199+
break;
200+
}
201+
}
202+
203+
return strtr($this->fileLinkFormat[0], array('%f' => $file, '%l' => $line));
190204
}
191205

192206
return false;

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function testFileLinkFormat()
359359
{
360360
$container = $this->createContainerFromFile('full');
361361

362-
$this->assertEquals('file%link%format', $container->getParameter('templating.helper.code.file_link_format'));
362+
$this->assertEquals('file%link%format', $container->getParameter('debug.file_link_format'));
363363
}
364364

365365
public function testValidationAnnotations()

0 commit comments

Comments
 (0)