forked from barryvdh/laravel-debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceProvider.php
155 lines (130 loc) · 4.5 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Barryvdh\Debugbar;
use Barryvdh\Debugbar\Middleware\DebugbarEnabled;
use Barryvdh\Debugbar\Middleware\InjectDebugbar;
use DebugBar\DataFormatter\DataFormatter;
use DebugBar\DataFormatter\DataFormatterInterface;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Foundation\Application;
use Illuminate\Routing\Router;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection;
use Illuminate\View\Engines\EngineResolver;
use Barryvdh\Debugbar\Facade as DebugBar;
class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$configPath = __DIR__ . '/../config/debugbar.php';
$this->mergeConfigFrom($configPath, 'debugbar');
$this->loadRoutesFrom(realpath(__DIR__ . '/debugbar-routes.php'));
$this->app->alias(
DataFormatter::class,
DataFormatterInterface::class
);
$this->app->singleton(LaravelDebugbar::class, function ($app) {
$debugbar = new LaravelDebugbar($app);
if ($app->bound(SessionManager::class)) {
$sessionManager = $app->make(SessionManager::class);
$httpDriver = new SymfonyHttpDriver($sessionManager);
$debugbar->setHttpDriver($httpDriver);
}
return $debugbar;
});
$this->app->alias(LaravelDebugbar::class, 'debugbar');
$this->app->singleton(
'command.debugbar.clear',
function ($app) {
return new Console\ClearCommand($app['debugbar']);
}
);
$this->app->extend(
'view.engine.resolver',
function (EngineResolver $resolver, Application $application): EngineResolver {
$laravelDebugbar = $application->make(LaravelDebugbar::class);
$shouldTrackViewTime = $laravelDebugbar->isEnabled() &&
$laravelDebugbar->shouldCollect('time', true) &&
$laravelDebugbar->shouldCollect('views', true) &&
$application['config']->get('debugbar.options.views.timeline', false);
if (! $shouldTrackViewTime) {
/* Do not swap the engine to save performance */
return $resolver;
}
return new class ($resolver, $laravelDebugbar) extends EngineResolver {
private $laravelDebugbar;
public function __construct(EngineResolver $resolver, LaravelDebugbar $laravelDebugbar)
{
foreach ($resolver->resolvers as $engine => $resolver) {
$this->register($engine, $resolver);
}
$this->laravelDebugbar = $laravelDebugbar;
}
public function register($engine, \Closure $resolver)
{
parent::register($engine, function () use ($resolver) {
return new DebugbarViewEngine($resolver(), $this->laravelDebugbar);
});
}
};
}
);
$this->commands(['command.debugbar.clear']);
Collection::macro('debug', function () {
debug($this);
return $this;
});
}
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$configPath = __DIR__ . '/../config/debugbar.php';
$this->publishes([$configPath => $this->getConfigPath()], 'config');
$this->registerMiddleware(InjectDebugbar::class);
}
/**
* Get the active router.
*
* @return Router
*/
protected function getRouter()
{
return $this->app['router'];
}
/**
* Get the config path
*
* @return string
*/
protected function getConfigPath()
{
return config_path('debugbar.php');
}
/**
* Publish the config file
*
* @param string $configPath
*/
protected function publishConfig($configPath)
{
$this->publishes([$configPath => config_path('debugbar.php')], 'config');
}
/**
* Register the Debugbar Middleware
*
* @param string $middleware
*/
protected function registerMiddleware($middleware)
{
$kernel = $this->app[Kernel::class];
$kernel->pushMiddleware($middleware);
}
}