forked from wintercms/wn-debugbar-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
224 lines (198 loc) · 7.84 KB
/
Plugin.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php namespace Winter\Debugbar;
use Backend\Classes\Controller as BackendController;
use Backend\Models\UserRole;
use Barryvdh\Debugbar\Facades\Debugbar;
use Barryvdh\Debugbar\LaravelDebugbar;
use Barryvdh\Debugbar\SymfonyHttpDriver;
use Cms\Classes\Controller as CmsController;
use Cms\Classes\Layout;
use Cms\Classes\Page;
use Config;
use Event;
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Session\SessionManager;
use System\Classes\CombineAssets;
use System\Classes\PluginBase;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
use Winter\Debugbar\Classes\WinterDebugbar;
use Winter\Debugbar\Collectors\BackendCollector;
use Winter\Debugbar\Collectors\CmsCollector;
use Winter\Debugbar\Collectors\ComponentsCollector;
use Winter\Debugbar\Collectors\ModelsCollector;
/**
* Winter.Debugbar Plugin
*/
class Plugin extends PluginBase
{
/**
* @var boolean Determine if this plugin should have elevated privileges.
*/
public $elevated = true;
/**
* Returns information about this plugin.
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'winter.debugbar::lang.plugin.name',
'description' => 'winter.debugbar::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-bug',
'homepage' => 'https://github.com/wintercms/wn-debugbar-plugin',
'replaces' => ['RainLab.Debugbar' => '<= 3.3.2'],
];
}
/**
* Registers the plugin
*/
public function register()
{
// Provide the winter.debugbar config under the debugbar namespace
Config::registerNamespaceAlias('winter.debugbar', 'debugbar');
// Register the service provider
$this->app->register(\Winter\Debugbar\Classes\ServiceProvider::class);
// Replace the LaravelDebugbar with the WinterDebugbar
$this->app->singleton(LaravelDebugbar::class, function ($app) {
$debugbar = new WinterDebugbar($app);
if ($app->bound(SessionManager::class)) {
$sessionManager = $app->make(SessionManager::class);
$httpDriver = new SymfonyHttpDriver($sessionManager);
$debugbar->setHttpDriver($httpDriver);
}
return $debugbar;
});
// Register alias
$alias = AliasLoader::getInstance();
$alias->alias('Debugbar', Debugbar::class);
// Register the asset bundle
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('$/winter/debugbar/assets/less/debugbar.less');
});
}
/**
* Boots service provider, Twig extensions, and alias facade, and injects collectors and resources.
*/
public function boot()
{
// Disabled by config, halt
if (Config::get('debugbar.enabled') === false) {
return;
}
// Register middleware
if (Config::get('app.debugAjax', false)) {
$this->app[HttpKernelContract::class]->pushMiddleware(\Winter\Debugbar\Middleware\InterpretsAjaxExceptions::class);
}
// Register custom collectors
if ($this->app->runningInBackend()) {
$this->addBackendCollectors();
} else {
$this->registerCmsTwigExtensions();
$this->addFrontendCollectors();
}
$this->addGlobalCollectors();
}
/**
* Add globally available collectors
*/
public function addGlobalCollectors()
{
if (Config::get('debugbar.collectors.models', true)) {
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
$modelsCollector = $this->app->make(ModelsCollector::class);
$debugBar->addCollector($modelsCollector);
}
}
/**
* Add collectors used by the frontend only
*/
public function addFrontendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
if (Config::get('debugbar.collectors.cms', true)) {
// Disable route collector as the CMS collector presents this info instead
Config::set('debugbar.collectors.route', false);
Event::listen('cms.page.beforeDisplay', function (CmsController $controller, $url, ?Page $page) use ($debugBar) {
if ($page) {
$collector = new CmsCollector($controller, $url, $page);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
}
if (Config::get('debugbar.collectors.components', true)) {
Event::listen('cms.page.initComponents', function (CmsController $controller, ?Page $page, ?Layout $layout) use ($debugBar) {
if ($page) {
$collector = new ComponentsCollector($controller, $page, $layout);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
}
}
/**
* Add collectors used by the Backend only
*/
public function addBackendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
if (Config::get('debugbar.collectors.backend', true)) {
Event::listen('backend.page.beforeDisplay', function (BackendController $controller, $action, array $params) use ($debugBar) {
$collector = new BackendCollector($controller, $action, $params);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
});
}
}
/**
* Registers extensions in the CMS Twig environment
*/
protected function registerCmsTwigExtensions()
{
$profile = new Profile;
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) use ($profile, $debugBar) {
$twig = $controller->getTwig();
if (!$twig->hasExtension(\Winter\DebugBar\Twig\Extension\Debug::class)) {
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Debug($this->app));
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Stopwatch($this->app));
}
if (!$twig->hasExtension(ProfilerExtension::class)) {
$twig->addExtension(new ProfilerExtension($profile));
}
});
if (class_exists(\DebugBar\Bridge\NamespacedTwigProfileCollector::class)) {
$debugBar->addCollector(new \DebugBar\Bridge\NamespacedTwigProfileCollector($profile));
} else {
$debugBar->addCollector(new \DebugBar\Bridge\TwigProfileCollector($profile));
}
}
/**
* Register the permissions used by the plugin
*
* @return array
*/
public function registerPermissions()
{
return [
'winter.debugbar.access_debugbar' => [
'tab' => 'winter.debugbar::lang.plugin.name',
'label' => 'winter.debugbar::lang.plugin.access_debugbar',
'roles' => [UserRole::CODE_DEVELOPER],
],
'winter.debugbar.access_stored_requests' => [
'tab' => 'winter.debugbar::lang.plugin.name',
'label' => 'winter.debugbar::lang.plugin.access_stored_requests',
'roles' => [UserRole::CODE_DEVELOPER],
],
];
}
}