forked from concrete5cojp/concrete5_debugbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.php
182 lines (154 loc) · 7.06 KB
/
controller.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
<?php
namespace Concrete\Package\ConcretecmsDebugbar;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Package\Package;
use Concrete\Core\Permission\Key\Key;
use ConcreteDebugbar\Debugbar;
use DebugBar\DebugBarException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
class Controller extends Package
{
const PLACEHOLDER_TEXT = '<!-- debugbar:placeholder -->';
protected $pkgHandle = 'concretecms_debugbar';
protected $appVersionRequired = '9.0.0';
protected $pkgVersion = '1.0.1';
protected $pkgAutoloaderRegistries = [
'src/ConcreteDebugbar' => '\ConcreteDebugbar',
];
/**
* Returns the translated name of the package.
*
* @return string
*/
public function getPackageName(): string
{
return t('PHP Debug Bar for Concrete CMS');
}
/**
* Returns the translated package description.
*
* @return string
*/
public function getPackageDescription(): string
{
return t('Displays a debug bar to display profiling data like database queries, memory usage, etc.');
}
/**
* Install process of the package.
*/
public function install()
{
$this->registerAutoload();
if (!class_exists('DebugBar\DebugBar')) {
throw new UserMessageException(t('Required libraries not found.'));
}
$this->installContentFile('config/permissions.xml');
return parent::install();
}
public function on_start(): void
{
$this->registerAutoload();
$permission = Key::getByHandle('show_debug_bar');
if ($permission && $permission->validate()) {
$app = $this->getApplication();
$app->singleton('debugbar', Debugbar::class);
$app->bind('debugbar/renderer', function () use ($app) {
/** @var Debugbar $debugbar */
$debugbar = $app->make('debugbar');
return $debugbar->getJavascriptRenderer($this->getRelativePath() . '/vendor/maximebf/debugbar/src/DebugBar/Resources');
});
$app->bind('debugbar/messages', function () use ($app) {
$debugbar = $app->make('debugbar');
return $debugbar['messages'];
});
$app->bind('debugbar/time', function () use ($app) {
$debugbar = $app->make('debugbar');
return $debugbar['time'];
});
/** @var EventDispatcher $director */
$director = $app->make('director');
$director->addListener('on_before_dispatch', function ($event) use ($app) {
$app->make('debugbar/time')->startMeasure('dispatch', t('Run App'));
});
$director->addListener('on_page_view', function ($event) use ($app) {
$app->make('debugbar/time')->startMeasure('page_view', t('Render Page'));
});
$director->addListener('on_start', function ($event) use ($app) {
$app->make('debugbar/time')->startMeasure('render_view', t('Render View'));
});
$director->addListener('on_before_render', function ($event) use ($app) {
$debugbarRenderer = $app->make('debugbar/renderer');
$v = $event->getArgument('view');
$v->addHeaderItem($debugbarRenderer->renderHead());
$v->addFooterItem(self::PLACEHOLDER_TEXT);
$app->make('debugbar/time')->startMeasure('render_template', t('Render Template'));
});
$director->addListener('on_render_complete', function ($event) use ($app) {
if ($app->make('debugbar/time')->hasStartedMeasure('render_view')) {
$app->make('debugbar/time')->stopMeasure('render_view');
}
if ($app->make('debugbar/time')->hasStartedMeasure('render_template')) {
$app->make('debugbar/time')->stopMeasure('render_template');
}
});
$director->addListener('on_shutdown', function ($event) use ($app) {
if ($app->make('debugbar/time')->hasStartedMeasure('page_view')) {
$app->make('debugbar/time')->stopMeasure('page_view');
}
if ($app->make('debugbar/time')->hasStartedMeasure('dispatch')) {
$app->make('debugbar/time')->stopMeasure('dispatch');
}
});
$director->addListener('on_block_load', function ($event) use ($app) {
$bID = $event->getArgument('bID');
$btHandle = $event->getArgument('btHandle');
$app->make('debugbar/time')->startMeasure(sprintf('load_block_%d', $bID), sprintf('Render %s block (bID: %d)', $btHandle, $bID));
});
$director->addListener('on_block_before_render', static function ($event) use ($app) {
/** @var \Concrete\Core\Block\Block $b */
$b = $event->getBlock();
if ($b) {
$bID = $b->getBlockID();
$btHandle = $b->getBlockTypeHandle();
$app->make('debugbar/time')->startMeasure(sprintf('render_block_%d', $bID), sprintf('Render %s block template (bID: %d)', $btHandle, $bID));
}
});
$director->addListener('on_block_output', static function ($event) use ($app) {
/** @var \Concrete\Core\Block\Block $b */
$b = $event->getBlock();
if ($b) {
$bID = $b->getBlockID();
if ($app->make('debugbar/time')->hasStartedMeasure(sprintf('load_block_%d', $bID))) {
$app->make('debugbar/time')->stopMeasure(sprintf('load_block_%d', $bID), [
'arHandle' => $b->getAreaHandle(),
]);
}
if ($app->make('debugbar/time')->hasStartedMeasure(sprintf('render_block_%d', $bID))) {
$app->make('debugbar/time')->stopMeasure(sprintf('render_block_%d', $bID), [
'template' => $b->getBlockFilename(),
]);
}
}
});
$director->addListener('on_page_output', function ($event) use ($app) {
$debugbarRenderer = $app->make('debugbar/renderer');
$contents = $event->getArgument('contents');
$contents = str_replace(self::PLACEHOLDER_TEXT, $debugbarRenderer->render(), $contents);
$event->setArgument('contents', $contents);
});
}
}
/**
* Register autoloader.
*/
protected function registerAutoload(): void
{
// Check if the composer dependencies are installed
$pkgDir = $this->getPackagePath();
if (!file_exists($pkgDir . '/vendor/autoload.php')) {
throw new UserMessageException(t('Required libraries not found. Please run `cd "%s" && composer install`', $pkgDir));
}
require $pkgDir . '/vendor/autoload.php';
}
}