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
-------

860575a882 [WebProfilerBundle] Render file links for twig templates
This commit is contained in:
Fabien Potencier 2017-09-27 07:36:24 -07:00
commit 9ebe2185bd
4 changed files with 28 additions and 2 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
use Twig\Markup;
use Twig\Profiler\Dumper\HtmlDumper;
use Twig\Profiler\Profile;
@ -27,11 +28,13 @@ use Twig\Profiler\Profile;
class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
{
private $profile;
private $twig;
private $computed;
public function __construct(Profile $profile)
public function __construct(Profile $profile, Environment $twig)
{
$this->profile = $profile;
$this->twig = $twig;
}
/**
@ -47,6 +50,17 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterf
public function lateCollect()
{
$this->data['profile'] = serialize($this->profile);
$this->data['template_paths'] = array();
$templateFinder = function (Profile $profile) use (&$templateFinder) {
if ($profile->isTemplate() && $template = $this->twig->load($profile->getName())->getSourceContext()->getPath()) {
$this->data['template_paths'][$profile->getName()] = $template;
}
foreach ($profile as $p) {
$templateFinder($p);
}
};
$templateFinder($this->profile);
}
public function getTime()
@ -59,6 +73,11 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterf
return $this->getComputedData('template_count');
}
public function getTemplatePaths()
{
return $this->data['template_paths'];
}
public function getTemplates()
{
return $this->getComputedData('templates');

View File

@ -68,6 +68,7 @@
<service id="data_collector.twig" class="Symfony\Bridge\Twig\DataCollector\TwigDataCollector">
<tag name="data_collector" template="@WebProfiler/Collector/twig.html.twig" id="twig" priority="257" />
<argument type="service" id="twig.profile" />
<argument type="service" id="twig" />
</service>
<service id="twig.extension.trans" class="Symfony\Bridge\Twig\Extension\TranslationExtension">

View File

@ -85,7 +85,9 @@
<tbody>
{% for template, count in collector.templates %}
<tr>
<td>{{ template }}</td>
{%- set file = collector.templatePaths[template]|default(false) -%}
{%- set link = file ? file|file_link(1) : false -%}
<td>{% if link %}<a href="{{ link }}" title="{{ file }}">{{ template }}</a>{% else %}{{ template }}{% endif %}</td>
<td class="font-normal">{{ count }}</td>
</tr>
{% endfor %}

View File

@ -58,6 +58,10 @@ a.doc:hover {
margin-top: 41px;
}
.source li code {
color: #555;
}
.source li.selected {
background: rgba(255, 255, 153, 0.5);
}