Skip to content

Collect other eloquent model events #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,16 @@ function (\Illuminate\Database\Events\ConnectionEstablished $event) {
if ($this->shouldCollect('models', true) && $events) {
try {
$this->addCollector(new ObjectCountCollector('models'));
$events->listen('eloquent.retrieved:*', function ($event, $models) {
foreach (array_filter($models) as $model) {
$this['models']->countClass($model);
}
});
$eventList = ['retrieved', 'created', 'updated', 'deleted'];
$this['models']->setKeyMap(array_combine($eventList, array_map('ucfirst', $eventList)));
$this['models']->collectCountSummary(true);
foreach ($eventList as $event) {
$events->listen("eloquent.{$event}: *", function ($event, $models) {
$event = explode(': ', $event);
$count = count(array_filter($models));
$this['models']->countClass($event[1], $count, explode('.', $event[0])[1]);
});
}
} catch (Exception $e) {
$this->addCollectorException('Cannot add Models Collector', $e);
}
Expand Down
13 changes: 10 additions & 3 deletions src/Resources/laravel-debugbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ div.phpdebugbar .phpdebugbar-header select {
}

dl.phpdebugbar-widgets-kvlist dt,
dl.phpdebugbar-widgets-kvlist dd {
dl.phpdebugbar-widgets-kvlist dd,
table.phpdebugbar-widgets-tablevar td {
min-height: 20px;
line-height: 20px;
padding: 4px 5px 5px;
Expand All @@ -239,7 +240,8 @@ dl.phpdebugbar-widgets-kvlist dd.phpdebugbar-widgets-value.phpdebugbar-widgets-p
background: transparent;
}

dl.phpdebugbar-widgets-kvlist dt {
dl.phpdebugbar-widgets-kvlist dt,
table.phpdebugbar-widgets-tablevar td:first-child {
width: calc(25% - 10px);
}

Expand Down Expand Up @@ -459,7 +461,8 @@ div.phpdebugbar-widgets-sqlqueries table.phpdebugbar-widgets-params td.phpdebugb
margin-left: 3px;
}

ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item:nth-child(even) {
ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item:nth-child(even),
table.phpdebugbar-widgets-tablevar tr:nth-child(even) {
background-color: var(--debugbar-background-alt);
}

Expand All @@ -476,6 +479,10 @@ div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugb
float: left;
}

table.phpdebugbar-widgets-tablevar td {
border: 0;
}

div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugbar-widgets-value.phpdebugbar-widgets-info {
color: #1299DA;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Support/Clockwork/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ public function convert($data)
$output['modelsCreated'] = [];
$output['modelsUpdated'] = [];
$output['modelsDeleted'] = [];
$output['modelsRetrieved'] = $data['models']['data'];
$output['modelsRetrieved'] = [];

foreach ($data['models']['data'] as $model => $value) {
foreach ($value as $event => $count) {
$eventKey = 'models' . ucfirst($event);
if (isset($output[$eventKey])) {
$output[$eventKey][$model] = $count;
}
}
}
}

if (isset($data['views']['templates'])) {
Expand Down
10 changes: 7 additions & 3 deletions tests/DataCollector/JobsCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,29 @@ public function testItCollectsDispatchedJobs()
/** @var \DebugBar\DataCollector\ObjectCountCollector $collector */
$collector = debugbar()->getCollector('jobs');
$collector->setXdebugLinkTemplate('');
$collector->setKeyMap([]);
$data = [];

$this->assertEquals(
['data' => [], 'count' => 0, 'is_counter' => true],
['data' => $data, 'count' => 0, 'key_map' => [], 'is_counter' => true],
$collector->collect()
);

OrderShipped::dispatch(1);

$data[OrderShipped::class] = ['value' => 1];
$this->assertEquals(
['data' => [OrderShipped::class => 1], 'count' => 1, 'is_counter' => true],
['data' => $data, 'count' => 1, 'key_map' => [], 'is_counter' => true],
$collector->collect()
);

dispatch(new SendNotification());
dispatch(new SendNotification());
dispatch(new SendNotification());

$data[SendNotification::class] = ['value' => 3];
$this->assertEquals(
['data' => [OrderShipped::class => 1, SendNotification::class => 3], 'count' => 4, 'is_counter' => true],
['data' => $data, 'count' => 4, 'key_map' => [], 'is_counter' => true],
$collector->collect()
);
}
Expand Down
36 changes: 31 additions & 5 deletions tests/DataCollector/ModelsCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ class ModelsCollectorTest extends TestCase
public function testItCollectsRetrievedModels()
{
$this->loadLaravelMigrations();

debugbar()->boot();

/** @var \DebugBar\DataCollector\ObjectCountCollector $collector */
$collector = debugbar()->getCollector('models');
$collector->setXdebugLinkTemplate('');
$collector->collectCountSummary(false);
$collector->setKeyMap([]);
$data = [];

$this->assertEquals(
['data' => $data, 'key_map' => [], 'count' => 0, 'is_counter' => true],
$collector->collect()
);

User::create([
'name' => 'John Doe',
Expand All @@ -34,22 +41,41 @@ public function testItCollectsRetrievedModels()
'password' => Hash::make('password'),
]);

$data[User::class] = ['created' => 2];
$this->assertEquals(
['data' => [], 'count' => 0, 'is_counter' => true],
['data' => $data, 'key_map' => [], 'count' => 2, 'is_counter' => true],
$collector->collect()
);

User::first();
$user = User::first();

$data[User::class]['retrieved'] = 1;
$this->assertEquals(
['data' => [User::class => 1], 'count' => 1, 'is_counter' => true],
['data' => $data, 'key_map' => [], 'count' => 3, 'is_counter' => true],
$collector->collect()
);

$user->update(['name' => 'Jane Doe']);

$data[User::class]['updated'] = 1;
$this->assertEquals(
['data' => $data, 'key_map' => [], 'count' => 4, 'is_counter' => true],
$collector->collect()
);

Person::all();

$data[Person::class] = ['retrieved' => 2];
$this->assertEquals(
['data' => $data, 'key_map' => [], 'count' => 6, 'is_counter' => true],
$collector->collect()
);

$user->delete();

$data[User::class]['deleted'] = 1;
$this->assertEquals(
['data' => [User::class => 1, Person::class => 2], 'count' => 3, 'is_counter' => true],
['data' => $data, 'key_map' => [], 'count' => 7, 'is_counter' => true],
$collector->collect()
);
}
Expand Down
Loading