Skip to content

Commit

Permalink
Make it possible to include debug bar also into non-HTML responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 22, 2017
1 parent d40cb3c commit 7ca0f87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
41 changes: 33 additions & 8 deletions system/src/Grav/Common/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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'));
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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
*
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down
5 changes: 0 additions & 5 deletions system/src/Grav/Common/Grav.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7ca0f87

Please sign in to comment.