Skip to content

Commit bda348f

Browse files
reininkbarryvdh
authored andcommitted
Add new models collector (barryvdh#947)
1 parent 2d19577 commit bda348f

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

config/debugbar.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
|
1010
| Debugbar is enabled by default, when debug is set to true in app.php.
1111
| You can override the value by setting enable to true or false instead of null.
12-
|
12+
|
1313
| You can provide an array of URI's that must be ignored (eg. 'api/*')
1414
|
1515
*/
@@ -79,7 +79,7 @@
7979
|
8080
*/
8181
'error_handler' => false,
82-
82+
8383
/*
8484
|--------------------------------------------------------------------------
8585
| Clockwork integration
@@ -122,6 +122,7 @@
122122
'files' => false, // Show the included files
123123
'config' => false, // Display config settings
124124
'cache' => false, // Display cache events
125+
'models' => false, // Display models
125126
],
126127

127128
/*
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\DataCollector;
4+
5+
use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
6+
use DebugBar\DataCollector\MessagesCollector;
7+
use Illuminate\Events\Dispatcher;
8+
use Illuminate\Support\Str;
9+
10+
/**
11+
* Collector for Models.
12+
*/
13+
class ModelsCollector extends MessagesCollector
14+
{
15+
public $models = [];
16+
17+
/**
18+
* @param Dispatcher $events
19+
*/
20+
public function __construct(Dispatcher $events)
21+
{
22+
parent::__construct('models');
23+
$this->setDataFormatter(new SimpleFormatter());
24+
25+
$events->listen('eloquent.*', function ($event, $models) {
26+
if (Str::contains($event, 'eloquent.retrieved')) {
27+
foreach ($models as $model) {
28+
$class = get_class($model);
29+
$this->models[$class] = ($this->models[$class] ?? 0) + 1;
30+
}
31+
}
32+
});
33+
}
34+
35+
public function collect()
36+
{
37+
foreach ($this->models as $type => $count) {
38+
$this->addMessage($count, $type);
39+
}
40+
41+
return [
42+
'count' => array_sum($this->models),
43+
'messages' => $this->getMessages(),
44+
];
45+
}
46+
47+
public function getWidgets()
48+
{
49+
$widgets = parent::getWidgets();
50+
$widgets['models']['icon'] = 'cubes';
51+
52+
return $widgets;
53+
}
54+
}

src/LaravelDebugbar.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Barryvdh\Debugbar\DataCollector\GateCollector;
88
use Barryvdh\Debugbar\DataCollector\LaravelCollector;
99
use Barryvdh\Debugbar\DataCollector\LogsCollector;
10+
use Barryvdh\Debugbar\DataCollector\ModelsCollector;
1011
use Barryvdh\Debugbar\DataCollector\MultiAuthCollector;
1112
use Barryvdh\Debugbar\DataCollector\QueryCollector;
1213
use Barryvdh\Debugbar\DataCollector\SessionCollector;
@@ -404,6 +405,15 @@ function ($query, $bindings = null, $time = null, $connectionName = null) use ($
404405
}
405406
}
406407

408+
if ($this->shouldCollect('models', false)) {
409+
try {
410+
$modelsCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\ModelsCollector');
411+
$this->addCollector($modelsCollector);
412+
} catch (\Exception $e){
413+
// No Models collector
414+
}
415+
}
416+
407417
if ($this->shouldCollect('mail', true) && class_exists('Illuminate\Mail\MailServiceProvider')) {
408418
try {
409419
$mailer = $this->app['mailer']->getSwiftMailer();

0 commit comments

Comments
 (0)