forked from Oksydan/is_themecore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_themecore.php
207 lines (177 loc) · 5.47 KB
/
is_themecore.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
<?php
declare(strict_types=1);
if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
require_once dirname(__FILE__) . '/vendor/autoload.php';
}
use Oksydan\Module\IsThemeCore\Core\Partytown\FilesInstallation;
use Oksydan\Module\IsThemeCore\Form\Settings\GeneralConfiguration;
use Oksydan\Module\IsThemeCore\Form\Settings\WebpConfiguration;
use Oksydan\Module\IsThemeCore\HookDispatcher;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class Is_themecore extends Module
{
protected $hookDispatcher;
/**
* @var array<string, string> Configuration values
*/
public const CONFIGURATION_VALUES = [
GeneralConfiguration::THEMECORE_DISPLAY_LIST => 'grid',
GeneralConfiguration::THEMECORE_EARLY_HINTS => false,
GeneralConfiguration::THEMECORE_PRELOAD_CSS => false,
GeneralConfiguration::THEMECORE_LOAD_PARTY_TOWN => false,
GeneralConfiguration::THEMECORE_DEBUG_PARTY_TOWN => false,
WebpConfiguration::THEMECORE_WEBP_ENABLED => false,
WebpConfiguration::THEMECORE_WEBP_QUALITY => 90,
WebpConfiguration::THEMECORE_WEBP_CONVERTER => null,
WebpConfiguration::THEMECORE_WEBP_SHARPYUV => false,
];
/**
* @var string[] Hooks to register
*/
public const HOOKS = [
'actionOutputHTMLBefore',
'actionDispatcherBefore',
'actionFrontControllerSetMedia',
'displayListingStructuredData',
'displayHeader',
'actionProductSearchAfter',
'actionHtaccessCreate',
'objectShopUrlAddAfter',
'objectShopUrlUpdateAfter',
'objectShopUrlDeleteAfter',
];
/**
* @var Configuration<string, mixed> Configuration
*/
private $configuration;
public function __construct()
{
$this->name = 'is_themecore';
$this->tab = 'others';
$this->version = '4.1.1';
$this->author = 'Igor Stępień';
$this->ps_versions_compliancy = ['min' => '8.0.0', 'max' => _PS_VERSION_];
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans(
'Theme core module',
[],
'Modules.isthemecore.Admin'
);
$this->description = $this->trans(
'Required for theme to work.',
[],
'Modules.isthemecore.Admin'
);
$this->tabs = [
[
'name' => 'Theme core module settings',
'class_name' => 'themecoreSettings',
'route_name' => 'is_themecore_module_settings',
'parent_class_name' => 'CONFIGURE',
'visible' => false,
'wording' => 'Theme core module settings',
'wording_domain' => 'Modules.isthemecore.Admin',
],
];
$this->configuration = new Configuration();
}
/**
* {@inheritdoc}
*/
public function isUsingNewTranslationSystem(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function install(): bool
{
return parent::install()
&& $this->installConfiguration()
&& $this->installPartytown()
&& $this->registerHook(self::HOOKS)
;
}
/**
* Install configuration values
*/
private function installConfiguration(): bool
{
try {
foreach (self::CONFIGURATION_VALUES as $key => $default_value) {
$this->configuration->set($key, $default_value);
}
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Install Partytown
*/
private function installPartytown(): bool
{
$installer = new FilesInstallation($this); // SERVICES NOT AVAILABLE DURING INSTALLATION
try {
$installer->installFiles();
} catch (Exception $e) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
public function uninstall(): bool
{
return parent::uninstall()
&& $this->uninstallConfiguration()
;
}
/**
* Uninstall configuration values
*/
private function uninstallConfiguration(): bool
{
try {
foreach (array_keys(self::CONFIGURATION_VALUES) as $key) {
$this->configuration->remove($key);
}
} catch (Exception $e) {
return false;
}
return true;
}
/**
* Get module configuration page content
*/
public function getContent(): void
{
$container = SymfonyContainer::getInstance();
if ($container != null) {
/** @var UrlGeneratorInterface */
$router = $container->get('router');
Tools::redirectAdmin($router->generate('is_themecore_module_settings'));
}
}
public function __call(string $methodName, array $arguments)
{
return $this
->getHookDispatcher()
->dispatch($methodName, $arguments[0] ?? []);
}
protected function getHookDispatcher(): HookDispatcher
{
if (!isset($this->hookDispatcher)) {
if (!class_exists(HookDispatcher::class)) {
require_once dirname(__FILE__) . '/vendor/autoload.php';
}
$this->hookDispatcher = new HookDispatcher($this);
}
return $this->hookDispatcher;
}
}