From 7ca0f8711cfbd9c5aae8ddd184b90cfe73a7323c Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 22 May 2017 14:55:26 +0300 Subject: [PATCH] Make it possible to include debug bar also into non-HTML responses --- CHANGELOG.md | 1 + system/src/Grav/Common/Debugger.php | 41 +++++++++++++++++++++++------ system/src/Grav/Common/Grav.php | 5 ---- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f2044ff..1df2864e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ 1. [](#improved) * Optionally remove unpublished pages from the translated languages, move into untranslated list [#1482](https://github.com/getgrav/grav/pull/1482) * Improved reliability of `hash` filecheck method + * Make it possible to include debug bar also into non-HTML responses 1. [](#bugfix) * Fix output handling in RenderProcessor [#1483](https://github.com/getgrav/grav/pull/1483) diff --git a/system/src/Grav/Common/Debugger.php b/system/src/Grav/Common/Debugger.php index 679772306..ef5b242b4 100644 --- a/system/src/Grav/Common/Debugger.php +++ b/system/src/Grav/Common/Debugger.php @@ -36,6 +36,9 @@ class Debugger */ public function __construct() { + // Enable debugger until $this->init() gets called. + $this->enabled = true; + $this->debugbar = new StandardDebugBar(); $this->debugbar['time']->addMeasure('Loading', $this->debugbar['time']->getRequestStartTime(), microtime(true)); } @@ -51,6 +54,9 @@ public function init() $this->grav = Grav::instance(); $this->config = $this->grav['config']; + // Enable/disable debugger based on configuration. + $this->enabled = $this->config->get('system.debugger.enabled'); + if ($this->enabled()) { $this->debugbar->addCollector(new ConfigCollector((array)$this->config->get('system'), 'Config')); $this->debugbar->addCollector(new ConfigCollector((array)$this->config->get('plugins'), 'Plugins')); @@ -70,10 +76,6 @@ public function enabled($state = null) { if (isset($state)) { $this->enabled = $state; - } else { - if (!isset($this->enabled)) { - $this->enabled = $this->config->get('system.debugger.enabled'); - } } return $this->enabled; @@ -91,7 +93,6 @@ public function addAssets() // Only add assets if Page is HTML $page = $this->grav['page']; if ($page->templateFormat() != 'html') { - $this->enabled = false; return $this; } @@ -163,6 +164,12 @@ public function getCollector($collector) public function render() { if ($this->enabled()) { + // Only add assets if Page is HTML + $page = $this->grav['page']; + if ($page->templateFormat() != 'html') { + return $this; + } + echo $this->renderer->render(); } @@ -176,11 +183,29 @@ public function render() */ public function sendDataInHeaders() { - $this->debugbar->sendDataInHeaders(); + if ($this->enabled()) { + $this->debugbar->sendDataInHeaders(); + } return $this; } + /** + * Returns collected debugger data. + * + * @return array + */ + public function getData() + { + if (!$this->enabled()) { + return null; + } + + $this->timers = []; + + return $this->debugbar->getData(); + } + /** * Start a timer with an associated name and description * @@ -191,7 +216,7 @@ public function sendDataInHeaders() */ public function startTimer($name, $description = null) { - if ($name[0] == '_' || $this->config->get('system.debugger.enabled')) { + if ($name[0] == '_' || $this->enabled()) { $this->debugbar['time']->startMeasure($name, $description); $this->timers[] = $name; } @@ -208,7 +233,7 @@ public function startTimer($name, $description = null) */ public function stopTimer($name) { - if (in_array($name, $this->timers) && ($name[0] == '_' || $this->config->get('system.debugger.enabled'))) { + if (in_array($name, $this->timers) && ($name[0] == '_' || $this->enabled())) { $this->debugbar['time']->stopMeasure($name); } diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index a260ec95f..c5adcb5d0 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -247,11 +247,6 @@ public function header() header('ETag: "' . md5($page->raw() . $page->modified()).'"'); } - // Set debugger data in headers - if (!($format === null || $format == 'html')) { - $this['debugger']->enabled(false); - } - // Set HTTP response code if (isset($this['page']->header()->http_response_code)) { http_response_code($this['page']->header()->http_response_code);