/
MergedArrayTreeVisitor.php
289 lines (247 loc) · 10.8 KB
/
MergedArrayTreeVisitor.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
<?php
declare(strict_types=1);
namespace Neos\Fusion\Core\ObjectTreeParser;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Fusion\Core\ObjectTreeParser\Ast\AssignedObjectPath;
use Neos\Fusion\Core\ObjectTreeParser\Ast\Block;
use Neos\Fusion\Core\ObjectTreeParser\Ast\DslExpressionValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\EelExpressionValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\FusionFile;
use Neos\Fusion\Core\ObjectTreeParser\Ast\FusionObjectValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\IncludeStatement;
use Neos\Fusion\Core\ObjectTreeParser\Ast\MetaPathSegment;
use Neos\Fusion\Core\ObjectTreeParser\Ast\ObjectStatement;
use Neos\Fusion\Core\ObjectTreeParser\Ast\ObjectPath;
use Neos\Fusion\Core\ObjectTreeParser\Ast\PathSegment;
use Neos\Fusion\Core\ObjectTreeParser\Ast\PrototypePathSegment;
use Neos\Fusion\Core\ObjectTreeParser\Ast\FloatValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\IntValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\BoolValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\NullValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\StatementList;
use Neos\Fusion\Core\ObjectTreeParser\Ast\StringValue;
use Neos\Fusion\Core\ObjectTreeParser\Ast\ValueAssignment;
use Neos\Fusion\Core\ObjectTreeParser\Ast\ValueCopy;
use Neos\Fusion\Core\ObjectTreeParser\Ast\ValueUnset;
use Neos\Fusion;
use Neos\Fusion\Core\ObjectTreeParser\Exception\ParserException;
/**
* Builds the merged array tree for the Fusion runtime
*/
class MergedArrayTreeVisitor implements AstNodeVisitorInterface
{
/**
* For nested blocks to determine the prefix
*/
protected array $currentObjectPathStack = [];
protected ?string $contextPathAndFilename;
protected int $currentObjectStatementCursor;
public function __construct(
protected MergedArrayTree $mergedArrayTree,
protected \Closure $handleFileInclude,
protected \Closure $handleDslTranspile
) {
}
public function visitFusionFile(FusionFile $fusionFile): MergedArrayTree
{
$this->contextPathAndFilename = $fusionFile->contextPathAndFileName;
$fusionFile->statementList->visit($this);
return $this->mergedArrayTree;
}
public function visitStatementList(StatementList $statementList)
{
foreach ($statementList->statements as $statement) {
$statement->visit($this);
}
}
public function visitIncludeStatement(IncludeStatement $includeStatement)
{
($this->handleFileInclude)($this->mergedArrayTree, $includeStatement->filePattern, $this->contextPathAndFilename);
}
public function visitObjectStatement(ObjectStatement $objectStatement)
{
$this->currentObjectStatementCursor = $objectStatement->cursor;
$currentPath = $objectStatement->path->visit($this, $this->getCurrentObjectPathPrefix());
$objectStatement->operation?->visit($this, $currentPath);
$objectStatement->block?->visit($this, $currentPath);
}
public function visitBlock(Block $block, array $currentPath = null)
{
$currentPath ?? throw new \BadMethodCallException('$currentPath is required.');
array_push($this->currentObjectPathStack, $currentPath);
$block->statementList->visit($this);
array_pop($this->currentObjectPathStack);
}
public function visitObjectPath(ObjectPath $objectPath, array $objectPathPrefix = []): array
{
$path = $objectPathPrefix;
foreach ($objectPath->segments as $segment) {
$path = [...$path, ...$segment->visit($this)];
}
return $path;
}
public function visitMetaPathSegment(MetaPathSegment $metaPathSegment): array
{
return ['__meta', $metaPathSegment->identifier];
}
public function visitPrototypePathSegment(PrototypePathSegment $prototypePathSegment): array
{
return ['__prototypes', $prototypePathSegment->identifier];
}
public function visitPathSegment(PathSegment $pathSegment): array
{
$key = stripslashes($pathSegment->identifier);
self::validateParseTreeKey($key);
return [$key];
}
public function visitValueAssignment(ValueAssignment $valueAssignment, array $currentPath = null)
{
$currentPath ?? throw new \BadMethodCallException('$currentPath is required.');
$value = $valueAssignment->pathValue->visit($this);
$this->mergedArrayTree->setValueInTree($currentPath, $value);
}
public function visitFusionObjectValue(FusionObjectValue $fusionObjectValue)
{
return [
'__objectType' => $fusionObjectValue->value, '__value' => null, '__eelExpression' => null
];
}
public function visitDslExpressionValue(DslExpressionValue $dslExpressionValue)
{
try {
return ($this->handleDslTranspile)($dslExpressionValue->identifier, $dslExpressionValue->code);
} catch (ParserException $e) {
throw $e;
} catch (\Exception $e) {
// convert all exceptions from dsl transpilation to fusion exception and add file and line info
throw $this->prepareParserException(new ParserException())
->setCode(1180600696)
->setMessage($e->getMessage())
->build();
}
}
public function visitEelExpressionValue(EelExpressionValue $eelExpressionValue)
{
$eelWithoutNewLines = str_replace("\n", '', $eelExpressionValue->value);
return [
'__eelExpression' => $eelWithoutNewLines, '__value' => null, '__objectType' => null
];
}
public function visitFloatValue(FloatValue $floatValue)
{
return $floatValue->value;
}
public function visitIntValue(IntValue $intValue)
{
return $intValue->value;
}
public function visitBoolValue(BoolValue $boolValue)
{
return $boolValue->value;
}
public function visitNullValue(NullValue $nullValue)
{
return null;
}
public function visitStringValue(StringValue $stringValue): string
{
return $stringValue->value;
}
public function visitValueCopy(ValueCopy $valueCopy, array $currentPath = null)
{
$currentPath ?? throw new \BadMethodCallException('$currentPath is required.');
$sourcePath = $valueCopy->assignedObjectPath->visit($this, $this->mergedArrayTree->getParentPath($currentPath));
$currentPathsPrototype = $this->mergedArrayTree->pathIsPrototype($currentPath);
$sourcePathIsPrototype = $this->mergedArrayTree->pathIsPrototype($sourcePath);
if ($currentPathsPrototype && $sourcePathIsPrototype) {
// both are a prototype definition
if (count($currentPath) !== 2 || count($sourcePath) !== 2) {
// one of the path has not a length of 2: this means
// at least one path is nested (f.e. foo.prototype(Bar))
// Currently, it is not supported to override the prototypical inheritance in
// parts of the Fusion rendering tree.
// Although this might work conceptually, it makes reasoning about the prototypical
// inheritance tree a lot more complex; that's why we forbid it right away.
throw $this->prepareParserException(new ParserException())
->setCode(1358418019)
->setMessage('Cannot inherit, when one of the sides is nested (e.g. foo.prototype(Bar)). Setting up prototype inheritance is only supported at the top level: prototype(Foo) < prototype(Bar)')
->build();
}
// it must be of the form "prototype(Foo) < prototype(Bar)"
$currentPath[] = '__prototypeObjectName';
$this->mergedArrayTree->setValueInTree($currentPath, end($sourcePath));
return;
}
if ($currentPathsPrototype xor $sourcePathIsPrototype) {
// Only one of "source" or "target" is a prototype. We do not support copying a
// non-prototype value to a prototype value or vice-versa.
throw $this->prepareParserException(new ParserException())
->setCode(1358418015)
->setMessage("Cannot inherit, when one of the sides is no prototype definition of the form prototype(Foo). It is only allowed to build inheritance chains with prototype objects.")
->build();
}
$this->mergedArrayTree->copyValueInTree($currentPath, $sourcePath);
}
public function visitAssignedObjectPath(AssignedObjectPath $assignedObjectPath, $relativePath = [])
{
$path = [];
if ($assignedObjectPath->isRelative) {
$path = $relativePath;
}
return $assignedObjectPath->objectPath->visit($this, $path);
}
public function visitValueUnset(ValueUnset $valueUnset, array $currentPath = null)
{
$currentPath ?? throw new \BadMethodCallException('$currentPath is required.');
$this->mergedArrayTree->removeValueInTree($currentPath);
}
protected function getCurrentObjectPathPrefix(): array
{
$lastElementOfStack = end($this->currentObjectPathStack);
return ($lastElementOfStack === false) ? [] : $lastElementOfStack;
}
protected function validateParseTreeKey(string $pathKey)
{
if ($pathKey === '') {
throw $this->prepareParserException(new ParserException())
->setCode(1646988838)
->setMessage("A path must not be empty.")
->build();
}
if (str_starts_with($pathKey, '__')
&& in_array($pathKey, Fusion\Core\Parser::$reservedParseTreeKeys, true)) {
throw $this->prepareParserException(new ParserException())
->setCode(1437065270)
->setMessage("Reversed key '$pathKey' used.")
->build();
}
if (str_contains($pathKey, "\n")) {
$cleaned = str_replace("\n", '', $pathKey);
throw $this->prepareParserException(new ParserException())
->setCode(1644068086)
->setMessage("Key '$cleaned' cannot contain newlines.")
->build();
}
}
protected function prepareParserException(ParserException $parserException): ParserException
{
if ($this->contextPathAndFilename === null) {
$fusionCode = '';
} else {
$fusionCode = file_get_contents($this->contextPathAndFilename);
}
return $parserException
->setHideColumnInformation()
->setFile($this->contextPathAndFilename)
->setFusion($fusionCode)
->setCursor($this->currentObjectStatementCursor);
}
}