-
Notifications
You must be signed in to change notification settings - Fork 20
/
Plugin.php
221 lines (194 loc) · 7.42 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
<?php namespace RainLab\Debugbar;
use App;
use Event;
use Config;
use Cms\Classes\Page;
use Cms\Classes\Layout;
use Cms\Classes\Controller as CmsController;
use Backend\Models\UserRole;
use Backend\Classes\Controller as BackendController;
use System\Classes\CombineAssets;
use System\Classes\PluginBase;
use RainLab\Debugbar\DataCollectors\OctoberBackendCollector;
use RainLab\Debugbar\DataCollectors\OctoberCmsCollector;
use RainLab\Debugbar\DataCollectors\OctoberComponentsCollector;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
/**
* Plugin Information File
*/
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' => 'rainlab.debugbar::lang.plugin.name',
'description' => 'rainlab.debugbar::lang.plugin.description',
'author' => 'RainLab',
'icon' => 'icon-bug',
'homepage' => 'https://github.com/rainlab/debugbar-plugin'
];
}
/**
* boot service provider, Twig extensions, and alias facade.
*/
public function boot()
{
// Transfer config to debugbar namespace
Config::set('debugbar', Config::get('rainlab.debugbar::config', []));
// Disabled by config, halt
if (Config::get('debugbar.enabled') === false) {
return;
}
// Register service provider
App::register(\RainLab\Debugbar\Classes\ServiceProvider::class);
// Register alias
$alias = AliasLoader::getInstance();
$alias->alias('Debugbar', \Barryvdh\Debugbar\Facades\Debugbar::class);
// Register middleware
if (Config::get('app.debug_ajax', Config::get('app.debugAjax', false))) {
$this->app[HttpKernelContract::class]->pushMiddleware(\RainLab\Debugbar\Middleware\InterpretsAjaxExceptions::class);
}
// Custom debugbar collectors and extensions
$this->registerResourceInjection();
if (App::runningInBackend()) {
$this->addBackendCollectors();
}
else {
// Only Twig 2 is supported at this stage
if (class_exists('\Twig_Extension')) {
$this->registerCmsTwigExtensions();
}
$this->addFrontendCollectors();
}
$this->addGlobalCollectors();
}
/**
* register the service provider
*/
public function register()
{
/*
* Register asset bundles
*/
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('$/rainlab/debugbar/assets/less/debugbar.less');
});
}
/**
* addGlobalCollectors adds globally available collectors
*/
public function addGlobalCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
$modelsCollector = $this->app->make(\RainLab\Debugbar\DataCollectors\OctoberModelsCollector::class);
$debugBar->addCollector($modelsCollector);
}
/**
* addFrontendCollectors used by the frontend only
*/
public function addFrontendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
Event::listen('cms.page.beforeDisplay', function(CmsController $controller, $url, ?Page $page) use ($debugBar) {
if ($page) {
$collector = new OctoberCmsCollector($controller, $url, $page);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
Event::listen('cms.page.initComponents', function(CmsController $controller, ?Page $page, ?Layout $layout) use ($debugBar) {
if ($page) {
$collector = new OctoberComponentsCollector($controller, $page, $layout);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
}
/**
* addBackendCollectors used by the backend only
*/
public function addBackendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
Event::listen('backend.page.beforeDisplay', function (BackendController $controller, $action, array $params) use ($debugBar) {
$collector = new OctoberBackendCollector($controller, $action, $params);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
});
}
/**
* registerCmsTwigExtensions 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(\Barryvdh\Debugbar\Twig\Extension\Debug::class)) {
$twig->addExtension(new \Barryvdh\Debugbar\Twig\Extension\Debug($this->app));
$twig->addExtension(new \Barryvdh\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));
}
}
/**
* registerResourceInjection adds styling to the page
*/
protected function registerResourceInjection()
{
// Add styling
$addResources = function($controller) {
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
if ($debugBar->isEnabled()) {
$controller->addCss('/plugins/rainlab/debugbar/assets/css/debugbar.css');
}
};
Event::listen('backend.page.beforeDisplay', $addResources, 500);
Event::listen('cms.page.beforeDisplay', $addResources, 500);
}
/**
* Register the permissions used by the plugin
*
* @return array
*/
public function registerPermissions()
{
return [
'rainlab.debugbar.access_debugbar' => [
'tab' => 'rainlab.debugbar::lang.plugin.name',
'label' => 'rainlab.debugbar::lang.plugin.access_debugbar',
'roles' => UserRole::CODE_DEVELOPER,
],
'rainlab.debugbar.access_stored_requests' => [
'tab' => 'rainlab.debugbar::lang.plugin.name',
'label' => 'rainlab.debugbar::lang.plugin.access_stored_requests',
'roles' => UserRole::CODE_DEVELOPER,
],
];
}
}