Skip to content

Commit

Permalink
add cache tab (barryvdh#678)
Browse files Browse the repository at this point in the history
* add cache tab. barryvdh#634

* More universal cache events checking

* cache tab: data formatter instead of serialize
  • Loading branch information
adelf authored and barryvdh committed Jul 24, 2017
1 parent e5257dc commit 41b818f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
'logs' => false, // Add the latest log messages
'files' => false, // Show the included files
'config' => false, // Display config settings
'cache' => false, // Display cache events
],

/*
Expand Down Expand Up @@ -153,6 +154,9 @@
'logs' => [
'file' => null
],
'cache' => [
'values' => true // collect cache values
],
],

/*
Expand Down
114 changes: 114 additions & 0 deletions src/DataCollector/CacheCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
namespace Barryvdh\Debugbar\DataCollector;

use DebugBar\DataCollector\TimeDataCollector;
use Illuminate\Events\Dispatcher;

class CacheCollector extends TimeDataCollector
{
/** @var bool */
protected $collectValues;

/** @var array */
protected $classMap = [
'Illuminate\Cache\Events\CacheHit' => 'hit',
'Illuminate\Cache\Events\CacheMissed' => 'missed',
'Illuminate\Cache\Events\KeyWritten' => 'write',
'Illuminate\Cache\Events\KeyForgotten' => 'delete',
];

public function __construct($requestStartTime = null, $collectValues)
{
parent::__construct();

$this->collectValues = $collectValues;
}

public function onClassEvent($event)
{
$class = get_class($event);
if (isset($this->classMap[$class])) {
$params = [];

if(isset($event->minutes)) {
$params['minutes'] = $event->minutes;
}

if(isset($event->value)) {
if ($this->collectValues) {
$params['value'] = $this->getDataFormatter()->formatVar($event->value);
} else {
$params['value'] = '(values collecting turned off)';
}
}

if(!empty($event->tags)) {
$params['tags'] = $event->tags;
}

$time = microtime(true);
$this->addMeasure($this->classMap[$class] . ' ' . $event->key, $time, $time, $params);
}
}

public function onStringEvent($event, $payload)
{
$params = [];

if(is_array($payload)) {
if (isset($payload[2])) {
$params['minutes'] = $payload[2];
}

if (isset($payload[1])) {
if ($this->collectValues) {
$params['value'] = $this->getDataFormatter()->formatVar($payload[1]);
} else {
$params['value'] = '(values collecting turned off)';
}
}
}

$time = microtime(true);
$this->addMeasure( str_replace('cache.', '', $event) . ' ' . (is_array($payload) ? $payload[0] : $payload),
$time, $time, $params);
}

public function subscribe(Dispatcher $events)
{
if (class_exists('Illuminate\Cache\Events\CacheHit')) {
$events->listen('Illuminate\Cache\Events\*', [$this, 'onClassEvent']);
} else {
$events->listen('cache.*', [$this, 'onStringEvent']);
}
}

public function collect()
{
$data = parent::collect();
$data['nb_measures'] = count($data['measures']);

return $data;
}

public function getName()
{
return 'cache';
}

public function getWidgets()
{
return [
'cache' => [
'icon' => 'lock',
'widget' => 'PhpDebugBar.Widgets.TimelineWidget',
'map' => 'cache',
'default' => '{}',
],
'cache:badge' => [
'map' => 'cache.nb_measures',
'default' => 0,
],
];
}
}
20 changes: 20 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Barryvdh\Debugbar;

use Barryvdh\Debugbar\DataCollector\AuthCollector;
use Barryvdh\Debugbar\DataCollector\CacheCollector;
use Barryvdh\Debugbar\DataCollector\EventCollector;
use Barryvdh\Debugbar\DataCollector\FilesCollector;
use Barryvdh\Debugbar\DataCollector\GateCollector;
Expand Down Expand Up @@ -462,6 +463,25 @@ function ($query, $bindings = null, $time = null, $connectionName = null) use ($
}
}

if ($this->shouldCollect('cache', false) && isset($this->app['events'])) {
try {
$collectValues = $this->app['config']->get('debugbar.options.cache.values', true);
$startTime = $this->app['request']->server('REQUEST_TIME_FLOAT');
$cacheCollector = new CacheCollector($startTime, $collectValues);
$this->addCollector($cacheCollector);
$this->app['events']->subscribe($cacheCollector);

} catch (\Exception $e) {
$this->addThrowable(
new Exception(
'Cannot add CacheCollector to Laravel Debugbar: ' . $e->getMessage(),
$e->getCode(),
$e
)
);
}
}

$renderer = $this->getJavascriptRenderer();
$renderer->setIncludeVendors($this->app['config']->get('debugbar.include_vendors', true));
$renderer->setBindAjaxHandlerToXHR($app['config']->get('debugbar.capture_ajax', true));
Expand Down

0 comments on commit 41b818f

Please sign in to comment.