Skip to content

Commit 43ba768

Browse files
Broutardbarryvdh
authored andcommitted
feat: add a forget button in the cache panel (barryvdh#728)
* feat: add a forget button in the cache panel * LaravelCacheWidget now extends TimelineWidget * fix: escape value
1 parent 99abc7f commit 43ba768

File tree

6 files changed

+118
-4
lines changed

6 files changed

+118
-4
lines changed

src/Controllers/CacheController.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace Barryvdh\Debugbar\Controllers;
2+
3+
use Illuminate\Http\Response;
4+
5+
class CacheController extends BaseController
6+
{
7+
/**
8+
* Forget a cache key
9+
*
10+
*/
11+
public function delete($key, $tags = '')
12+
{
13+
$cache = app('cache');
14+
15+
if (!empty($tags)) {
16+
$tags = json_decode($tags, true);
17+
$cache = $cache->tags($tags);
18+
} else {
19+
unset($tags);
20+
}
21+
22+
$success = $cache->forget($key);
23+
24+
return response()->json(compact('success'));
25+
}
26+
27+
}

src/DataCollector/CacheCollector.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,26 @@ public function onCacheEvent(CacheEvent $event)
3434
$class = get_class($event);
3535
$params = get_object_vars($event);
3636

37-
if(isset($params['value'])) {
37+
$label = $this->classMap[$class];
38+
39+
if (isset($params['value'])) {
3840
if ($this->collectValues) {
39-
$params['value'] = $this->getDataFormatter()->formatVar($event->value);
41+
$params['value'] = htmlspecialchars($this->getDataFormatter()->formatVar($event->value));
4042
} else {
4143
unset($params['value']);
4244
}
4345
}
4446

47+
48+
if (!empty($params['key']) && in_array($label, ['hit', 'written'])) {
49+
$params['delete'] = route('debugbar.cache.delete', [
50+
'key' => urlencode($params['key']),
51+
'tags' => !empty($params['tags']) ? json_encode($params['tags']) : '',
52+
]);
53+
}
54+
4555
$time = microtime(true);
46-
$this->addMeasure($this->classMap[$class] . "\t" . $event->key, $time, $time, $params);
56+
$this->addMeasure($label . "\t" . $event->key, $time, $time, $params);
4757
}
4858

4959

@@ -72,7 +82,7 @@ public function getWidgets()
7282
return [
7383
'cache' => [
7484
'icon' => 'clipboard',
75-
'widget' => 'PhpDebugBar.Widgets.TimelineWidget',
85+
'widget' => 'PhpDebugBar.Widgets.LaravelCacheWidget',
7686
'map' => 'cache',
7787
'default' => '{}',
7888
],

src/JavascriptRenderer.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(DebugBar $debugBar, $baseUrl = null, $basePath = nul
2020
$this->cssFiles['laravel'] = __DIR__ . '/Resources/laravel-debugbar.css';
2121
$this->cssVendors['fontawesome'] = __DIR__ . '/Resources/vendor/font-awesome/style.css';
2222
$this->jsFiles['laravel-sql'] = __DIR__ . '/Resources/sqlqueries/widget.js';
23+
$this->jsFiles['laravel-cache'] = __DIR__ . '/Resources/cache/widget.js';
2324
}
2425

2526
/**

src/Resources/cache/widget.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(function($) {
2+
3+
var csscls = PhpDebugBar.utils.makecsscls('phpdebugbar-widgets-');
4+
5+
/**
6+
* Widget for the displaying cache events
7+
*
8+
* Options:
9+
* - data
10+
*/
11+
var LaravelCacheWidget = PhpDebugBar.Widgets.LaravelCacheWidget = PhpDebugBar.Widgets.TimelineWidget.extend({
12+
13+
tagName: 'ul',
14+
15+
className: csscls('timeline cache'),
16+
17+
onForgetClick: function(e, el) {
18+
e.stopPropagation();
19+
20+
$.ajax({
21+
url: $(el).attr("data-url"),
22+
type: 'DELETE',
23+
success: function(result) {
24+
$(el).fadeOut(200);
25+
}
26+
});
27+
},
28+
29+
render: function() {
30+
LaravelCacheWidget.__super__.render.apply(this);
31+
32+
this.bindAttr('data', function(data) {
33+
34+
if (data.measures) {
35+
var self = this;
36+
var lines = this.$el.find('.'+csscls('measure'));
37+
38+
for (var i = 0; i < data.measures.length; i++) {
39+
var measure = data.measures[i];
40+
var m = lines[i];
41+
42+
if (measure.params && !$.isEmptyObject(measure.params)) {
43+
44+
if (measure.params.delete && measure.params.key) {
45+
$('<a />')
46+
.addClass(csscls('forget'))
47+
.text('forget')
48+
.attr('data-url', measure.params.delete)
49+
.one('click', function(e) { self.onForgetClick(e, this); })
50+
.appendTo(m);
51+
}
52+
}
53+
}
54+
}
55+
});
56+
}
57+
});
58+
59+
})(PhpDebugBar.$);

src/Resources/laravel-debugbar.css

+12
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,15 @@ ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-table-list-item {
303303
.phpdebugbar-text-muted {
304304
color: #888;
305305
}
306+
307+
ul.phpdebugbar-widgets-cache a.phpdebugbar-widgets-forget {
308+
float: right;
309+
font-size: 12px;
310+
padding: 0 4px;
311+
background: #f4645f;
312+
margin: 0 2px;
313+
border-radius: 4px;
314+
color: #fff;
315+
text-decoration: none;
316+
line-height: 1.5rem;
317+
}

src/ServiceProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public function boot()
9393
'uses' => 'AssetController@js',
9494
'as' => 'debugbar.assets.js',
9595
]);
96+
97+
$router->delete('cache/{key}/{tags?}', [
98+
'uses' => 'CacheController@delete',
99+
'as' => 'debugbar.cache.delete',
100+
]);
96101
});
97102

98103
$this->registerMiddleware(InjectDebugbar::class);

0 commit comments

Comments
 (0)