Skip to content

Commit 8dd763a

Browse files
committed
The controller and template file paths are now clickable links
1 parent 925e3ed commit 8dd763a

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

app/Resources/views/default/_source_code.html.twig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@
2727
</div>
2828

2929
<div class="modal-body">
30-
<h3>Controller code <small class="pull-right">{{ controller_file_path }}</small></h3>
31-
<pre><code class="php">{{ controller_source_code }}</code></pre>
30+
{% if controller %}
31+
<h3>Controller code <small class="pull-right">{{ controller.file_path|format_file(controller.starting_line) }}</small></h3>
32+
<pre><code class="php">{{ controller.source_code }}</code></pre>
33+
{% else %}
34+
<h3>Controller code</h3>
35+
<pre><code>Not available</code></pre>
36+
{% endif %}
3237

33-
<h3>Twig template code <small class="pull-right">{{ template_file_path }}</small></h3>
34-
<pre><code class="twig">{{ template_source_code }}</code></pre>
38+
<h3>Twig template code <small class="pull-right">{{ template.file_path|format_file(template.starting_line) }}</small></h3>
39+
<pre><code class="twig">{{ template.source_code }}</code></pre>
3540
</div>
3641
</div>
3742
</div>

app/config/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ imports:
1111

1212
# Basic configuration for the Symfony framework features
1313
framework:
14+
# Uncomment the 'ide' option to turn all of the file paths in an exception
15+
# page into clickable links that open the given file using your favorite IDE.
16+
# Supported values are 'textmate', 'macvim', 'emacs' and 'sublime' shortcuts
17+
# and any custom configuration string, such as: "phpstorm://open?file=%%f&line=%%l"
18+
# See http://symfony.com/doc/current/reference/configuration/framework.html#ide
19+
ide: sublime
20+
1421
# esi: ~
1522
translator: { fallback: "%locale%" }
1623
secret: "%secret%"

app/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ services:
2020

2121
app.twig.source_code_extension:
2222
class: AppBundle\Twig\SourceCodeExtension
23-
arguments: ["@twig.loader"]
23+
arguments: ["@twig.loader", "%kernel.root_dir%", "%templating.helper.code.file_link_format%"]
2424
tags:
2525
- { name: twig.extension }
2626

src/AppBundle/Twig/SourceCodeExtension.php

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ class SourceCodeExtension extends \Twig_Extension
2525
protected $loader;
2626
protected $controller;
2727
protected $template;
28+
protected $kernelRootDir;
29+
protected $fileLinkFormat;
2830

29-
public function __construct(\Twig_LoaderInterface $loader)
31+
public function __construct(\Twig_LoaderInterface $loader, $kernelRootDir, $fileLinkFormat)
3032
{
33+
$this->kernelRootDir = $kernelRootDir;
3134
$this->loader = $loader;
35+
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
3236
}
3337

3438
public function setController($controller)
@@ -48,18 +52,16 @@ public function showSourceCode(\Twig_Environment $twig, $template)
4852
$this->template = $template;
4953

5054
return $twig->render('default/_source_code.html.twig', array(
51-
'controller_source_code' => $this->getControllerCode(),
52-
'controller_file_path' => $this->getControllerRelativePath(),
53-
'template_source_code' => $this->getTemplateCode(),
54-
'template_file_path' => $this->getTemplateRelativePath(),
55+
'controller' => $this->getController(),
56+
'template' => $this->getTemplate(),
5557
));
5658
}
5759

58-
private function getControllerCode()
60+
private function getController()
5961
{
6062
// this happens for example for exceptions (404 errors, etc.)
6163
if (null === $this->controller) {
62-
return 'Not available';
64+
return;
6365
}
6466

6567
$className = get_class($this->controller[0]);
@@ -70,42 +72,22 @@ private function getControllerCode()
7072
$methodCode = array_slice($classCode, $method->getStartline() - 1, $method->getEndLine() - $method->getStartline() + 1);
7173
$controllerCode = ' '.$method->getDocComment()."\n".implode('', $methodCode);
7274

73-
return $this->unindentCode($controllerCode);
74-
}
75-
76-
private function getControllerRelativePath()
77-
{
78-
// this happens for example for exceptions (404 errors, etc.)
79-
if (null === $this->controller) {
80-
return '';
81-
}
82-
83-
$className = get_class($this->controller[0]);
84-
$class = new \ReflectionClass($className);
85-
86-
$absolutePath = $class->getFilename();
87-
$pathParts = explode(DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR, $absolutePath);
88-
$relativePath = 'src'.DIRECTORY_SEPARATOR.$pathParts[1];
89-
90-
return $relativePath;
75+
return array(
76+
'file_path' => $class->getFilename(),
77+
'starting_line' => $method->getStartline(),
78+
'source_code' => $this->unindentCode($controllerCode)
79+
);
9180
}
9281

93-
private function getTemplateCode()
82+
private function getTemplate()
9483
{
95-
return $this->loader->getSource($this->template->getTemplateName());
96-
}
84+
$templateName = $this->template->getTemplateName();
9785

98-
/**
99-
* The logic implemented in this method is solely developed for the Symfony
100-
* Demo application and cannot be used as a general purpose solution.
101-
* Specifically, this logic won't work for templates that use a namespaced path
102-
* (e.g. @WebProfiler/Collector/time.html.twig) or any loader different from
103-
* Twig_Loader_Filesystem (e.g. TwigBundle:Exception:exception.txt.twig notation
104-
* or an anonymous template created by the {% embed %} tag).
105-
*/
106-
private function getTemplateRelativePath()
107-
{
108-
return 'app/Resources/views/'.$this->template->getTemplateName();
86+
return array(
87+
'file_path' => $this->kernelRootDir.'/Resources/views/'.$templateName,
88+
'starting_line' => 1,
89+
'source_code' => $this->loader->getSource($templateName),
90+
);
10991
}
11092

11193
/**

0 commit comments

Comments
 (0)