Skip to content

Commit

Permalink
Use list for Models collector + sort
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh authored Apr 16, 2020
1 parent d1277d7 commit b949786
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/DataCollector/ModelsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Barryvdh\Debugbar\DataCollector;

use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\DataCollectorInterface;
use DebugBar\DataCollector\Renderable;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str;

/**
* Collector for Models.
*/
class ModelsCollector extends MessagesCollector
class ModelsCollector extends DataCollector implements DataCollectorInterface, Renderable
{
public $models = [];

Expand All @@ -19,9 +20,6 @@ class ModelsCollector extends MessagesCollector
*/
public function __construct(Dispatcher $events)
{
parent::__construct('models');
$this->setDataFormatter(new SimpleFormatter());

$events->listen('eloquent.*', function ($event, $models) {
if (Str::contains($event, 'eloquent.retrieved')) {
foreach (array_filter($models) as $model) {
Expand All @@ -34,21 +32,31 @@ public function __construct(Dispatcher $events)

public function collect()
{
foreach ($this->models as $type => $count) {
$this->addMessage($count, $type);
}
ksort($this->models, SORT_NUMERIC);

return [
'count' => array_sum($this->models),
'messages' => $this->getMessages(),
];
return array_reverse($this->models);
}

public function getWidgets()
/**
* {@inheritDoc}
*/
public function getName()
{
$widgets = parent::getWidgets();
$widgets['models']['icon'] = 'cubes';
return 'models';
}

return $widgets;
/**
* {@inheritDoc}
*/
public function getWidgets()
{
return [
"models" => [
"icon" => "cubes",
"widget" => "PhpDebugBar.Widgets.HtmlVariableListWidget",
"map" => "models",
"default" => "{}"
]
];
}
}

0 comments on commit b949786

Please sign in to comment.