From 3257c865d664bca3e30653ea6bf3e563dca212bc Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Sat, 24 Dec 2016 23:10:21 +0100 Subject: [PATCH 1/4] Add Cookie help --- .../DependencyInjection/Configuration.php | 1 + .../WebProfilerExtension.php | 1 + .../Resources/config/profiler.xml | 1 + .../views/Collector/request.html.twig | 5 +- .../views/Profiler/base_js.html.twig | 27 +++++++++ .../views/Profiler/profiler.css.twig | 57 +++++++++++++++++++ .../Twig/WebProfilerExtension.php | 40 ++++++++++++- 7 files changed, 130 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php index 957a683ca3563..7034efdbc8bcb 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php @@ -46,6 +46,7 @@ public function getConfigTreeBuilder() ->end() ->booleanNode('intercept_redirects')->defaultFalse()->end() ->scalarNode('excluded_ajax_paths')->defaultValue('^/(app(_[\\w]+)?\\.php/)?_wdt')->end() + ->booleanNode('help')->defaultFalse()->end() ->end() ; diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php index e507bf2d22b70..dafe5c33e6140 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php @@ -45,6 +45,7 @@ public function load(array $configs, ContainerBuilder $container) $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('profiler.xml'); $container->setParameter('web_profiler.debug_toolbar.position', $config['position']); + $container->setParameter('web_profiler.debug_toolbar.help', $config['help']); if ($config['toolbar'] || $config['intercept_redirects']) { $loader->load('toolbar.xml'); diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml index 931363ac6d328..2a5ce66cf9f40 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml @@ -34,6 +34,7 @@ + %web_profiler.debug_toolbar.help% diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig index cdf2839c6dcd2..50ffd87f4cb5f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig @@ -143,7 +143,10 @@ {{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestattributes }, with_context = false) }} {% endif %} -

Cookies

+

Cookies

