From bcd8058478f5c5ba3f9813aedff663a81be19551 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 17 Jul 2018 09:08:24 -0400 Subject: [PATCH] Show relative path of the template and improving panel view --- .../Bridge/Twig/Extension/CodeExtension.php | 14 +++++++++- .../Tests/Extension/CodeExtensionTest.php | 7 ++++- .../TwigBundle/Resources/config/twig.xml | 1 + .../Resources/views/Collector/twig.html.twig | 26 ++++++++++++++----- .../Resources/views/Icon/twig.svg | 4 +-- .../views/Profiler/profiler.css.twig | 26 +++++++++++++++++++ .../Bundle/WebProfilerBundle/composer.json | 2 +- 7 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php index c0982fab00..f2885266d9 100644 --- a/src/Symfony/Bridge/Twig/Extension/CodeExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/CodeExtension.php @@ -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; } /** @@ -53,6 +55,7 @@ class CodeExtension extends AbstractExtension 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')), ); } @@ -209,6 +212,15 @@ class CodeExtension extends AbstractExtension 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) { diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php index 336991c6ca..52e7ff876c 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/CodeExtensionTest.php @@ -23,6 +23,11 @@ class CodeExtensionTest extends TestCase $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 */ @@ -64,6 +69,6 @@ class CodeExtensionTest extends TestCase 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'); } } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 5d014e930c..9b24780197 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -85,6 +85,7 @@ %kernel.root_dir% %kernel.charset% + %kernel.project_dir% diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig index dbf3825ee6..28df9d26ea 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig @@ -75,20 +75,32 @@

Rendered Templates

- +
- - - - + + + + {% for template, count in collector.templates %} {%- set file = collector.templatePaths[template]|default(false) -%} {%- set link = file ? file|file_link(1) : false -%} - - + + {% endfor %} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Icon/twig.svg b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Icon/twig.svg index bfb4e6dea1..7ff26c146e 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Icon/twig.svg +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Icon/twig.svg @@ -1,5 +1,3 @@ - + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig index 30667a9eec..e6601614a9 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig @@ -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 { @@ -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; @@ -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 { diff --git a/src/Symfony/Bundle/WebProfilerBundle/composer.json b/src/Symfony/Bundle/WebProfilerBundle/composer.json index 1acf8ba572..4c48a0cc73 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/composer.json +++ b/src/Symfony/Bundle/WebProfilerBundle/composer.json @@ -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" },
Template NameRender Count
Template Name & PathRender Count
{% if link %}{{ template }}{% else %}{{ template }}{% endif %}{{ count }} + {{ include('@WebProfiler/Icon/twig.svg') }} + {% if link %} + {{ template }} + + {% else %} + {{ template }} + {% endif %} + {{ count }}