-
Notifications
You must be signed in to change notification settings - Fork 5
/
controller.php
105 lines (85 loc) · 3.13 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
<?php
namespace Concrete\Package\Concrete5Debugbar;
use Concrete\Core\Package\Package;
use Concrete\Core\Permission\Key\Key;
use Concrete5Debugbar\Debugbar;
class Controller extends Package
{
const PLACEHOLDER_TEXT = '<!-- debugbar:placeholder -->';
protected $pkgHandle = 'concrete5_debugbar';
protected $appVersionRequired = '8.3.0';
protected $pkgVersion = '0.3';
/**
* Returns the translated name of the package.
*
* @return string
*/
public function getPackageName()
{
return t('PHP Debug Bar for concrete5');
}
/**
* Returns the translated package description.
*
* @return string
*/
public function getPackageDescription()
{
return t('Displays a debug bar in the browser with information from php.');
}
/**
* Install process of the package.
*/
public function install()
{
$this->registerAutoload();
if (!class_exists('DebugBar\DebugBar')) {
throw new \Exception(t('Required libraries not found.'));
}
$this->installContentFile('config/permissions.xml');
return parent::install();
}
public function on_start()
{
$permission = Key::getByHandle('show_debug_bar');
if ($permission && $permission->validate()) {
$this->registerAutoload();
$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'];
});
$app->make('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('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()
{
if (file_exists($this->getPackagePath() . '/vendor/autoload.php')) {
require $this->getPackagePath() . '/vendor/autoload.php';
}
}
}