Skip to content

Commit 90ecc7b

Browse files
authored
Add more data to timeline (barryvdh#1726)
1 parent c765a52 commit 90ecc7b

File tree

6 files changed

+40
-48
lines changed

6 files changed

+40
-48
lines changed

config/debugbar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,11 @@
232232
'hard_limit' => 500, // After the hard limit, queries are ignored
233233
],
234234
'mail' => [
235-
'timeline' => false, // Add mails to the timeline
235+
'timeline' => true, // Add mails to the timeline
236236
'show_body' => true,
237237
],
238238
'views' => [
239-
'timeline' => false, // Add the views to the timeline (Experimental)
239+
'timeline' => true, // Add the views to the timeline
240240
'data' => false, // True for all data, 'keys' for only names, false for no parameters.
241241
'group' => 50, // Group duplicate views. Pass value to auto-group, or true/false to force
242242
'exclude_paths' => [ // Add the paths which you don't want to appear in the views

src/DataCollector/EventCollector.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class EventCollector extends TimeDataCollector
2222
public function __construct($requestStartTime = null, $collectValues = false)
2323
{
2424
parent::__construct($requestStartTime);
25-
$this->previousTime = microtime(true);
2625
$this->collectValues = $collectValues;
2726
$this->setDataFormatter(new SimpleFormatter());
2827
}
@@ -32,8 +31,7 @@ public function onWildcardEvent($name = null, $data = [])
3231
$currentTime = microtime(true);
3332

3433
if (! $this->collectValues) {
35-
$this->addMeasure($name, $this->previousTime, $currentTime);
36-
$this->previousTime = $currentTime;
34+
$this->addMeasure($name, $currentTime, $currentTime, [], null, 'Events');
3735

3836
return;
3937
}
@@ -74,8 +72,7 @@ public function onWildcardEvent($name = null, $data = [])
7472

7573
$params['listeners.' . $i] = $listener;
7674
}
77-
$this->addMeasure($name, $this->previousTime, $currentTime, $params);
78-
$this->previousTime = $currentTime;
75+
$this->addMeasure($name, $currentTime, $currentTime, $params, null, 'Events');
7976
}
8077

8178
public function subscribe(Dispatcher $events)

src/DataCollector/QueryCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function addQuery($query)
203203
];
204204

205205
if ($this->timeCollector !== null) {
206-
$this->timeCollector->addMeasure(Str::limit($sql, 100), $startTime, $endTime, [], 'db');
206+
$this->timeCollector->addMeasure(Str::limit($sql, 100), $startTime, $endTime, [], 'db', 'Database Query');
207207
}
208208
}
209209

src/DataCollector/ViewCollector.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use DebugBar\DataCollector\AssetProvider;
77
use DebugBar\DataCollector\DataCollector;
88
use DebugBar\DataCollector\Renderable;
9+
use DebugBar\DataCollector\TimeDataCollector;
10+
use Illuminate\Support\Str;
911
use Illuminate\View\View;
1012

1113
class ViewCollector extends DataCollector implements Renderable, AssetProvider
@@ -15,21 +17,24 @@ class ViewCollector extends DataCollector implements Renderable, AssetProvider
1517
protected $collect_data;
1618
protected $exclude_paths;
1719
protected $group;
20+
protected $timeCollector;
1821

1922
/**
2023
* Create a ViewCollector
2124
*
2225
* @param bool|string $collectData Collects view data when true
2326
* @param string[] $excludePaths Paths to exclude from collection
2427
* @param int|bool $group Group the same templates together
28+
* @param TimeDataCollector|null TimeCollector
2529
* */
26-
public function __construct($collectData = true, $excludePaths = [], $group = true)
30+
public function __construct($collectData = true, $excludePaths = [], $group = true, ?TimeDataCollector $timeCollector = null)
2731
{
2832
$this->setDataFormatter(new SimpleFormatter());
2933
$this->collect_data = $collectData;
3034
$this->templates = [];
3135
$this->exclude_paths = $excludePaths;
3236
$this->group = $group;
37+
$this->timeCollector = $timeCollector;
3338
}
3439

