-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhhmodulesmanager.php
320 lines (292 loc) · 9.05 KB
/
hhmodulesmanager.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file docs/licenses/LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author Hervé HENNES <[email protected]>
* @copyright since 2023 Hervé HENNES
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License ("AFL") v. 3.0
*/
if (!defined('_PS_VERSION_')) {
exit;
}
include dirname(__FILE__) . '/vendor/autoload.php';
use Hhennes\ModulesManager\Change;
use Hhennes\ModulesManager\ConfigForm;
use Hhennes\ModulesManager\Installer;
use Psr\Log\LoggerInterface;
class HhModulesManager extends Module
{
/**
* List of ignored configuration
*/
public const EXCLUDED_CONFIGURATIONS = [
'HHMODULESMANAGER_ENABLE_CHANGE_RECORDER',
'PS_CCCJS_VERSION',
'PS_CCCCSS_VERSION',
];
/** @var LoggerInterface|null */
protected $logger = null;
public function __construct()
{
$this->name = 'hhmodulesmanager';
$this->tab = 'administration';
$this->version = '0.4.1';
$this->author = 'hhennes';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Enhanced modules management');
$this->description = $this->l('Enhanced modules management through cli and CI/CD');
}
/**
* Install Module
*
* @return bool
*/
public function install(): bool
{
$installer = new Installer($this);
return parent::install()
&& $installer->install();
}
/**
* Uninstall Module
*
* @return bool
*/
public function uninstall(): bool
{
$installer = new Installer($this);
return parent::uninstall()
&& $installer->uninstall();
}
/**
* Hook executed after a module install
*
* @param array $params
*
* @return void
*/
public function hookActionModuleInstallAfter(array $params): void
{
/** @var Module $module */
$module = $params['object'];
$this->logEvent('module', 'install', $module->name, ['name' => $module->name]);
}
/**
* Hook executed after a module uninstall
*
* @param array $params
*
* @return void
*/
public function hookActionModuleUnInstallAfter(array $params): void
{
/** @var Module $module */
$module = $params['object'];
$this->logEvent('module', 'uninstall', $module->name, ['name' => $module->name]);
}
/**
* Hook (custom) to listen to module upgrades
*
* @param array $params
*
* @return void
*/
public function hookActionModuleUpgradeVersion(array $params): void
{
$this->logEvent(
'module',
'update',
$params['module'],
['name' => $params['module'], 'version' => $params['version']]
);
}
/**
* Hook (custom) executed BEFORE updating a configuration
*
* So we can check if the configuration have changed or not
*
* @param array $params
*/
public function hookActionConfigurationUpdateValue(array $params): void
{
$configurationKey = $params['configuration']['key'];
$configurationValue = $params['configuration']['values'];
$idShop = $params['configuration']['idShop'];
$idShopGroup = $params['configuration']['idShopGroup'];
if (!$this->isExludedConfiguration($configurationKey)) {
if (($oldValue = Configuration::get($configurationKey, null, $idShopGroup, $idShop)) != $configurationValue) {
$this->logEvent('configuration', 'update', $configurationKey, ['configuration' => $params['configuration'], 'old_value' => $oldValue]);
}
}
}
/**
* Hook (custom) executed BEFORE deleting a configuration
*
* @param array $params
*/
public function hookActionConfigurationDeleteKey(array $params): void
{
if (!$this->isExludedConfiguration($params['key'])) {
$this->logEvent('configuration', 'delete', $params['key'], ['name' => $params['key']]);
}
}
/**
* Hook (custom) executed BEFORE deleting a configuration in a specific context
*
* @param array $params
*/
public function hookActionConfigurationDeleteContextKey(array $params): void
{
if (!$this->isExludedConfiguration($params['key'])) {
$this->logEvent(
'configuration',
'delete',
null,
['name' => $params['key'], 'idShop' => $params['idShop'], 'idShopGroup' => $params['idShopGroup']]
);
}
}
/**
* Hook (custom) in back office, in the module listing page.
*
* Allow to display a warning about the disabling of module update
*
* @param array $params
*
* @return string
*/
public function hookDisplayAdminModulesListHeader(array $params): string
{
if (!Configuration::get('HHMODULESMANAGER_ENABLE_BO_MODULES_UPDATE')) {
return '<div class="alert alert-warning align-content-center">' .
$this->l('Modules upgrades are disabled into the back office on this environnement.') . '<br />' .
$this->l('Please check hhmodulesmanager configuration if you need to update them') .
'</div>';
}
return '';
}
/**
* Define if recording of events is enabled
*
* @return bool
*/
public function isRecorderEnabled(): bool
{
return (bool) Configuration::get(strtoupper($this->name) . '_ENABLE_CHANGE_RECORDER');
}
/**
* Define if configuration is excluded from recording
*
* @param string $key
*
* @return bool
*/
protected function isExludedConfiguration($key): bool
{
$excludedConfigurations = [];
Hook::exec(
'actionHhmodulesmanagerExcludeConfiguration',
['configuration' => &$excludedConfigurations],
null,
true
);
$excludedConfigurations = array_merge($excludedConfigurations, self::EXCLUDED_CONFIGURATIONS);
return in_array($key, $excludedConfigurations);
}
/**
* Log the event change
*
* @param string $name Event name
* @param string $type Event type
* @param string|null $key Event key
* @param array $details Event array of details
*/
protected function logEvent(string $name, string $type, ?string $key = null, array $details = []): void
{
if (!$this->isRecorderEnabled()) {
return;
}
try {
$change = new Change();
$change->entity = $name;
$change->action = $type;
$change->key = $key;
$change->details = json_encode($details);
$change->add();
$message = date('Y-m-d H:i:s') . ' : '
. $name . ' ' . $type . ' ' . json_encode($details) . "\n";
$this->log($message);
} catch (Exception $e) {
echo $e->getMessage();
}
}
/**
* Log data with monolog
*
* @param string $message
* @param int $level
*
* @return void
*/
public function log(string $message, int $level = 3): void
{
try {
$logger = $this->getLogger();
if ($logger) {
$logger->log($level * 100, $message);
} else {
//In some context the logger might not be defined due to missing symfony context
//So we make a fallback
file_put_contents(
_PS_ROOT_DIR_ . '/var/logs/' . $this->name . '/' . $this->name . '.log',
'['.date('Y-m-d H:i:s') . '] - '.$this->name .'.'. $level . ' - ' . $message . "\n",
FILE_APPEND
);
}
} catch (Exception $e) {
file_put_contents(
dirname(__FILE__) . '/logs/exceptions.log',
date('Y-m-d H:i:s') . ' ' . $e->getMessage() . "\n",
FILE_APPEND
);
}
}
/**
* Get logger interface from service
*
* @return LoggerInterface|null|false
*
* @throws Exception
*/
protected function getLogger()
{
if (null === $this->logger) {
$this->logger = $this->get('hhennes.modulesmanager.logger');
}
return $this->logger;
}
/**
* Module configuration
*
* @return string
*
* @throws Exception
*/
public function getContent(): string
{
$html = '';
$configForm = new ConfigForm($this, $this->context);
$html .= $configForm->postProcess();
$html .= $configForm->renderForm();
return $html;
}
}