-
Notifications
You must be signed in to change notification settings - Fork 638
/
InheritancesResolverService.php
362 lines (328 loc) · 12.3 KB
/
InheritancesResolverService.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Form\Mvc\Configuration;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\Exception\MissingArrayPathException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Form\Mvc\Configuration\Exception\CycleInheritancesException;
/**
* Resolve declared inheritances within an configuration array
*
* Basic concept:
* - Take a large YAML config and replace the key '__inheritance' by the referenced YAML partial (of the same config file)
* - Maybe also override some keys of the referenced partial
* - Avoid endless loop by reference cycles
*
* e.g.
* ---------------------
*
* Form:
* part1:
* key1: value1
* key2: value2
* key3: value3
* part2:
* __inheritance:
* 10: Form.part1
* key2: another_value
*
* will result in:
* ---------------------
*
* Form:
* part1:
* key1: value1
* key2: value2
* key3: value3
* part2:
* key1: value1
* key2: another_value
* key3: value3
*
* ---------------------
* Scope: frontend / backend
* @internal
*/
class InheritancesResolverService
{
/**
* The operator which is used to declare inheritances
*/
const INHERITANCE_OPERATOR = '__inheritances';
/**
* The reference configuration is used to get untouched values which
* can be merged into the touched configuration.
*
* @var array
*/
protected $referenceConfiguration = [];
/**
* This stack is needed to find cyclically inheritances which are on
* the same nesting level but which do not follow each other directly.
*
* @var array
*/
protected $inheritanceStack = [];
/**
* Needed to buffer a configuration path for cyclically inheritances
* detection while inheritances for this path is ongoing.
*
* @var string
*/
protected $inheritancePathToCkeck = '';
/**
* Returns an instance of this service. Additionally the configuration
* which should be resolved can be passed.
*
* @param array $configuration
* @return InheritancesResolverService
* @internal
*/
public static function create(array $configuration = []): InheritancesResolverService
{
/** @var InheritancesResolverService $inheritancesResolverService */
$inheritancesResolverService = GeneralUtility::makeInstance(ObjectManager::class)
->get(self::class);
$inheritancesResolverService->setReferenceConfiguration($configuration);
return $inheritancesResolverService;
}
/**
* Reset the state of this service.
* Mainly introduced for unit tests.
*
* @return InheritancesResolverService
* @internal
*/
public function reset()
{
$this->referenceConfiguration = [];
$this->inheritanceStack = [];
$this->inheritancePathToCkeck = '';
return $this;
}
/**
* Set the reference configuration which is used to get untouched
* values which can be merged into the touched configuration.
*
* @param array $referenceConfiguration
* @return InheritancesResolverService
*/
public function setReferenceConfiguration(array $referenceConfiguration)
{
$this->referenceConfiguration = $referenceConfiguration;
return $this;
}
/**
* Resolve all inheritances within a configuration.
* After that the configuration array is cleaned from the
* inheritance operator.
*
* @return array
* @internal
*/
public function getResolvedConfiguration(): array
{
$configuration = $this->resolve($this->referenceConfiguration);
$configuration = $this->removeInheritanceOperatorRecursive($configuration);
return $configuration;
}
/**
* Resolve all inheritances within a configuration.
*
* Takes a YAML config mapped to associative array $configuration
* - replace all findings of key '__inheritance' recursively
* - perform a deep search in config by iteration, thus check for endless loop by reference cycle
*
* Return the completed configuration.
*
* @param array $configuration - a mapped YAML configuration (full or partial)
* @param array $pathStack - an identifier for YAML key as array (Form.part1.key => {Form, part1, key})
* @param bool $setInheritancePathToCkeck
* @return array
*/
protected function resolve(
array $configuration,
array $pathStack = [],
bool $setInheritancePathToCkeck = true
): array {
foreach ($configuration as $key => $values) {
//add current key to pathStack
$pathStack[] = $key;
$path = implode('.', $pathStack);
//check endless loop for current path
$this->throwExceptionIfCycleInheritances($path, $path);
//overwrite service property 'inheritancePathToCheck' with current path
if ($setInheritancePathToCkeck) {
$this->inheritancePathToCkeck = $path;
}
//if value of subnode is an array, perform a deep search iteration step
if (is_array($configuration[$key])) {
if (isset($configuration[$key][self::INHERITANCE_OPERATOR])) {
$inheritances = $this->getValueByPath($this->referenceConfiguration, $path . '.' . self::INHERITANCE_OPERATOR);
//and replace the __inheritance operator by the respective partial
if (is_array($inheritances)) {
$inheritedConfigurations = $this->resolveInheritancesRecursive($inheritances);
$configuration[$key] = array_replace_recursive($inheritedConfigurations, $configuration[$key]);
}
//remove the inheritance operator from configuration
unset($configuration[$key][self::INHERITANCE_OPERATOR]);
}
if (!empty($configuration[$key])) {
// resolve subnode of YAML config
$configuration[$key] = $this->resolve($configuration[$key], $pathStack);
}
}
array_pop($pathStack);
}
return $configuration;
}
/**
* Additional helper for the resolve method.
*
* Takes all inheritances (an array of YAML paths), and check them for endless loops
*
* @param array $inheritances
* @return array
* @throws CycleInheritancesException
*/
protected function resolveInheritancesRecursive(array $inheritances): array
{
ksort($inheritances);
$inheritedConfigurations = [];
foreach ($inheritances as $inheritancePath) {
$this->throwExceptionIfCycleInheritances($inheritancePath, $inheritancePath);
$inheritedConfiguration = $this->getValueByPath($this->referenceConfiguration, $inheritancePath);
if (
isset($inheritedConfiguration[self::INHERITANCE_OPERATOR])
&& count($inheritedConfiguration) === 1
) {
if ($this->inheritancePathToCkeck === $inheritancePath) {
throw new CycleInheritancesException(
$this->inheritancePathToCkeck . ' has cycle inheritances',
1474900796
);
}
$inheritedConfiguration = $this->resolveInheritancesRecursive(
$inheritedConfiguration[self::INHERITANCE_OPERATOR]
);
} else {
$pathStack = explode('.', $inheritancePath);
$key = array_pop($pathStack);
$newConfiguration = [
$key => $inheritedConfiguration
];
$inheritedConfiguration = $this->resolve(
$newConfiguration,
$pathStack,
false
);
$inheritedConfiguration = $inheritedConfiguration[$key];
}
if ($inheritedConfiguration === null) {
throw new CycleInheritancesException(
$inheritancePath . ' does not exist within the configuration',
1489260796
);
}
$inheritedConfigurations = array_replace_recursive(
$inheritedConfigurations,
$inheritedConfiguration
);
}
return $inheritedConfigurations;
}
/**
* Throw an exception if a cycle is detected.
*
* @param string $path
* @param string $pathToCheck
* @throws CycleInheritancesException
*/
protected function throwExceptionIfCycleInheritances(string $path, string $pathToCheck)
{
$configuration = $this->getValueByPath($this->referenceConfiguration, $path);
if (isset($configuration[self::INHERITANCE_OPERATOR])) {
$inheritances = $this->getValueByPath($this->referenceConfiguration, $path . '.' . self::INHERITANCE_OPERATOR);
if (is_array($inheritances)) {
foreach ($inheritances as $inheritancePath) {
$configuration = $this->getValueByPath($this->referenceConfiguration, $inheritancePath);
if (isset($configuration[self::INHERITANCE_OPERATOR])) {
$_inheritances = $this->getValueByPath($this->referenceConfiguration, $inheritancePath . '.' . self::INHERITANCE_OPERATOR);
foreach ($_inheritances as $_inheritancePath) {
if (strpos($pathToCheck, $_inheritancePath) === 0) {
throw new CycleInheritancesException(
$pathToCheck . ' has cycle inheritances',
1474900797
);
}
}
}
if (
isset($this->inheritanceStack[$pathToCheck])
&& is_array($this->inheritanceStack[$pathToCheck])
&& in_array($inheritancePath, $this->inheritanceStack[$pathToCheck])
) {
$this->inheritanceStack[$pathToCheck][] = $inheritancePath;
throw new CycleInheritancesException(
$pathToCheck . ' has cycle inheritances',
1474900799
);
}
$this->inheritanceStack[$pathToCheck][] = $inheritancePath;
$this->throwExceptionIfCycleInheritances($inheritancePath, $pathToCheck);
}
$this->inheritanceStack[$pathToCheck] = null;
}
}
}
/**
* Recursively remove self::INHERITANCE_OPERATOR keys
*
* @param array $array
* @return array the modified array
*/
protected function removeInheritanceOperatorRecursive(array $array): array
{
$result = $array;
foreach ($result as $key => $value) {
if ($key === self::INHERITANCE_OPERATOR) {
unset($result[$key]);
continue;
}
if (is_array($value)) {
$result[$key] = $this->removeInheritanceOperatorRecursive($value);
}
}
return $result;
}
/**
* Check the given array representation of a YAML config for the given path and return it's value / sub-array.
* If path is not found, return null;
*
* @param array $config
* @param string $path
* @param string $delimiter
* @return string|array|null
*/
protected function getValueByPath(array $config, string $path, string $delimiter = '.')
{
try {
return ArrayUtility::getValueByPath($config, $path, $delimiter);
} catch (MissingArrayPathException $exception) {
return null;
}
}
}