8000 feature #27978 [WebProfilerBundle] Show relative path of the template… · symfony/symfony@7b853bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b853bb

Browse files
committed
feature #27978 [WebProfilerBundle] Show relative path of the template and improving panel view (yceruto)
This PR was merged into the 4.2-dev branch. Discussion ---------- [WebProfilerBundle] Show relative path of the template and improving panel view | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27911 | License | MIT | Doc PR | - | Before | After | | --- | --- | | ![twig-panel-before](https://user-images.githubusercontent.com/2028198/42819536-dfbed0c6-89a1-11e8-8eb1-8151172e1ad7.png) | ![twig-panel-after](https://user-images.githubusercontent.com/2028198/42819544-e5e73182-89a1-11e8-9d49-80e343040260.png) | #27978 (comment): | Before | After | | --- | --- | | ![twig-toolbar-icon-before](https://user-images.githubusercontent.com/2028198/42820333-e35684c0-89a3-11e8-8b3a-f02bb835103c.png) | ![twig-toolbar-icon-after](https://user-images.githubusercontent.com/2028198/42820342-eac3a95e-89a3-11e8-929e-ef4fb1e392bb.png) | ![twig-profiler-icon-before](https://user-images.githubusercontent.com/2028198/42820356-f3f04b72-89a3-11e8-9209-41ed37cf0915.png) | ![twig-profiler-icon-after](https://user-images.githubusercontent.com/2028198/42820370-fef24336-89a3-11e8-9b09-5ba1bf97df67.png) | In addition, I added some visual improvements. Commits ------- bcd8058 Show relative path of the template and improving panel view
2 parents 01c65c8 + bcd8058 commit 7b853bb

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

src/Symfony/Bridge/Twig/Extension/CodeExtension.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ class CodeExtension extends AbstractExtension
2525
private $fileLinkFormat;
2626
private $rootDir;
2727
private $charset;
28+
private $projectDir;
2829

2930
/**
3031
* @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
3132
* @param string $rootDir The project root directory
3233
* @param string $charset The charset
3334
*/
34-
public function __construct($fileLinkFormat, string $rootDir, string $charset)
35+
public function __construct($fileLinkFormat, string $rootDir, string $charset, string $projectDir = null)
3536
{
3637
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
3738
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
3839
$this->charset = $charset;
40+
$this->projectDir = $projectDir;
3941
}
4042

4143
/**
@@ -53,6 +55,7 @@ public function getFilters()
5355
new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
5456
new TwigFilter('format_log_message', array($this, 'formatLogMessage'), array('is_safe' => array('html'))),
5557
new TwigFilter('file_link', array($this, 'getFileLink')),
58+
new TwigFilter('file_relative', array($this, 'getFileRelative')),
5659
);
5760
}
5861

@@ -209,6 +212,15 @@ public function getFileLink($file, $line)
209212
return false;
210213
}
211214

215+
public function getFileRelative(string $file): ?string
216+
{
217+
if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
218+
return ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
219+
}
220+
221+
return null;
222+
}
223+
212224
public function formatFileFromText($text)
213225
{
214226
return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {

src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public function testFormatFile()
2323
$this->assertEquals($expected, $this->getExtension()->formatFile(__FILE__, 25));
2424
}
2525

26+
public function testFileRelative()
27+
{
28+
$this->assertEquals('path/to/file.ext', $this->getExtension()->getFileRelative('/root/path/to/file.ext'));
29+
}
30+
2631
/**
2732
* @dataProvider getClassNameProvider
2833
*/
@@ -64,6 +69,6 @@ public function testGetName()
6469

6570
protected function getExtension()
6671
{
67-
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8');
72+
return new CodeExtension(new FileLinkFormatter('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar'), '/root', 'UTF-8', '/root');
6873
}
6974
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<argument type="service" id="debug.file_link_formatter" on-invalid="ignore" />
8686
<argument>%kernel.root_dir%</argument>
8787
<argument>%kernel.charset%</argument>
88+
<argument>%kernel.project_dir%</argument>
8889
</service>
8990

9091
<service id="twig.extension.routing" class="Symfony\Bridge\Twig\Extension\RoutingExtension">

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,32 @@
7575

7676
<h2>Rendered Templates</h2>
7777

78-
<table>
78+
<table id="twig-table">
7979
<thead>
80-
<tr>
81-
<th scope="col">Template Name</th>
82-
<th scope="col">Render Count</th>
83-
</tr>
80+
<tr>
81+
<th scope="col">Template Name &amp; Path</th>
82+
<th class="num-col" scope="col">Render Count</th>
83+
</tr>
8484
</thead>
8585
<tbody>
8686
{% for template, count in collector.templates %}
8787
<tr>
8888
{%- set file = collector.templatePaths[template]|default(false) -%}
8989
{%- set link = file ? file|file_link(1) : false -%}
90-
<td>{% if link %}<a href="{{ link }}" title="{{ file }}">{{ template }}</a>{% else %}{{ template }}{% endif %}</td>
91-
<td class="font-normal">{{ count }}</td>
90+
<td>
91+
<span class="sf-icon icon-twig">{{ include('@WebProfiler/Icon/twig.svg') }}</span>
92+
{% if link %}
93+
<a href="{{ link }}" title="{{ file }}">{{ template }}</a>
94+
<div>
95+
<a class="text-muted" href="{{ link }}" title="{{ file }}">
96+
{{ file|file_relative|default(file) }}
97+
</a>
98+
</div>
99+
{% else %}
100+
{{ template }}
101+
{% endif %}
102+
</td>
103+
<td class="font-normal num-col">{{ count }}</td>
92104
</tr>
93105
{% endfor %}
94106
</tbody>
Lines changed: 1 addition & 3 deletions
Loading

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ table tbody ul {
186186
padding: 0 0 0 1em;
187187
}
188188

189+
table thead th.num-col,
190+
table tbody td.num-col {
191+
text-align: center;
192+
}
193+
189194
{# Utility classes
190195
========================================================================= #}
191196
.block {
@@ -852,6 +857,10 @@ tr.status-warning td {
852857
#twig-dump pre {
853858
font-size: 12px;
854859
line-height: 1.7;
860+
background-color: #fff;
861+
border: 1px solid #E0E0E0;
862+
padding: 15px;
863+
box-shadow: 0 0 1px rgba(128, 128, 128, .2);
855864
}
856865
#twig-dump span {
857866
border-radius: 2px;
@@ -861,6 +870,23 @@ tr.status-warning td {
861870
#twig-dump .status-warning { background: rgba(240, 181, 24, 0.3); }
862871
#twig-dump .status-success { background: rgba(100, 189, 99, 0.2); }
863872

873+
#twig-table tbody td {
874+
vertical-align: middle;
875+
}
876+
#twig-table tbody td > a {
877+
margin-left: -5px;
878+
}
879+
#twig-table tbody td div {
880+
margin: 0;
881+
}
882+
883+
.icon-twig {
884+
vertical-align: text-bottom;
885+
}
886+
.icon-twig svg path {
887+
fill: #7eea12;
888+
}
889+
864890
{# Logger panel
865891
========================================================================= #}
866892
table.logs .metadata {

src/Symfony/Bundle/WebProfilerBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/config": "^4.2",
2121
"symfony/http-kernel": "~4.1",
2222
"symfony/routing": "~3.4|~4.0",
23-
"symfony/twig-bundle": "^3.4.3|^4.0.3",
23+
"symfony/twig-bundle": "~4.2",
2424
"symfony/var-dumper": "~3.4|~4.0",
2525
"twig/twig": "~1.34|~2.4"
2626
},

0 commit comments

Comments
 (0)
0