Skip to content

Commit 4c5f34a

Browse files
vdauchybarryvdh
andauthored
Add view generation time to timeline tab when 'time' and 'view' collectors are enabled. (#1178)
* Add view 'time' when 'time' and 'view' collectors are enabled. * Use Debugbar instance to activate the tracking. * Update debugbar.php * Update ServiceProvider.php Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
1 parent eb61c2f commit 4c5f34a

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

config/debugbar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
'full_log' => false,
162162
],
163163
'views' => [
164+
'timeline' => false, // Add the views to the timeline (Experimental)
164165
'data' => false, //Note: Can slow down the application, because the data can be quite large..
165166
],
166167
'route' => [

src/DebugbarViewEngine.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\Debugbar;
6+
7+
use Illuminate\Contracts\View\Engine;
8+
9+
class DebugbarViewEngine implements Engine
10+
{
11+
/**
12+
* @var Engine
13+
*/
14+
protected $engine;
15+
16+
/**
17+
* @var LaravelDebugbar
18+
*/
19+
protected $laravelDebugbar;
20+
21+
/**
22+
* @param Engine $engine
23+
* @param LaravelDebugbar $laravelDebugbar
24+
*/
25+
public function __construct(Engine $engine, LaravelDebugbar $laravelDebugbar)
26+
{
27+
$this->engine = $engine;
28+
$this->laravelDebugbar = $laravelDebugbar;
29+
}
30+
31+
/**
32+
* @param string $path
33+
* @param array $data
34+
* @return string
35+
*/
36+
public function get($path, array $data = [])
37+
{
38+
return $this->laravelDebugbar->measure($path, function () use ($path, $data) {
39+
return $this->engine->get($path, $data);
40+
});
41+
}
42+
43+
/**
44+
* NOTE: This is done to support other Engine swap (example: Livewire).
45+
* @param $name
46+
* @param $arguments
47+
* @return mixed
48+
*/
49+
public function __call($name, $arguments)
50+
{
51+
return $this->engine->$name(...$arguments);
52+
}
53+
}

src/ServiceProvider.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use DebugBar\DataFormatter\DataFormatter;
88
use DebugBar\DataFormatter\DataFormatterInterface;
99
use Illuminate\Contracts\Http\Kernel;
10+
use Illuminate\Foundation\Application;
1011
use Illuminate\Routing\Router;
1112
use Illuminate\Session\SessionManager;
1213
use Illuminate\Support\Collection;
14+
use Illuminate\View\Engines\EngineResolver;
15+
use Barryvdh\Debugbar\Facade as DebugBar;
1316

1417
class ServiceProvider extends \Illuminate\Support\ServiceProvider
1518
{
@@ -51,6 +54,43 @@ function ($app) {
5154
}
5255
);
5356

57+
$this->app->extend(
58+
'view.engine.resolver',
59+
function (EngineResolver $resolver, Application $application): EngineResolver {
60+
$laravelDebugbar = $application->make(LaravelDebugbar::class);
61+
62+
$shouldTrackViewTime = $laravelDebugbar->isEnabled() &&
63+
$laravelDebugbar->shouldCollect('time', true) &&
64+
$laravelDebugbar->shouldCollect('views', true) &&
65+
$application['config']->get('debugbar.options.views.timeline', false);
66+
67+
if (! $shouldTrackViewTime) {
68+
/* Do not swap the engine to save performance */
69+
return $resolver;
70+
}
71+
72+
return new class($resolver, $laravelDebugbar) extends EngineResolver {
73+
74+
private $laravelDebugbar;
75+
76+
public function __construct(EngineResolver $resolver, LaravelDebugbar $laravelDebugbar)
77+
{
78+
foreach ($resolver->resolvers as $engine => $resolver) {
79+
$this->register($engine, $resolver);
80+
}
81+
$this->laravelDebugbar = $laravelDebugbar;
82+
}
83+
84+
public function register($engine, \Closure $resolver)
85+
{
86+
parent::register($engine, function () use ($resolver) {
87+
return new DebugbarViewEngine($resolver(), $this->laravelDebugbar);
88+
});
89+
}
90+
};
91+
}
92+
);
93+
5494
$this->commands(['command.debugbar.clear']);
5595

5696
Collection::macro('debug', function () {

0 commit comments

Comments
 (0)