Skip to content

Commit 676247e

Browse files
authored
Refactor Models, Jobs to ObjectCountCollector (#1517)
1 parent 9ec8bbb commit 676247e

File tree

5 files changed

+21
-138
lines changed

5 files changed

+21
-138
lines changed

src/DataCollector/JobsCollector.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/DataCollector/ModelsCollector.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/LaravelDebugbar.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Barryvdh\Debugbar\DataCollector\GateCollector;
1010
use Barryvdh\Debugbar\DataCollector\LaravelCollector;
1111
use Barryvdh\Debugbar\DataCollector\LogsCollector;
12-
use Barryvdh\Debugbar\DataCollector\ModelsCollector;
1312
use Barryvdh\Debugbar\DataCollector\MultiAuthCollector;
1413
use Barryvdh\Debugbar\DataCollector\QueryCollector;
1514
use Barryvdh\Debugbar\DataCollector\SessionCollector;
@@ -24,6 +23,7 @@
2423
use DebugBar\DataCollector\ExceptionsCollector;
2524
use DebugBar\DataCollector\MemoryCollector;
2625
use DebugBar\DataCollector\MessagesCollector;
26+
use DebugBar\DataCollector\ObjectCountCollector;
2727
use Barryvdh\Debugbar\DataCollector\PhpInfoCollector;
2828
use DebugBar\DataCollector\RequestDataCollector;
2929
use DebugBar\DataCollector\TimeDataCollector;
@@ -460,8 +460,12 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) use ($queryC
460460

461461
if ($this->shouldCollect('models', true)) {
462462
try {
463-
$modelsCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\ModelsCollector');
464-
$this->addCollector($modelsCollector);
463+
$this->addCollector(new ObjectCountCollector('models'));
464+
$this->app['events']->listen('eloquent.retrieved:*', function ($event, $models) {
465+
foreach (array_filter($models) as $model) {
466+
$this['models']->countClass($model);
467+
}
468+
});
465469
} catch (\Exception $e) {
466470
// No Models collector
467471
}
@@ -598,8 +602,10 @@ public function __toString(): string
598602

599603
if ($this->shouldCollect('jobs', false)) {
600604
try {
601-
$jobsCollector = $this->app->make('Barryvdh\Debugbar\DataCollector\JobsCollector');
602-
$this->addCollector($jobsCollector);
605+
$this->addCollector(new ObjectCountCollector('jobs', 'briefcase'));
606+
$this->app['events']->listen(\Illuminate\Queue\Events\JobQueued::class, function ($event) {
607+
$this['jobs']->countClass($event->job);
608+
});
603609
} catch (\Exception $e) {
604610
// No Jobs collector
605611
}

tests/DataCollector/JobsCollectorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ public function testItCollectsDispatchedJobs()
3131

3232
debugbar()->boot();
3333

34-
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
34+
/** @var \DebugBar\DataCollector\ObjectCountCollector $collector */
3535
$collector = debugbar()->getCollector('jobs');
36+
$collector->setXdebugLinkTemplate('');
3637

3738
$this->assertEquals(
38-
['data' => [], 'count' => 0],
39+
['data' => [], 'count' => 0, 'is_counter' => true],
3940
$collector->collect()
4041
);
4142

4243
OrderShipped::dispatch(1);
4344

4445
$this->assertEquals(
45-
['data' => [OrderShipped::class => 1], 'count' => 1],
46+
['data' => [OrderShipped::class => 1], 'count' => 1, 'is_counter' => true],
4647
$collector->collect()
4748
);
4849

@@ -51,7 +52,7 @@ public function testItCollectsDispatchedJobs()
5152
dispatch(new SendNotification());
5253

5354
$this->assertEquals(
54-
['data' => [OrderShipped::class => 1, SendNotification::class => 3], 'count' => 4],
55+
['data' => [OrderShipped::class => 1, SendNotification::class => 3], 'count' => 4, 'is_counter' => true],
5556
$collector->collect()
5657
);
5758
}

tests/DataCollector/ModelsCollectorTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public function testItCollectsRetrievedModels()
1818

1919
debugbar()->boot();
2020

21-
/** @var \Barryvdh\Debugbar\DataCollector\ModelsCollector $collector */
21+
/** @var \DebugBar\DataCollector\ObjectCountCollector $collector */
2222
$collector = debugbar()->getCollector('models');
23+
$collector->setXdebugLinkTemplate('');
2324

2425
User::create([
2526
'name' => 'John Doe',
@@ -34,21 +35,21 @@ public function testItCollectsRetrievedModels()
3435
]);
3536

3637
$this->assertEquals(
37-
['data' => [], 'count' => 0],
38+
['data' => [], 'count' => 0, 'is_counter' => true],
3839
$collector->collect()
3940
);
4041

4142
User::first();
4243

4344
$this->assertEquals(
44-
['data' => [User::class => 1], 'count' => 1],
45+
['data' => [User::class => 1], 'count' => 1, 'is_counter' => true],
4546
$collector->collect()
4647
);
4748

4849
Person::all();
4950

5051
$this->assertEquals(
51-
['data' => [User::class => 1, Person::class => 2], 'count' => 3],
52+
['data' => [User::class => 1, Person::class => 2], 'count' => 3, 'is_counter' => true],
5253
$collector->collect()
5354
);
5455
}

0 commit comments

Comments
 (0)