Skip to content

Commit

Permalink
Merge pull request #126 from Firesphere/hans/header-collector
Browse files Browse the repository at this point in the history
Add a Header collector
  • Loading branch information
lekoala authored Jan 3, 2021
2 parents 2d18a18 + 789dccb commit 66c2caf
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.php_cs.cache
.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ LeKoala\DebugBar\DebugBar:
| `config_collector` | bool | Show the config tab. Defaults to true |
| `partial_cache_collector` | bool | Show the partial cache tab. Defaults to true |
| `email_collector` | bool | Show the email tab. Defaults to true |
| `header_collector` | bool | Show the headers tab. Defaults to true |

#### Disabling the debug bar

Expand Down
4 changes: 2 additions & 2 deletions _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ function d()
}
foreach ($args as $arg) {
if ($isPlain && $arg === "") {
echo "(empty)";
echo "(empty)";
continue;
}
if ($isPlain && $arg === null) {
echo "(null)";
echo "(null)";
continue;
}
if (is_string($arg)) {
Expand Down
1 change: 1 addition & 0 deletions _config/debugbar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ LeKoala\DebugBar\DebugBar:
config_collector: true
partial_cache_collector: true
email_collector: true
header_collector: true
SilverStripe\Control\Director:
rules:
'__debugbar': 'LeKoala\DebugBar\DebugBarController'
Expand Down
2 changes: 1 addition & 1 deletion code/Collector/DatabaseCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected function collectData(TimeDataCollector $timeCollector = null)
$warnDurationThreshold = DebugBar::config()->get('warn_dbqueries_threshold_seconds');

$showDb = count(array_unique(array_map(function ($stmt) {
return $stmt['database'];
return $stmt['database'];
}, $queries))) > 1;

foreach ($queries as $stmt) {
Expand Down
76 changes: 76 additions & 0 deletions code/Collector/HeaderCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace LeKoala\DebugBar\Collector;

use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use SilverStripe\Control\Controller;

class HeaderCollector extends DataCollector implements Renderable, AssetProvider
{
/**
* @var \SilverStripe\Control\Controller;
*/
protected $controller;

/**
* HeaderCollector constructor.
* @param Controller $controller
*/
public function __construct($controller)
{
$this->controller = $controller;
}

/**
* @return array
*/
public function getWidgets()
{
return [
'Headers' => [
'icon' => 'gear',
'widget' => 'PhpDebugBar.Widgets.VariableListWidget',
'map' => 'Headers.list',
'default' => '{}'
],
'Headers:badge' => [
'map' => 'Headers.count',
'default' => 0
]
];
}

/**
* @return array
*/
public function collect()
{

$result = $this->controller->getResponse()->getHeaders();

foreach ($result as $key => &$value) {
$value = trim(implode(PHP_EOL, explode('; ', $value)));
$value = implode(PHP_EOL . ' - ', explode(' ', $value));
}

return [
'count' => count($result),
'list' => $result
];
}

/**
* @return string
*/
public function getName()
{
return 'Headers';
}

public function getAssets()
{
return [];
}
}
5 changes: 5 additions & 0 deletions code/Extension/ControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LeKoala\DebugBar\Extension;

use LeKoala\DebugBar\Collector\HeaderCollector;
use LeKoala\DebugBar\Collector\SilverStripeCollector;
use LeKoala\DebugBar\DebugBar;
use SilverStripe\Control\Controller;
Expand Down Expand Up @@ -40,6 +41,10 @@ public function onAfterInit()
}

DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
// Add the headers Collector
if (!$debugbar->hasCollector('Headers') && DebugBar::config()->get('header_collector')) {
$debugbar->addCollector(new HeaderCollector($this->owner));
}
/** @var $timeData DebugBar\DataCollector\TimeDataCollector */
$timeData = $debugbar->getCollector('time');
if (!$timeData) {
Expand Down

0 comments on commit 66c2caf

Please sign in to comment.