{% if collector.requestcookies.all is empty %}
diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig index ca69e371df281..26a7283c98e5f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig @@ -491,6 +491,32 @@ toggle.innerHTML = currentContent !== altContent ? altContent : originalContent; }); } + }, + + createHelp: function() { + var attributes = ['data-help-doc', 'data-help-api']; + var labels = { doc: 'Doc', api: 'API' }; + var query = attributes.map(function (attr) { return '[' + attr + ']'; }).join(', '); + var helps = document.querySelectorAll(query); + + for (var i = 0; i < helps.length; i++) { + var root = document.createElement('div'); + Sfjs.addClass(root, 'help'); + var container = document.createElement('div'); + root.appendChild(container); + helps[i].appendChild(root); + + // Add links + for (var j = 0; j < attributes.length; j++) { + if (helps[i].hasAttribute(attributes[j])) { + var link = document.createElement('a'); + link.textContent = labels[attributes[j].substr('data-help-'.length)]; + link.setAttribute('href', helps[i].getAttribute(attributes[j])); + link.setAttribute('target', '_blank'); + container.appendChild(link); + } + } + } } }; })(); @@ -499,6 +525,7 @@ Sfjs.createTabs(); Sfjs.createToggles(); Sfjs.renderAjaxRequests(); + Sfjs.createHelp(); }); /*]]>*/ 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 e3a0168553224..331a631da0bf0 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig @@ -994,3 +994,60 @@ table.logs .metadata { margin-left: 2px; } } + +{# Help tooltip + ========================================================================= #} + +.help { + display: inline-block; + text-align: center; + background-color: #6da581; + border-radius: 50%; + width: 21px; + height: 21px; + font-size: 13px; + line-height: 23px; + cursor: default; + margin: 0 5px; +} + +.help:before { + content: '?'; + font-weight: bold; + color: #fff; +} + +.help:hover div { + display: block; + transform-origin: 100% 0; +} + +.help div { + display: none; + background-color: #444444; + padding: 8px; + width: auto; + position: absolute; + border-radius: 3px; + box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2); + color: #FFF; + font-size: 13px; + margin-top: 5px; + line-height: 1.4; +} + +.help div a { + color: white; + padding: 0 5px; +} + +.help div:before { + position: absolute; + content: ''; + width: 0; + height: 0; + border: 6px solid transparent; + border-bottom-color: #444444; + left: 5px; + top: -12px; +} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php index 032d6f08bbfb2..a296ca5c1db18 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php @@ -14,6 +14,7 @@ use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Dumper\HtmlDumper; +use Symfony\Component\HttpKernel\Kernel; /** * Twig extension for the profiler. @@ -27,6 +28,8 @@ class WebProfilerExtension extends \Twig_Extension_Profiler */ private $valueExporter; + private $help; + /** * @var HtmlDumper */ @@ -42,10 +45,11 @@ class WebProfilerExtension extends \Twig_Extension_Profiler */ private $stackLevel = 0; - public function __construct(HtmlDumper $dumper = null) + public function __construct(HtmlDumper $dumper = null, $help = false) { $this->dumper = $dumper ?: new HtmlDumper(); $this->dumper->setOutput($this->output = fopen('php://memory', 'r+b')); + $this->help = $help; } public function enter(\Twig_Profiler_Profile $profile) @@ -72,6 +76,7 @@ public function getFunctions() return array( new \Twig_SimpleFunction('profiler_dump', $profilerDump, array('is_safe' => array('html'), 'needs_environment' => true)), new \Twig_SimpleFunction('profiler_dump_log', array($this, 'dumpLog'), array('is_safe' => array('html'), 'needs_environment' => true)), + new \Twig_SimpleFunction('help', array($this, 'help'), array('is_safe' => array('html'))), ); } @@ -120,6 +125,39 @@ public function dumpValue($value) return $this->valueExporter->exportValue($value); } + public function help($links) + { + if (!$this->help) { + return ''; + } + + $attrs = []; + + if (isset($links['doc'])) { + $attrs['data-help-doc'] = sprintf( + 'https://symfony.com/doc/%s/%s', + Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION, + $links['doc'] + ); + } + + if (isset($links['api'])) { + $attrs['data-help-api'] = sprintf( + 'http://api.symfony.com/%s/%s', + Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION, + $links['api'] + ); + } + + $html = ''; + foreach ($attrs as $k => $v) { + $html .= $k.'="'.$v.'" '; + } + + return $html; + } + + /** * {@inheritdoc} */ From c447842621a38428be0565360776c01cb10f45eb Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Sun, 25 Dec 2016 00:04:50 +0100 Subject: [PATCH 2/4] Fix cs --- .../Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php index a296ca5c1db18..0b65488d6adff 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php @@ -131,7 +131,7 @@ public function help($links) return ''; } - $attrs = []; + $attrs = array(); if (isset($links['doc'])) { $attrs['data-help-doc'] = sprintf( @@ -157,7 +157,6 @@ public function help($links) return $html; } - /** * {@inheritdoc} */ From 09e450a22a54d0ca2d4e748268f769e5f6767d9f Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Mon, 26 Dec 2016 00:29:43 +0100 Subject: [PATCH 3/4] Add help.yml + remove help config + improve design + refactoring --- .../Resources/config/help.yml | 3 ++ .../Resources/config/profiler.xml | 1 - .../views/Collector/request.html.twig | 5 +-- .../views/Profiler/base_js.html.twig | 20 ++++----- .../views/Profiler/profiler.css.twig | 23 ++++++---- .../Twig/WebProfilerExtension.php | 43 ++++++++----------- .../Bundle/WebProfilerBundle/composer.json | 3 +- 7 files changed, 45 insertions(+), 53 deletions(-) create mode 100644 src/Symfony/Bundle/WebProfilerBundle/Resources/config/help.yml diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/help.yml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/help.yml new file mode 100644 index 0000000000000..c592aa7d4826b --- /dev/null +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/help.yml @@ -0,0 +1,3 @@ +cookies: + Doc: 'https://symfony.com/doc/%s/components/http_foundation.html#setting-cookies' + API: 'http://api.symfony.com/%s/Symfony/Component/HttpFoundation/Cookie.html' diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml index 2a5ce66cf9f40..931363ac6d328 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml @@ -34,7 +34,6 @@ - %web_profiler.debug_toolbar.help% diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig index 50ffd87f4cb5f..7a2bb9b4e5437 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig @@ -143,10 +143,7 @@ {{ include('@WebProfiler/Profiler/bag.html.twig', { bag: collector.requestattributes }, with_context = false) }} {% endif %} -

Cookies

+

Cookies

{% if collector.requestcookies.all is empty %}
diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig index 26a7283c98e5f..0746d3e08e85b 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig @@ -494,10 +494,7 @@ }, createHelp: function() { - var attributes = ['data-help-doc', 'data-help-api']; - var labels = { doc: 'Doc', api: 'API' }; - var query = attributes.map(function (attr) { return '[' + attr + ']'; }).join(', '); - var helps = document.querySelectorAll(query); + var helps = document.querySelectorAll('[data-help]'); for (var i = 0; i < helps.length; i++) { var root = document.createElement('div'); @@ -507,14 +504,13 @@ helps[i].appendChild(root); // Add links - for (var j = 0; j < attributes.length; j++) { - if (helps[i].hasAttribute(attributes[j])) { - var link = document.createElement('a'); - link.textContent = labels[attributes[j].substr('data-help-'.length)]; - link.setAttribute('href', helps[i].getAttribute(attributes[j])); - link.setAttribute('target', '_blank'); - container.appendChild(link); - } + var links = JSON.parse(helps[i].getAttribute('data-help')); + for (var key in links) { + var link = document.createElement('a'); + link.textContent = key; + link.setAttribute('href', links[key]); + link.setAttribute('target', '_blank'); + container.appendChild(link); } } } 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 331a631da0bf0..131d348f35fbf 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/profiler.css.twig @@ -1001,7 +1001,7 @@ table.logs .metadata { .help { display: inline-block; text-align: center; - background-color: #6da581; + background-color: #e0e0e0; border-radius: 50%; width: 21px; height: 21px; @@ -1009,6 +1009,9 @@ table.logs .metadata { line-height: 23px; cursor: default; margin: 0 5px; + vertical-align: middle; + border: 1px solid #c1c1c1; + text-shadow: 0 0 2px #444444; } .help:before { @@ -1019,28 +1022,23 @@ table.logs .metadata { .help:hover div { display: block; - transform-origin: 100% 0; } .help div { display: none; - background-color: #444444; + background-color: #e0e0e0; padding: 8px; width: auto; position: absolute; border-radius: 3px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2); + border: 1px solid #444444; color: #FFF; font-size: 13px; margin-top: 5px; line-height: 1.4; } -.help div a { - color: white; - padding: 0 5px; -} - .help div:before { position: absolute; content: ''; @@ -1048,6 +1046,13 @@ table.logs .metadata { height: 0; border: 6px solid transparent; border-bottom-color: #444444; - left: 5px; + left: 4px; top: -12px; } + +.help div a { + color: black; + padding: 0 5px; + text-shadow: none; + font-weight: bold; +} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php index 0b65488d6adff..5656fdf734494 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php @@ -15,6 +15,7 @@ use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Component\VarDumper\Dumper\HtmlDumper; use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\Yaml\Yaml; /** * Twig extension for the profiler. @@ -28,7 +29,10 @@ class WebProfilerExtension extends \Twig_Extension_Profiler */ private $valueExporter; - private $help; + /** + * @var array + */ + private $helpLinks; /** * @var HtmlDumper @@ -45,11 +49,10 @@ class WebProfilerExtension extends \Twig_Extension_Profiler */ private $stackLevel = 0; - public function __construct(HtmlDumper $dumper = null, $help = false) + public function __construct(HtmlDumper $dumper = null) { $this->dumper = $dumper ?: new HtmlDumper(); $this->dumper->setOutput($this->output = fopen('php://memory', 'r+b')); - $this->help = $help; } public function enter(\Twig_Profiler_Profile $profile) @@ -76,7 +79,7 @@ public function getFunctions() return array( new \Twig_SimpleFunction('profiler_dump', $profilerDump, array('is_safe' => array('html'), 'needs_environment' => true)), new \Twig_SimpleFunction('profiler_dump_log', array($this, 'dumpLog'), array('is_safe' => array('html'), 'needs_environment' => true)), - new \Twig_SimpleFunction('help', array($this, 'help'), array('is_safe' => array('html'))), + new \Twig_SimpleFunction('help_attrs', array($this, 'help'), array('is_safe' => array('html'))), ); } @@ -125,34 +128,22 @@ public function dumpValue($value) return $this->valueExporter->exportValue($value); } - public function help($links) + public function help($category) { - if (!$this->help) { - return ''; + if (!$this->helpLinks) { + $this->helpLinks = Yaml::parse(file_get_contents(__DIR__.'/../Resources/config/help.yml')); } - $attrs = array(); - - if (isset($links['doc'])) { - $attrs['data-help-doc'] = sprintf( - 'https://symfony.com/doc/%s/%s', - Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION, - $links['doc'] - ); + if (!isset($this->helpLinks[$category])) { + throw new \Exception(sprintf('Help "%s" not found', $category)); } - if (isset($links['api'])) { - $attrs['data-help-api'] = sprintf( - 'http://api.symfony.com/%s/%s', - Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION, - $links['api'] - ); - } + $links = $this->helpLinks[$category]; + array_walk($links, function (&$link) { + $link = sprintf($link, Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION); + }); - $html = ''; - foreach ($attrs as $k => $v) { - $html .= $k.'="'.$v.'" '; - } + $html = 'data-help = "'.htmlspecialchars(json_encode($links, JSON_UNESCAPED_SLASHES)).'"'; return $html; } diff --git a/src/Symfony/Bundle/WebProfilerBundle/composer.json b/src/Symfony/Bundle/WebProfilerBundle/composer.json index 57656312bce8b..88eb63c5d299d 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/composer.json +++ b/src/Symfony/Bundle/WebProfilerBundle/composer.json @@ -22,7 +22,8 @@ "symfony/routing": "~2.8|~3.0", "symfony/twig-bridge": "~2.8|~3.0", "twig/twig": "~1.28|~2.0", - "symfony/var-dumper": "~3.2" + "symfony/var-dumper": "~3.2", + "symfony/yaml": "~2.8|~3.0" }, "require-dev": { "symfony/config": "~2.8|~3.0", From cf484d64029d694a5a7a6706f069f60325be421b Mon Sep 17 00:00:00 2001 From: Dany Maillard Date: Mon, 26 Dec 2016 00:46:57 +0100 Subject: [PATCH 4/4] Remove help config --- .../WebProfilerBundle/DependencyInjection/Configuration.php | 1 - .../DependencyInjection/WebProfilerExtension.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php index 7034efdbc8bcb..957a683ca3563 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php @@ -46,7 +46,6 @@ public function getConfigTreeBuilder() ->end() ->booleanNode('intercept_redirects')->defaultFalse()->end() ->scalarNode('excluded_ajax_paths')->defaultValue('^/(app(_[\\w]+)?\\.php/)?_wdt')->end() - ->booleanNode('help')->defaultFalse()->end() ->end() ; diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php index dafe5c33e6140..e507bf2d22b70 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php @@ -45,7 +45,6 @@ public function load(array $configs, ContainerBuilder $container) $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('profiler.xml'); $container->setParameter('web_profiler.debug_toolbar.position', $config['position']); - $container->setParameter('web_profiler.debug_toolbar.help', $config['help']); if ($config['toolbar'] || $config['intercept_redirects']) { $loader->load('toolbar.xml');