3540
public function getName()
@@ -103,6 +108,11 @@ public function addView(View $view)
103108
}
104109

105110
$this->addTemplate($name, $data, $type, $path);
111+
112+
if ($this->timeCollector !== null) {
113+
$time = microtime(true);
114+
$this->timeCollector->addMeasure('View: ' . $name, $time, $time, [], 'views', 'View');
115+
}
106116
}
107117

108118
private function getInertiaView(string $name, array $data, ?string $path)

src/LaravelDebugbar.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,31 @@ public function boot()
196196
$this['time']->showMemoryUsage();
197197
}
198198

199-
if (! $this->isLumen() && $startTime) {
199+
if ($startTime) {
200200
$app->booted(
201201
function () use ($startTime) {
202202
$this->addMeasure('Booting', $startTime, microtime(true), [], 'time');
203203
}
204204
);
205205
}
206206

207-
$this->startMeasure('application', 'Application', 'time');
207+
if ($events) {
208+
$events->listen(\Illuminate\Routing\Events\Routing::class, function() {
209+
$this->startMeasure('Routing');
210+
});
211+
$events->listen(\Illuminate\Routing\Events\RouteMatched::class, function() {
212+
$this->stopMeasure('Routing');
213+
});
214+
215+
$events->listen(\Illuminate\Routing\Events\PreparingResponse::class, function() {
216+
$this->startMeasure('Preparing Response');
217+
});
218+
$events->listen(\Illuminate\Routing\Events\ResponsePrepared::class, function() {
219+
$this->stopMeasure('Preparing Response');
220+
});
221+
} else {
222+
$this->startMeasure('application', 'Application', 'time');
223+
}
208224
}
209225

210226
if ($this->shouldCollect('memory', true)) {
@@ -253,7 +269,12 @@ function () use ($startTime) {
253269
$collectData = $config->get('debugbar.options.views.data', true);
254270
$excludePaths = $config->get('debugbar.options.views.exclude_paths', []);
255271
$group = $config->get('debugbar.options.views.group', true);
256-
$this->addCollector(new ViewCollector($collectData, $excludePaths, $group));
272+
if ($this->hasCollector('time') && $config->get('debugbar.options.views.timeline', false)) {
273+
$timeCollector = $this['time'];
274+
} else {
275+
$timeCollector = null;
276+
}
277+
$this->addCollector(new ViewCollector($collectData, $excludePaths, $group, $timeCollector));
257278
$events->listen(
258279
'composing:*',
259280
function ($event, $params) {

src/ServiceProvider.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,6 @@ function ($app) {
4848
}
4949
);
5050

51-
$this->app->extend(
52-
'view',
53-
function (Factory $factory, Container $application): Factory {
54-
$laravelDebugbar = $application->make(LaravelDebugbar::class);
55-
56-
$shouldTrackViewTime = $laravelDebugbar->isEnabled() &&
57-
$laravelDebugbar->shouldCollect('time', true) &&
58-
$laravelDebugbar->shouldCollect('views', true) &&
59-
$application['config']->get('debugbar.options.views.timeline', false);
60-
61-
if (! $shouldTrackViewTime) {
62-
/* Do not swap the engine to save performance */
63-
return $factory;
64-
}
65-
66-
$extensions = array_reverse($factory->getExtensions());
67-
$engines = array_flip($extensions);
68-
$enginesResolver = $application->make('view.engine.resolver');
69-
70-
foreach ($engines as $engine => $extension) {
71-
$resolved = $enginesResolver->resolve($engine);
72-
73-
$factory->addExtension($extension, $engine, function () use ($resolved, $laravelDebugbar) {
74-
return new DebugbarViewEngine($resolved, $laravelDebugbar);
75-
});
76-
}
77-
78-
// returns original order of extensions
79-
foreach ($extensions as $extension => $engine) {
80-
$factory->addExtension($extension, $engine);
81-
}
82-
83-
return $factory;
84-
}
85-
);
86-
8751
Collection::macro('debug', function () {
8852
debug($this);
8953
return $this;

0 commit comments

Comments
 (0)