From 00d51fed9f61db25c4affee6aceba44305d7626e Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 16 Sep 2017 21:37:07 +0200 Subject: [PATCH 1/2] [WebProfilerBundle] Render file links for twig templates --- .../Twig/DataCollector/TwigDataCollector.php | 25 ++++++++++++++++++- .../TwigBundle/Resources/config/twig.xml | 1 + .../Resources/views/Collector/twig.html.twig | 4 ++- .../Resources/views/Profiler/open.css.twig | 4 +++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php b/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php index f28c92422ae48..f5f12f0a99798 100644 --- a/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php +++ b/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Twig\Loader\FilesystemLoader; use Twig\Markup; use Twig\Profiler\Dumper\HtmlDumper; use Twig\Profiler\Profile; @@ -27,11 +28,13 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterface { private $profile; + private $loader; private $computed; - public function __construct(Profile $profile) + public function __construct(Profile $profile, FilesystemLoader $loader = null) { $this->profile = $profile; + $this->loader = $loader; } /** @@ -47,6 +50,21 @@ public function collect(Request $request, Response $response, \Exception $except public function lateCollect() { $this->data['profile'] = serialize($this->profile); + $this->data['template_paths'] = array(); + + if (null !== $this->loader) { + $locator = (new \ReflectionObject($this->loader))->getMethod('findTemplate'); + $locator->setAccessible(true); + $finder = function (Profile $profile) use (&$finder, $locator) { + if ($profile->isTemplate()) { + $this->data['template_paths'][$profile->getName()] = $locator->invoke($this->loader, $profile->getName()); + } + foreach ($profile as $p) { + $finder($p); + } + }; + $finder($this->profile); + } } public function getTime() @@ -59,6 +77,11 @@ public function getTemplateCount() return $this->getComputedData('template_count'); } + public function getTemplatePaths() + { + return $this->data['template_paths']; + } + public function getTemplates() { return $this->getComputedData('templates'); diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index 8c6a944603333..b6c6deb0f0c77 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -68,6 +68,7 @@ + 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 c3c8a5d5f6764..dbf3825ee60f6 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/twig.html.twig @@ -85,7 +85,9 @@ {% for template, count in collector.templates %} - {{ template }} + {%- set file = collector.templatePaths[template]|default(false) -%} + {%- set link = file ? file|file_link(1) : false -%} + {% if link %}{{ template }}{% else %}{{ template }}{% endif %} {{ count }} {% endfor %} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.css.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.css.twig index 7a0941c955a6d..d0f5cda02dccc 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/open.css.twig @@ -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); } From 7a403fcdea34dd128e4b7c3732faab7fdf37b926 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Wed, 27 Sep 2017 10:28:22 +0200 Subject: [PATCH 2/2] @stof feedback --- .../Twig/DataCollector/TwigDataCollector.php | 30 ++++++++----------- .../TwigBundle/Resources/config/twig.xml | 2 +- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php b/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php index f5f12f0a99798..d5efaa805362c 100644 --- a/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php +++ b/src/Symfony/Bridge/Twig/DataCollector/TwigDataCollector.php @@ -15,7 +15,7 @@ use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Twig\Loader\FilesystemLoader; +use Twig\Environment; use Twig\Markup; use Twig\Profiler\Dumper\HtmlDumper; use Twig\Profiler\Profile; @@ -28,13 +28,13 @@ class TwigDataCollector extends DataCollector implements LateDataCollectorInterface { private $profile; - private $loader; + private $twig; private $computed; - public function __construct(Profile $profile, FilesystemLoader $loader = null) + public function __construct(Profile $profile, Environment $twig) { $this->profile = $profile; - $this->loader = $loader; + $this->twig = $twig; } /** @@ -52,19 +52,15 @@ public function lateCollect() $this->data['profile'] = serialize($this->profile); $this->data['template_paths'] = array(); - if (null !== $this->loader) { - $locator = (new \ReflectionObject($this->loader))->getMethod('findTemplate'); - $locator->setAccessible(true); - $finder = function (Profile $profile) use (&$finder, $locator) { - if ($profile->isTemplate()) { - $this->data['template_paths'][$profile->getName()] = $locator->invoke($this->loader, $profile->getName()); - } - foreach ($profile as $p) { - $finder($p); - } - }; - $finder($this->profile); - } + $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() diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index b6c6deb0f0c77..ff564b412148a 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -68,7 +68,7 @@ - +