Skip to content

[WebProfilerBundle] Show relative path of the template and improving panel view #27978

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

Merged
merged 1 commit into from
Jul 18, 2018
Merged
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
14 changes: 13 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ class CodeExtension extends AbstractExtension
private $fileLinkFormat;
private $rootDir;
private $charset;
private $projectDir;

/**
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
* @param string $rootDir The project root directory
* @param string $charset The charset
*/
public function __construct($fileLinkFormat, string $rootDir, string $charset)
public function __construct($fileLinkFormat, string $rootDir, string $charset, string $projectDir = null)
{
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
$this->charset = $charset;
$this->projectDir = $projectDir;
}

/**
Expand All @@ -53,6 +55,7 @@ public function getFilters()
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
new TwigFilter('format_log_message', array($this, 'formatLogMessage'), array('is_safe' => array('html'))),
new TwigFilter('file_link', array($this, 'getFileLink')),
new TwigFilter('file_relative', array($this, 'getFileRelative')),
);
}

Expand Down Expand Up @@ -209,6 +212,15 @@ public function getFileLink($file, $line)
return false;
}

public function getFileRelative(string $file): ?string
{
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
return ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
}

return null;
}

public function formatFileFromText($text)
{
return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function testFormatFile()
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
}

public function testFileRelative()
{
$this->assertEquals('path/to/file.ext', $this->getExtension()->getFileRelative('/root/path/to/file.ext'));
}

/**
* @dataProvider getClassNameProvider
*/
Expand Down Expand Up @@ -64,6 +69,6 @@ public function testGetName()

protected function getExtension()
{
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8');
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8', '/root');
}
}
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 @@ -85,6 +85,7 @@
<argument type="service" id="debug.file_link_formatter" on-invalid="ignore" />
<argument>%kernel.root_dir%</argument>
<argument>%kernel.charset%</argument>
<argument>%kernel.project_dir%</argument>
</service>

<service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,32 @@

<h2>Rendered Templates</h2>

<table>
<table id="twig-table">
<thead>
<tr>
<th scope="col">Template Name</th>
<th scope="col">Render Count</th>
</tr>
<tr>
<th scope="col">Template Name &amp; Path</th>
<th class="num-col" scope="col">Render Count</th>
</tr>
</thead>
<tbody>
{% for template, count in collector.templates %}
<tr>
{%- 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>
<td>
<span class="sf-icon icon-twig">{{ include('@WebProfiler/Icon/twig.svg') }}</span>
{% if link %}
<a href="{{ link }}" title="{{ file }}">{{ template }}</a>
<div>
<a class="text-muted" href="{{ link }}" title="{{ file }}">
{{ file|file_relative|default(file) }}
</a>
</div>
{% else %}
{{ template }}
{% endif %}
</td>
<td class="font-normal num-col">{{ count }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ table tbody ul {
padding: 0 0 0 1em;
}

table thead th.num-col,
table tbody td.num-col {
text-align: center;
}

{# Utility classes
========================================================================= #}
.block {
Expand Down Expand Up @@ -852,6 +857,10 @@ tr.status-warning td {
#twig-dump pre {
font-size: 12px;
line-height: 1.7;
background-color: #fff;
border: 1px solid #E0E0E0;
padding: 15px;
box-shadow: 0 0 1px rgba(128, 128, 128, .2);
}
#twig-dump span {
border-radius: 2px;
Expand All @@ -861,6 +870,23 @@ tr.status-warning td {
#twig-dump .status-warning { background: rgba(240, 181, 24, 0.3); }
#twig-dump .status-success { background: rgba(100, 189, 99, 0.2); }

#twig-table tbody td {
vertical-align: middle;
}
#twig-table tbody td > a {
margin-left: -5px;
}
#twig-table tbody td div {
margin: 0;
}

.icon-twig {
vertical-align: text-bottom;
}
.icon-twig svg path {
fill: #7eea12;
}

{# Logger panel
========================================================================= #}
table.logs .metadata {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/WebProfilerBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/config": "^4.2",
"symfony/http-kernel": "~4.1",
"symfony/routing": "~3.4|~4.0",
"symfony/twig-bundle": "^3.4.3|^4.0.3",
"symfony/twig-bundle": "~4.2",
"symfony/var-dumper": "~3.4|~4.0",
"twig/twig": "~1.34|~2.4"
},
Expand Down