Skip to content

[WebProfilerBundle] Render file links for twig templates #24236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -27,11 +28,13 @@
class TwigDataCollector extends DataCollector implements LateDataCollectorInterface
{
private $profile;
private $twig;
private $computed;

public function __construct(Profile $profile)
public function __construct(Profile $profile, Environment $twig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a BC break. It should be optional. @ro0NL Can you submit a PR to fix this?

{
$this->profile = $profile;
$this->twig = $twig;
}

/**
Expand All @@ -47,6 +50,17 @@ public function collect(Request $request, Response $response, \Exception $except
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()
Expand All @@ -59,6 +73,11 @@ public function getTemplateCount()
return $this->getComputedData('template_count');
}

public function getTemplatePaths()
{
return $this->data['template_paths'];
}

public function getTemplates()
{
return $this->getComputedData('templates');
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down