8000 feature #19807 [FrameworkBundle] Add %debug.file_link_format% with re… · symfony/symfony@059b436 · GitHub
[go: up one dir, main page]

Skip to content

Commit 059b436

Browse files
committed
feature #19807 [FrameworkBundle] Add %debug.file_link_format% with remapping 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
2 parents 277c7fa + 1c4ca8c commit 059b436

File tree

20 files changed

+143
-76
lines changed

20 files changed

+143
-76
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@ class CodeExtension extends \Twig_Extension
2525
/**
2626
* Constructor.
2727
*
28-
* @param string $fileLinkFormat The format for links to source files
29-
* @param string $rootDir The project root directory
30-
* @param string $charset The charset
28+
* @param string|array $fileLinkFormat The format for links to source files
29+
* @param string $rootDir The project root directory
30+
* @param string $charset The charset
3131
*/
3232
public function __construct($fileLinkFormat, $rootDir, $charset)
3333
{
34-
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
34+
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
35+
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
36+
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
37+
$i = strpos($fileLinkFormat, '#', $i) ?: strlen($fileLinkFormat);
38+
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
39+
parse_str($fileLinkFormat[1], $fileLinkFormat[1]);
40+
}
41+
$this->fileLinkFormat = $fileLinkFormat;
3542
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
3643
$this->charset = $charset;
3744
}
@@ -190,8 +197,15 @@ public function formatFile($file, $line, $text = null)
190197
*/
191198
public function getFileLink($file, $line)
192199
{
193-
if ($this->fileLinkFormat && is_file($file)) {
194-
return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
200+
if ($this->fileLinkFormat && file_exists($file)) {
201+
foreach ($this->fileLinkFormat[1] as $k => $v) {
202+
if (0 === strpos($file, $k)) {
203+
$file = substr_replace($file, $v, 0, strlen($k));
204+
break;
205+
}
206+
}
207+
208+
return strtr($this->fileLinkFormat[0], array('%f' => $file, '%l' => $line));
195209
}
196210

197211
return false;

src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CodeExtensionTest extends \PHPUnit_Framework_TestCase
1919

2020
public function testFormatFile()
2121
{
22-
$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__);
22+
$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__);
2323
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
2424
}
2525

@@ -64,6 +64,6 @@ public function testGetName()
6464

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

src/Symfony/Bundle/DebugBundle/DependencyInjection/Compiler/DumpDataCollectorPass.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public function process(ContainerBuilder $container)
3333

3434
$definition = $container->getDefinition('data_collector.dump');
3535

36-
if ($container->hasParameter('templating.helper.code.file_link_format')) {
37-
$definition->replaceArgument(1, $container->getParameter('templating.helper.code.file_link_format'));
38-
}
39-
4036
if (!$container->hasParameter('web_profiler.debug_toolbar.mode') || WebDebugToolbarListener::DISABLED === $container->getParameter('web_profiler.debug_toolbar.mode')) {
4137
$definition->replaceArgument(3, null);
4238
}

src/Symfony/Bundle/DebugBundle/Resources/config/services.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
<service id="twig.extension.dump" class="Symfony\Bridge\Twig\Extension\DumpExtension" public="false">
99
<tag name="twig.extension" />
1010
<argument type="service" id="var_dumper.cloner" />
11+
<argument type="service" id="var_dumper.html_dumper" />
1112
</service>
1213

1314
<service id="data_collector.dump" class="Symfony\Component\HttpKernel\DataCollector\DumpDataCollector">
1415
<tag name="data_collector" id="dump" template="@Debug/Profiler/dump.html.twig" priority="240" />
1516
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
16-
<argument>null</argument><!-- %templating.helper.code.file_link_format% -->
17+
<argument>%debug.file_link_format%</argument>
1718
<argument>%kernel.charset%</argument>
1819
<argument type="service" id="request_stack" />
1920
<argument>null</argument><!-- var_dumper.cli_dumper when debug.dump_destination is set -->
@@ -29,6 +30,17 @@
2930
<service id="var_dumper.cli_dumper" class="Symfony\Component\VarDumper\Dumper\CliDumper">
3031
<argument>null</argument><!-- debug.dump_destination -->
3132
<argument>%kernel.charset%</argument>
33+
<argument>0</argument> <!-- flags -->
34+
</service>
35+
<service id="var_dumper.html_dumper" class="Symfony\Component\VarDumper\Dumper\HtmlDumper" public="false">
36+
<argument>null</argument>
37+
<argument>%kernel.charset%</argument>
38+
<argument>0</argument> <!-- flags -->
39+
<call method="setDisplayOptions">
40+
<argument type="collection">
41+
<argument key="fileLinkFormat">%debug.file_link_format%</argument>
42+
</argument>
43+
</call>
3244
</service>
3345
</services>
3446

