/
ToolboxExtension.php
260 lines (204 loc) · 9.42 KB
/
ToolboxExtension.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
<?php
namespace ToolboxBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Yaml\Yaml;
use ToolboxBundle\Manager\ConfigManager;
use ToolboxBundle\Manager\LayoutManagerInterface;
use ToolboxBundle\Resolver\ContextResolver;
class ToolboxExtension extends Extension implements PrependExtensionInterface
{
public function prepend(ContainerBuilder $container): void
{
$hasTheme = false;
$headlessAware = false;
$wysiwygEditor = null;
if ($container->hasExtension('pimcore_tinymce') === true) {
$wysiwygEditor = 'tiny_mce';
}
$coreLoader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loaded = [];
foreach ($container->getExtensionConfig($this->getAlias()) as $toolboxConfigNode) {
if (!empty($toolboxConfigNode['theme']['layout'])) {
$hasTheme = true;
$headlessAware = $toolboxConfigNode['theme']['layout'] === LayoutManagerInterface::TOOLBOX_LAYOUT_HEADLESS;
}
if (($toolboxConfigNode['enabled_core_areas'] ?? null) === null) {
continue;
}
foreach ($toolboxConfigNode['enabled_core_areas'] as $areaName) {
if (in_array($areaName, $loaded, true)) {
continue;
}
$coreLoader->load(sprintf('core_areas/%s_service.yaml', $areaName));
$this->prependConfigToContainer($coreLoader, $container, sprintf('core_areas/%s_config.yaml', $areaName));
$loaded[] = $areaName;
}
}
// add default theme (b4) if not set
if ($hasTheme === false) {
$this->prependConfigToContainer($coreLoader, $container, 'theme/bootstrap4_theme.yaml');
}
$container->setParameter('toolbox.wysiwyg_editor', $wysiwygEditor);
$container->setParameter('toolbox.headless_aware', $headlessAware);
}
public function load(array $configs, ContainerBuilder $container): void
{
$contextAwareConfigs = $this->parseContextConfigs($configs);
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $contextAwareConfigs);
$this->validateToolboxContextConfig($config);
$this->allocateGoogleMapsApiKey($container);
$contextResolver = $config['context_resolver'];
unset($config['context_resolver']);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
$configManagerDefinition = $container->getDefinition(ConfigManager::class);
$configManagerDefinition->addMethodCall('setConfig', [$config]);
$container->setParameter('toolbox.area_brick.dialog_aware_bricks', $this->determinateConfigDialogAwareBricks($config));
//context resolver
$definition = $container->getDefinition(ContextResolver::class);
$definition->setClass($contextResolver);
}
private function determinateConfigDialogAwareBricks(array $config): array
{
$configDialogAwareBricks = [];
foreach ($config['areas'] as $areaId => $areaSection) {
if (isset($areaSection['config_elements']) && count($areaSection['config_elements']) > 0) {
$configDialogAwareBricks[] = $areaId;
}
}
foreach ($config['context'] as $context) {
foreach ($context['areas'] as $areaId => $areaSection) {
if (isset($areaSection['config_elements']) && count($areaSection['config_elements']) > 0) {
$configDialogAwareBricks[] = $areaId;
}
}
}
return array_unique($configDialogAwareBricks);
}
private function allocateGoogleMapsApiKey(ContainerBuilder $container): void
{
$googleBrowserApiKey = null;
$googleSimpleApiKey = null;
$pimcoreGoogleBrowserApiKey = null;
$pimcoreGoogleSimpleApiKey = null;
/** @phpstan-ignore-next-line */
if ($container->hasParameter('pimcore_google_marketing')) {
$pimcoreGoogleMarketingSettings = $container->getParameter('pimcore_google_marketing');
/** @phpstan-ignore-next-line */
$pimcoreGoogleBrowserApiKey = $pimcoreGoogleMarketingSettings['browser_api_key'] ?? null;
/** @phpstan-ignore-next-line */
$pimcoreGoogleSimpleApiKey = $pimcoreGoogleMarketingSettings['simple_api_key'] ?? null;
}
// browser api key
/** @phpstan-ignore-next-line */
if ($container->hasParameter('toolbox_google_service_browser_api_key')) {
$googleBrowserApiKey = $container->getParameter('toolbox_google_service_browser_api_key');
/** @phpstan-ignore-next-line */
} elseif ($pimcoreGoogleBrowserApiKey !== null) {
$googleBrowserApiKey = $pimcoreGoogleBrowserApiKey;
}
//simple api key
/** @phpstan-ignore-next-line */
if ($container->hasParameter('pimcore_system_config.services.google.simpleapikey')) {
$googleSimpleApiKey = $container->getParameter('pimcore_system_config.services.google.simpleapikey');
/** @phpstan-ignore-next-line */
} elseif ($container->hasParameter('toolbox_google_service_simple_api_key')) {
$googleSimpleApiKey = $container->getParameter('toolbox_google_service_simple_api_key');
/** @phpstan-ignore-next-line */
} elseif ($pimcoreGoogleSimpleApiKey !== null) {
$googleSimpleApiKey = $pimcoreGoogleSimpleApiKey;
}
$container->setParameter('toolbox.google_maps.browser_api_key', $googleBrowserApiKey);
$container->setParameter('toolbox.google_maps.simple_api_key', $googleSimpleApiKey);
}
private function validateToolboxContextConfig(array $config): void
{
foreach ($config['context'] as $contextId => $contextConfig) {
//check if theme is same since it's not possible to merge different themes
if ($contextConfig['settings']['merge_with_root'] === true && isset($contextConfig['theme']) && $contextConfig['theme']['layout'] !== $config['theme']['layout']) {
@trigger_error(sprintf(
'toolbox context conflict for "%s": merged context cannot have another theme than "%s", "%s" given.',
$contextId,
$config['theme']['layout'],
$contextConfig['theme']['layout']
), E_USER_ERROR);
}
}
}
private function parseContextConfigs(array $configs): array
{
$data = [];
$rootConfigs = [];
$contextConfigData = [];
$contextMergeCandidates = [];
foreach ($configs as $rootConfig) {
unset($rootConfig['context'], $rootConfig['context_resolver'], $rootConfig['enabled_core_areas']);
$rootConfigs[] = $rootConfig;
}
foreach ($configs as $config) {
if (!isset($config['context'])) {
continue;
}
foreach ($config['context'] as $contextName => $contextConfig) {
$contextMergeCandidates[$contextName] = $contextConfig['settings']['merge_with_root'] ?? false;
}
}
// get context data
foreach ($configs as $config) {
if (!isset($config['context'])) {
continue;
}
foreach ($config['context'] as $contextName => $contextConfig) {
if ($contextMergeCandidates[$contextName] === false) {
continue;
}
$cleanContextConfig = $contextConfig;
unset($cleanContextConfig['settings']);
$contextConfigData[$contextName][] = $cleanContextConfig;
}
}
// get context merge data
foreach ($contextMergeCandidates as $contextName => $merge) {
if ($merge === false) {
continue;
}
foreach ($rootConfigs as $rootConfig) {
$data[] = [
'context' => [
$contextName => $rootConfig
]
];
}
}
// append merge data
foreach ($data as $append) {
$configs[] = $append;
}
// append custom context data
foreach ($contextConfigData as $contextName => $contextConfigs) {
foreach ($contextConfigs as $el) {
$configs[] = [
'context' => [
$contextName => $el
]
];
}
}
return $configs;
}
private function prependConfigToContainer(YamlFileLoader $loader, ContainerBuilder $container, string $configPath): void
{
// @see https://github.com/symfony/symfony/issues/52789
$data = $loader->getLocator()->locate($configPath);
$parsedData = Yaml::parseFile($data, Yaml::PARSE_CONSTANT);
if (!array_key_exists('toolbox', $parsedData)) {
return;
}
$container->prependExtensionConfig('toolbox', $parsedData['toolbox']);
}
}