forked from friends-of-presta/developer_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeveloper_tools.php
executable file
·195 lines (165 loc) · 5.34 KB
/
developer_tools.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
<?php
/**
* @author Mickaël Andrieu <[email protected]>
*
* This module provides tools for PrestaShop developers
*/
use PrestaShopBundle\Form\Admin\Type\SwitchType;
require_once 'vendor/autoload.php';
class Developer_Tools extends Module
{
const HOOKS_DISPLAY = 'DEV_TOOLS_HOOKS_DISPLAY';
const PROFILER = 'DEV_TOOLS_PROFILER';
/**
* @var array the full list of registered hooks.
*/
private $hooks;
/**
* In constructor we define our module's meta data.
* It's better tot keep constructor (and main module class itself) as thin as possible
* and do any processing in controller.
*/
public function __construct()
{
$this->name = 'developer_tools';
$this->version = '2.0.0';
$this->author = 'Mickaël Andrieu';
$this->displayName = 'Developer tools';
$this->description = 'Help the developer creates modules and themes.';
$this->ps_versions_compliancy = [
'min' => '1.7.5.0',
'max' => _PS_VERSION_,
];
$sql = new DbQuery();
$sql->select('name')->from('hook', 'h');
$this->hooks = Db::getInstance()->executeS($sql);
parent::__construct();
}
/**
* {@inheritdoc}
*/
public function install()
{
Configuration::updateValue(self::HOOKS_DISPLAY, false);
Configuration::updateValue(self::PROFILER, false);
$installStatus = parent::install() &&
$this->registerHook('actionPerformancePageForm') &&
$this->registerHook('actionPerformancePageSave')
;
$this->registerHooks($this->hooks);
return $installStatus;
}
/**
* {@inheritdoc}
*/
public function uninstall()
{
Configuration::deleteByName(self::HOOKS_DISPLAY);
Configuration::deleteByName(self::PROFILER);
return parent::uninstall();
}
/**
* Helper function to register a list of hooks.
*
* @param array $hooks
*/
private function registerHooks(array $hooks)
{
foreach($hooks as $hook) {
$this->registerHook($hook);
}
}
/**
* Add the required styles and javascript for the Hook Display.
*
* @param $arguments
* @return string
*/
public function hookDisplayHeader($arguments)
{
if ($this->isHooksDisplayEnabled()) {
$this->context->controller->registerStylesheet(
'developer-tools-style',
'modules/'.$this->name.'/public/css/hook.css',
[
'media' => 'all',
'priority' => 1000,
]
);
$this->context->controller->registerJavascript(
'developer-tools-javascript',
'modules/'.$this->name.'/public/js/hook.js',
[
'position' => 'bottom',
'priority' => 1000,
]
);
}
return $this->__call('actionFrontControllerSetMedia', $arguments);
}
/**
* Enables the displaying of every hook of display.
*
* @param array $arguments
* @return string
*/
public function hookActionAdminControllerSetMedia($arguments)
{
$this->context->controller->addCSS($this->_path.'public/css/hook.css');
$this->context->controller->addJS($this->_path.'public/js/hook.js');
return $this->__call('actionAdminControllerSetMedia', $arguments);
}
/**
* Display the new options under Performances Page
*/
public function hookActionPerformancePageForm(&$hookParams)
{
$formBuilder = $hookParams['form_builder'];
$optionalFeatures = $formBuilder->get('optional_features');
$optionalFeatures->add(
'hooks_display',
SwitchType::class,
[
'label' => 'Display available hooks?',
'data' => $this->isHooksDisplayEnabled(),
]
);
$optionalFeatures->add(
'profiler',
SwitchType::class,
[
'label' => 'Display the Front Profiler',
'data' => $this->isProfilerEnabled(),
]
);
}
public function hookActionPerformancePageSave($hookParams)
{
$hooksDisplayFeatureEnabled = $hookParams['form_data']['optional_features']['hooks_display'];
$profilerFeatureEnabled = $hookParams['form_data']['optional_features']['profiler'];
Configuration::updateValue(self::HOOKS_DISPLAY, (bool) $hooksDisplayFeatureEnabled);
Configuration::updateValue(self::PROFILER, (bool) $profilerFeatureEnabled);
}
/**
* @param string $hookName
* @param array $hookArguments
* @return string
*/
public function __call($hookName, $hookArguments) {
if ($hookName === 'hookDisplayOverrideTemplate' || strpos($hookName, 'filter') !== false) {
return '';
}
if ($this->isHooksDisplayEnabled()) {
$this->context->smarty->assign('name', $hookName);
return $this->display(__FILE__ , 'views/hook.tpl');
}
}
private function isHooksDisplayEnabled()
{
return (bool) Configuration::get(self::HOOKS_DISPLAY);
}
private function isProfilerEnabled()
{
return (bool) Configuration::get(self::PROFILER);
}
}