8000 feature #24236 [WebProfilerBundle] Render file links for twig templat… · symfony/symfony@9ebe218 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ebe218

Browse files
committed
feature #24236 [WebProfilerBundle] Render file links for twig templates (ro0NL)
This PR was squashed before being merged into the 3.4 branch (closes #24236). Discussion ---------- [WebProfilerBundle] Render file links for twig templates | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | #24218 | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> ![image](https://user-images.githubusercontent.com/1047696/30515418-8c4615c6-9b27-11e7-8e26-8caa30ff7cbb.png) Also tweaked default code color a bit for yaml, twig and the like. Before ![image](https://user-images.githubusercontent.com/1047696/30515499-2231d768-9b29-11e7-8cab-a61537e83343.png) After ![image](https://user-images.githubusercontent.com/1047696/30515504-354ea218-9b29-11e7-9457-518e9413e6f9.png) Commits ------- 860575a [WebProfilerBundle] Render file links for twig templates
2 parents 9f56a9e + 860575a commit 9ebe218

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
18+
use Twig\Environment;
1819
use Twig\Markup;
1920
use Twig\Profiler\Dumper\HtmlDumper;
2021
use Twig\Profiler\Profile;
@@ -27,11 +28,13 @@
2728
class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
2829
{
2930
private $profile;
31+
private $twig;
3032
private $computed;
3133

32-
public function __construct(Profile $profile)
34+
public function __construct(Profile $profile, Environment $twig)
3335
{
3436
$this->profile = $profile;
37+
$this->twig = $twig;
3538
}
3639

3740
/**
@@ -47,6 +50,17 @@ public function collect(Request $request, Response $response, \Exception $except
4750
public function lateCollect()
4851
{
4952
$this->data['profile'] = serialize($this->profile);
53+
$this->data['template_paths'] = array();
54+
55+
$templateFinder = function (Profile $profile) use (&$templateFinder) {
56+
if ($profile->isTemplate() && $template = $this->twig->load($profile->getName())->getSourceContext()->getPath()) {
57+
$this->data['template_paths'][$profile->getName()] = $template;
58+
}
59+
foreach ($profile as $p) {
60+
$templateFinder($p);
61+
}
62+
};
63+
$templateFinder($this->profile);
5064
}
5165

5266
public function getTime()
@@ -59,6 +73,11 @@ public function getTemplateCount()
5973
return $this->getComputedData('template_count');
6074
}
6175

76+
public function getTemplatePaths()
77+
{
78+
return $this->data['template_paths'];
79+
}
80+
6281
public function getTemplates()
6382
{
6483
return $this->getComputedData('templates');

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<service id="data_collector.twig" class="Symfony\Bridge\Twig\DataCollector\TwigDataCollector">
6969
<tag name="data_collector" template="@WebProfiler/Collector/twig.html.twig" id="twig" priority="257" />
7070
<argument type="service" id="twig.profile" />
71+
<argument type="service" id="twig" />
7172
</service>
7273

7374
<service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension">

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@
8585
<tbody>
8686
{% for template, count in collector.templates %}
8787
<tr>
88-
<td>{{ template }}</td>
88+
{%- set file = collector.templatePaths[template]|default(false) -%}
89+
{%- set link = file ? file|file_link(1) : false -%}
90+
<td>{% if link %}<a href="{{ link }}" title="{{ file }}">{{ template }}</a>{% else %}{{ template }}{% endif %}</td>
8991
<td class="font-normal">{{ count }}</td>
9092
</tr>
9193
{% endfor %}

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.css.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ a.doc:hover {
5858
margin-top: 41px;
5959
}
6060

61+
.source li code {
62+
color: #555;
63+
}
64+
6165
.source li.selected {
6266
background: rgba(255, 255, 153, 0.5);
6367
}

0 commit comments

Comments
 (0)
0