src/Symfony/Bundle/DebugBundle/Tests/DependencyInjection/Compiler/DumpDataCollectorPassTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@
1919

2020
class DumpDataCollectorPassTest extends \PHPUnit_Framework_TestCase
2121
{
22-
public function testProcessWithFileLinkFormatParameter()
23-
{
24-
$container = new ContainerBuilder();
25-
$container->addCompilerPass(new DumpDataCollectorPass());
26-
$container->setParameter('templating.helper.code.file_link_format', 'file-link-format');
27-
28-
$definition = new Definition('Symfony\Component\HttpKernel\DataCollector\DumpDataCollector', array(null, null, null, null));
29-
$container->setDefinition('data_collector.dump', $definition);
30-
31-
$container->compile();
32-
33-
$this->assertSame('file-link-format', $definition->getArgument(1));
34-
}
35-
3622
public function testProcessWithoutFileLinkFormatParameter()
3723
{
3824
$container = new ContainerBuilder();

src/Symfony/Bundle/FrameworkBundle/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')) {

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php

Lines changed: 19 additions & 5 deletions
< 10000 tr class="diff-line-row">
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;

src/Symfony/Bundle/FrameworkBundle/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()

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ public function process(ContainerBuilder $container)
5555
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
5656
}
5757

58-
if ($container->hasParameter('templating.helper.code.file_link_format')) {
59-
$container->getDefinition('twig.extension.code')->replaceArgument(0, $container->getParameter('templating.helper.code.file_link_format'));
60-
}
61-
6258
if ($container->getParameter('kernel.debug')) {
6359
$container->getDefinition('twig.extension.profiler')->addTag('twig.extension');
6460
$container->getDefinition('twig.extension.debug')->addTag('twig.extension');

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

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

8484
<service id="twig.extension.code" class="Symfony\Bridge\Twig\Extension\CodeExtension" public="false">
8585
<tag name="twig.extension" />
86-
<argument /> <!-- %templating.helper.code.file_link_format% -->
86+
<argument>%debug.file_link_format%</argument>
8787
<argument>%kernel.root_dir%</argument>
8888
<argument>%kernel.charset%</argument>
8989
</service>

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"symfony/routing": "~2.8|~3.0",
3232
"symfony/templating": "~2.8|~3.0",
3333
"symfony/yaml": "~2.8|~3.0",
34-
"symfony/framework-bundle": "~2.8|~3.0"
34+
"symfony/framework-bundle": "~3.2"
3535
},
3636
"autoload": {
3737
"psr-4": { "Symfony\\Bundle\\TwigBundle\\": "" },

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434

3535
<service id="twig.extension.webprofiler" class="Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension" public="false">
3636
<tag name="twig.extension" />
37+
<argument type="service">
38+
<service class="Symfony\Component\VarDumper\Dumper\HtmlDumper">
39+
<argument>null</argument>
40+
<argument>%kernel.charset%</argument>
41+
<argument type="constant">Symfony\Component\VarDumper\Dumper\HtmlDumper::DUMP_LIGHT_ARRAY</argument>
42+
<call method="setDisplayOptions">
43+
<argument type="collection">
44+
<argument key="fileLinkFormat">%debug.file_link_format%</argument>
45+
</argument>
46+
</call>
47+
</service>
48+
</argument>
3749
</service>
3850
</services>
3951
</container>

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected function setUp()
5656
$this->container->setParameter('kernel.cache_dir', __DIR__);
5757
$this->container->setParameter('kernel.debug', false);
5858
$this->container->setParameter('kernel.root_dir', __DIR__);
59+
$this->container->setParameter('kernel.charset', 'UTF-8');
60+
$this->container->setParameter('debug.file_link_format', null);
5961
$this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
6062
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
6163
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));

src/Symfony/Bundle/WebProfilerBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"symfony/http-kernel": "~3.1",
2121
"symfony/polyfill-php70": "~1.0",
2222
"symfony/routing": "~2.8|~3.0",
23-
"symfony/twig-bridge": "~2.8|~3.0"
23+
"symfony/twig-bridge": "~2.8|~3.0",
24+
"symfony/var-dumper": "~3.2"
2425
},
2526
"require-dev": {
2627
"symfony/config": "~2.8|~3.0",

0 commit comments

Comments
 (0)
0