[WebProfilerBundle] Render file links for twig templates

This commit is contained in:
Roland Franssen 2017-09-16 21:37:07 +02:00 committed by Fabien Potencier
parent 40a3466268
commit 860575a882
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);
}