|
1
|
|
|
<?php |
|
2
|
|
|
namespace GraphQL\Language; |
|
3
|
|
|
|
|
4
|
|
|
use GraphQL\Language\AST\Node; |
|
5
|
|
|
use GraphQL\Language\AST\NodeKind; |
|
6
|
|
|
use GraphQL\Language\AST\NodeList; |
|
7
|
|
|
use GraphQL\Utils\TypeInfo; |
|
8
|
|
|
|
|
9
|
|
|
class VisitorOperation |
|
10
|
|
|
{ |
|
11
|
|
|
public $doBreak; |
|
12
|
|
|
|
|
13
|
|
|
public $doContinue; |
|
14
|
|
|
|
|
15
|
|
|
public $removeNode; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Utility for efficient AST traversal and modification. |
|
20
|
|
|
* |
|
21
|
|
|
* `visit()` will walk through an AST using a depth first traversal, calling |
|
22
|
|
|
* the visitor's enter function at each node in the traversal, and calling the |
|
23
|
|
|
* leave function after visiting that node and all of it's child nodes. |
|
24
|
|
|
* |
|
25
|
|
|
* By returning different values from the enter and leave functions, the |
|
26
|
|
|
* behavior of the visitor can be altered, including skipping over a sub-tree of |
|
27
|
|
|
* the AST (by returning false), editing the AST by returning a value or null |
|
28
|
|
|
* to remove the value, or to stop the whole traversal by returning BREAK. |
|
29
|
|
|
* |
|
30
|
|
|
* When using `visit()` to edit an AST, the original AST will not be modified, and |
|
31
|
|
|
* a new version of the AST with the changes applied will be returned from the |
|
32
|
|
|
* visit function. |
|
33
|
|
|
* |
|
34
|
|
|
* $editedAST = Visitor::visit($ast, [ |
|
35
|
|
|
* 'enter' => function ($node, $key, $parent, $path, $ancestors) { |
|
36
|
|
|
* // return |
|
37
|
|
|
* // null: no action |
|
38
|
|
|
* // Visitor::skipNode(): skip visiting this node |
|
39
|
|
|
* // Visitor::stop(): stop visiting altogether |
|
40
|
|
|
* // Visitor::removeNode(): delete this node |
|
41
|
|
|
* // any value: replace this node with the returned value |
|
42
|
|
|
* }, |
|
43
|
|
|
* 'leave' => function ($node, $key, $parent, $path, $ancestors) { |
|
44
|
|
|
* // return |
|
45
|
|
|
* // null: no action |
|
46
|
|
|
* // Visitor::stop(): stop visiting altogether |
|
47
|
|
|
* // Visitor::removeNode(): delete this node |
|
48
|
|
|
* // any value: replace this node with the returned value |
|
49
|
|
|
* } |
|
50
|
|
|
* ]); |
|
51
|
|
|
* |
|
52
|
|
|
* Alternatively to providing enter() and leave() functions, a visitor can |
|
53
|
|
|
* instead provide functions named the same as the [kinds of AST nodes](reference.md#graphqllanguageastnodekind), |
|
54
|
|
|
* or enter/leave visitors at a named key, leading to four permutations of |
|
55
|
|
|
* visitor API: |
|
56
|
|
|
* |
|
57
|
|
|
* 1) Named visitors triggered when entering a node a specific kind. |
|
58
|
|
|
* |
|
59
|
|
|
* Visitor::visit($ast, [ |
|
60
|
|
|
* 'Kind' => function ($node) { |
|
61
|
|
|
* // enter the "Kind" node |
|
62
|
|
|
* } |
|
63
|
|
|
* ]); |
|
64
|
|
|
* |
|
65
|
|
|
* 2) Named visitors that trigger upon entering and leaving a node of |
|
66
|
|
|
* a specific kind. |
|
67
|
|
|
* |
|
68
|
|
|
* Visitor::visit($ast, [ |
|
69
|
|
|
* 'Kind' => [ |
|
70
|
|
|
* 'enter' => function ($node) { |
|
71
|
|
|
* // enter the "Kind" node |
|
72
|
|
|
* } |
|
73
|
|
|
* 'leave' => function ($node) { |
|
74
|
|
|
* // leave the "Kind" node |
|
75
|
|
|
* } |
|
76
|
|
|
* ] |
|
77
|
|
|
* ]); |
|
78
|
|
|
* |
|
79
|
|
|
* 3) Generic visitors that trigger upon entering and leaving any node. |
|
80
|
|
|
* |
|
81
|
|
|
* Visitor::visit($ast, [ |
|
82
|
|
|
* 'enter' => function ($node) { |
|
83
|
|
|
* // enter any node |
|
84
|
|
|
* }, |
|
85
|
|
|
* 'leave' => function ($node) { |
|
86
|
|
|
* // leave any node |
|
87
|
|
|
* } |
|
88
|
|
|
* ]); |
|
89
|
|
|
* |
|
90
|
|
|
* 4) Parallel visitors for entering and leaving nodes of a specific kind. |
|
91
|
|
|
* |
|
92
|
|
|
* Visitor::visit($ast, [ |
|
93
|
|
|
* 'enter' => [ |
|
94
|
|
|
* 'Kind' => function($node) { |
|
95
|
|
|
* // enter the "Kind" node |
|
96
|
|
|
* } |
|
97
|
|
|
* }, |
|
98
|
|
|
* 'leave' => [ |
|
99
|
|
|
* 'Kind' => function ($node) { |
|
100
|
|
|
* // leave the "Kind" node |
|
101
|
|
|
* } |
|
102
|
|
|
* ] |
|
103
|
|
|
* ]); |
|
104
|
|
|
*/ |
|
105
|
|
|
class Visitor |
|
106
|
|
|
{ |
|
107
|
|
|
public static $visitorKeys = [ |
|
108
|
|
|
NodeKind::NAME => [], |
|
109
|
|
|
NodeKind::DOCUMENT => ['definitions'], |
|
110
|
|
|
NodeKind::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], |
|
111
|
|
|
NodeKind::VARIABLE_DEFINITION => ['variable', 'type', 'defaultValue'], |
|
112
|
|
|
NodeKind::VARIABLE => ['name'], |
|
113
|
|
|
NodeKind::SELECTION_SET => ['selections'], |
|
114
|
|
|
NodeKind::FIELD => ['alias', 'name', 'arguments', 'directives', 'selectionSet'], |
|
115
|
|
|
NodeKind::ARGUMENT => ['name', 'value'], |
|
116
|
|
|
NodeKind::FRAGMENT_SPREAD => ['name', 'directives'], |
|
117
|
|
|
NodeKind::INLINE_FRAGMENT => ['typeCondition', 'directives', 'selectionSet'], |
|
118
|
|
|
NodeKind::FRAGMENT_DEFINITION => [ |
|
119
|
|
|
'name', |
|
120
|
|
|
// Note: fragment variable definitions are experimental and may be changed |
|
121
|
|
|
// or removed in the future. |
|
122
|
|
|
'variableDefinitions', |
|
123
|
|
|
'typeCondition', |
|
124
|
|
|
'directives', |
|
125
|
|
|
'selectionSet' |
|
126
|
|
|
], |
|
127
|
|
|
|
|
128
|
|
|
NodeKind::INT => [], |
|
129
|
|
|
NodeKind::FLOAT => [], |
|
130
|
|
|
NodeKind::STRING => [], |
|
131
|
|
|
NodeKind::BOOLEAN => [], |
|
132
|
|
|
NodeKind::NULL => [], |
|
133
|
|
|
NodeKind::ENUM => [], |
|
134
|
|
|
NodeKind::LST => ['values'], |
|
135
|
|
|
NodeKind::OBJECT => ['fields'], |
|
136
|
|
|
NodeKind::OBJECT_FIELD => ['name', 'value'], |
|
137
|
|
|
NodeKind::DIRECTIVE => ['name', 'arguments'], |
|
138
|
|
|
NodeKind::NAMED_TYPE => ['name'], |
|
139
|
|
|
NodeKind::LIST_TYPE => ['type'], |
|
140
|
|
|
NodeKind::NON_NULL_TYPE => ['type'], |
|
141
|
|
|
|
|
142
|
|
|
NodeKind::SCHEMA_DEFINITION => ['directives', 'operationTypes'], |
|
143
|
|
|
NodeKind::OPERATION_TYPE_DEFINITION => ['type'], |
|
144
|
|
|
NodeKind::SCALAR_TYPE_DEFINITION => ['description', 'name', 'directives'], |
|
145
|
|
|
NodeKind::OBJECT_TYPE_DEFINITION => ['description', 'name', 'interfaces', 'directives', 'fields'], |
|
146
|
|
|
NodeKind::FIELD_DEFINITION => ['description', 'name', 'arguments', 'type', 'directives'], |
|
147
|
|
|
NodeKind::INPUT_VALUE_DEFINITION => ['description', 'name', 'type', 'defaultValue', 'directives'], |
|
148
|
|
|
NodeKind::INTERFACE_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'], |
|
149
|
|
|
NodeKind::UNION_TYPE_DEFINITION => ['description', 'name', 'directives', 'types'], |
|
150
|
|
|
NodeKind::ENUM_TYPE_DEFINITION => ['description', 'name', 'directives', 'values'], |
|
151
|
|
|
NodeKind::ENUM_VALUE_DEFINITION => ['description', 'name', 'directives'], |
|
152
|
|
|
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => ['description', 'name', 'directives', 'fields'], |
|
153
|
|
|
|
|
154
|
|
|
NodeKind::SCALAR_TYPE_EXTENSION => ['name', 'directives'], |
|
155
|
|
|
NodeKind::OBJECT_TYPE_EXTENSION => ['name', 'interfaces', 'directives', 'fields'], |
|
156
|
|
|
NodeKind::INTERFACE_TYPE_EXTENSION => ['name', 'directives', 'fields'], |
|
157
|
|
|
NodeKind::UNION_TYPE_EXTENSION => ['name', 'directives', 'types'], |
|
158
|
|
|
NodeKind::ENUM_TYPE_EXTENSION => ['name', 'directives', 'values'], |
|
159
|
|
|
NodeKind::INPUT_OBJECT_TYPE_EXTENSION => ['name', 'directives', 'fields'], |
|
160
|
|
|
|
|
161
|
|
|
NodeKind::DIRECTIVE_DEFINITION => ['description', 'name', 'arguments', 'locations'] |
|
162
|
|
|
]; |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Visit the AST (see class description for details) |
|
166
|
|
|
* |
|
167
|
|
|
* @api |
|
168
|
|
|
* @param Node $root |
|
169
|
|
|
* @param array $visitor |
|
170
|
|
|
* @param array $keyMap |
|
171
|
|
|
* @return Node|mixed |
|
172
|
|
|
* @throws \Exception |
|
173
|
|
|
*/ |
|
174
|
563 |
|
public static function visit($root, $visitor, $keyMap = null) |
|
175
|
|
|
{ |
|
176
|
563 |
|
$visitorKeys = $keyMap ?: self::$visitorKeys; |
|
177
|
|
|
|
|
178
|
563 |
|
$stack = null; |
|
179
|
563 |
|
$inArray = $root instanceof NodeList || is_array($root); |
|
|
|
|
|
|
180
|
563 |
|
$keys = [$root]; |
|
181
|
563 |
|
$index = -1; |
|
182
|
563 |
|
$edits = []; |
|
183
|
563 |
|
$parent = null; |
|
184
|
563 |
|
$path = []; |
|
185
|
563 |
|
$ancestors = []; |
|
186
|
563 |
|
$newRoot = $root; |
|
187
|
|
|
|
|
188
|
563 |
|
$UNDEFINED = null; |
|
189
|
|
|
|
|
190
|
|
|
do { |
|
191
|
563 |
|
$index++; |
|
192
|
563 |
|
$isLeaving = $index === count($keys); |
|
193
|
563 |
|
$key = null; |
|
194
|
563 |
|
$node = null; |
|
195
|
563 |
|
$isEdited = $isLeaving && count($edits) !== 0; |
|
196
|
|
|
|
|
197
|
563 |
|
if ($isLeaving) { |
|
198
|
561 |
|
$key = !$ancestors ? $UNDEFINED : $path[count($path) - 1]; |
|
199
|
561 |
|
$node = $parent; |
|
200
|
561 |
|
$parent = array_pop($ancestors); |
|
201
|
|
|
|
|
202
|
561 |
|
if ($isEdited) { |
|
203
|
84 |
|
if ($inArray) { |
|
204
|
|
|
// $node = $node; // arrays are value types in PHP |
|
205
|
76 |
|
if ($node instanceof NodeList) { |
|
206
|
76 |
|
$node = clone $node; |
|
207
|
|
|
} |
|
208
|
|
|
} else { |
|
209
|
84 |
|
$node = clone $node; |
|
210
|
|
|
} |
|
211
|
84 |
|
$editOffset = 0; |
|
212
|
84 |
|
for ($ii = 0; $ii < count($edits); $ii++) { |
|
|
|
|
|
|
213
|
84 |
|
$editKey = $edits[$ii][0]; |
|
214
|
84 |
|
$editValue = $edits[$ii][1]; |
|
215
|
|
|
|
|
216
|
84 |
|
if ($inArray) { |
|
217
|
76 |
|
$editKey -= $editOffset; |
|
218
|
|
|
} |
|
219
|
84 |
|
if ($inArray && $editValue === null) { |
|
220
|
4 |
|
if ($node instanceof NodeList) { |
|
221
|
4 |
|
$node->splice($editKey, 1); |
|
222
|
|
|
} else { |
|
223
|
|
|
array_splice($node, $editKey, 1); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
4 |
|
$editOffset++; |
|
226
|
|
|
} else { |
|
227
|
84 |
|
if ($node instanceof NodeList || is_array($node)) { |
|
228
|
76 |
|
$node[$editKey] = $editValue; |
|
229
|
|
|
} else { |
|
230
|
84 |
|
$node->{$editKey} = $editValue; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
561 |
|
$index = $stack['index']; |
|
236
|
561 |
|
$keys = $stack['keys']; |
|
237
|
561 |
|
$edits = $stack['edits']; |
|
238
|
561 |
|
$inArray = $stack['inArray']; |
|
239
|
561 |
|
$stack = $stack['prev']; |
|
240
|
|
|
} else { |
|
241
|
563 |
|
$key = $parent ? ($inArray ? $index : $keys[$index]) : $UNDEFINED; |
|
242
|
563 |
|
$node = $parent ? (($parent instanceof NodeList || is_array($parent)) ? $parent[$key] : $parent->{$key}) : $newRoot; |
|
243
|
563 |
|
if ($node === null || $node === $UNDEFINED) { |
|
244
|
544 |
|
continue; |
|
245
|
|
|
} |
|
246
|
563 |
|
if ($parent) { |
|
247
|
548 |
|
$path[] = $key; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
563 |
|
$result = null; |
|
252
|
563 |
|
if (!$node instanceof NodeList && !is_array($node)) { |
|
253
|
563 |
|
if (!($node instanceof Node)) { |
|
254
|
2 |
|
throw new \Exception('Invalid AST Node: ' . json_encode($node)); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
561 |
|
$visitFn = self::getVisitFn($visitor, $node->kind, $isLeaving); |
|
|
|
|
|
|
258
|
|
|
|
|
259
|
561 |
|
if ($visitFn) { |
|
260
|
561 |
|
$result = call_user_func($visitFn, $node, $key, $parent, $path, $ancestors); |
|
|
|
|
|
|
261
|
|
|
|
|
262
|
561 |
|
if ($result !== null) { |
|
263
|
149 |
|
if ($result instanceof VisitorOperation) { |
|
264
|
7 |
|
if ($result->doBreak) { |
|
265
|
2 |
|
break; |
|
266
|
|
|
} |
|
267
|
5 |
|
if (!$isLeaving && $result->doContinue) { |
|
268
|
1 |
|
array_pop($path); |
|
269
|
1 |
|
continue; |
|
270
|
|
|
} |
|
271
|
4 |
|
if ($result->removeNode) { |
|
272
|
4 |
|
$editValue = null; |
|
273
|
|
|
} |
|
274
|
|
|
} else { |
|
275
|
142 |
|
$editValue = $result; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
146 |
|
$edits[] = [$key, $editValue]; |
|
|
|
|
|
|
279
|
146 |
|
if (!$isLeaving) { |
|
280
|
64 |
|
if ($editValue instanceof Node) { |
|
281
|
4 |
|
$node = $editValue; |
|
282
|
|
|
} else { |
|
283
|
60 |
|
array_pop($path); |
|
284
|
60 |
|
continue; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
561 |
|
if ($result === null && $isEdited) { |
|
292
|
76 |
|
$edits[] = [$key, $node]; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
561 |
|
if ($isLeaving) { |
|
296
|
561 |
|
array_pop($path); |
|
297
|
|
|
} else { |
|
298
|
|
|
$stack = [ |
|
299
|
561 |
|
'inArray' => $inArray, |
|
300
|
561 |
|
'index' => $index, |
|
301
|
561 |
|
'keys' => $keys, |
|
302
|
561 |
|
'edits' => $edits, |
|
303
|
561 |
|
'prev' => $stack |
|
304
|
|
|
]; |
|
305
|
561 |
|
$inArray = $node instanceof NodeList || is_array($node); |
|
306
|
|
|
|
|
307
|
561 |
|
$keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: []; |
|
|
|
|
|
|
308
|
561 |
|
$index = -1; |
|
309
|
561 |
|
$edits = []; |
|
310
|
561 |
|
if ($parent) { |
|
311
|
548 |
|
$ancestors[] = $parent; |
|
312
|
|
|
} |
|
313
|
561 |
|
$parent = $node; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
561 |
|
} while ($stack); |
|
317
|
|
|
|
|
318
|
561 |
|
if (count($edits) !== 0) { |
|
319
|
146 |
|
$newRoot = $edits[0][1]; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
561 |
|
return $newRoot; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Returns marker for visitor break |
|
327
|
|
|
* |
|
328
|
|
|
* @api |
|
329
|
|
|
* @return VisitorOperation |
|
330
|
|
|
*/ |
|
331
|
6 |
|
public static function stop() |
|
332
|
|
|
{ |
|
333
|
6 |
|
$r = new VisitorOperation(); |
|
334
|
6 |
|
$r->doBreak = true; |
|
335
|
6 |
|
return $r; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Returns marker for skipping current node |
|
340
|
|
|
* |
|
341
|
|
|
* @api |
|
342
|
|
|
* @return VisitorOperation |
|
343
|
|
|
*/ |
|
344
|
176 |
|
public static function skipNode() |
|
345
|
|
|
{ |
|
346
|
176 |
|
$r = new VisitorOperation(); |
|
347
|
176 |
|
$r->doContinue = true; |
|
348
|
176 |
|
return $r; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Returns marker for removing a node |
|
353
|
|
|
* |
|
354
|
|
|
* @api |
|
355
|
|
|
* @return VisitorOperation |
|
356
|
|
|
*/ |
|
357
|
4 |
|
public static function removeNode() |
|
358
|
|
|
{ |
|
359
|
4 |
|
$r = new VisitorOperation(); |
|
360
|
4 |
|
$r->removeNode = true; |
|
361
|
4 |
|
return $r; |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* @param $visitors |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
519 |
|
static function visitInParallel($visitors) |
|
|
|
|
|
|
369
|
|
|
{ |
|
370
|
519 |
|
$visitorsCount = count($visitors); |
|
371
|
519 |
|
$skipping = new \SplFixedArray($visitorsCount); |
|
372
|
|
|
|
|
373
|
|
|
return [ |
|
374
|
|
|
'enter' => function ($node) use ($visitors, $skipping, $visitorsCount) { |
|
375
|
519 |
|
for ($i = 0; $i < $visitorsCount; $i++) { |
|
376
|
519 |
|
if (empty($skipping[$i])) { |
|
377
|
519 |
|
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ false); |
|
|
|
|
|
|
378
|
|
|
|
|
379
|
519 |
|
if ($fn) { |
|
380
|
459 |
|
$result = call_user_func_array($fn, func_get_args()); |
|
|
|
|
|
|
381
|
|
|
|
|
382
|
459 |
|
if ($result instanceof VisitorOperation) { |
|
383
|
177 |
|
if ($result->doContinue) { |
|
384
|
174 |
|
$skipping[$i] = $node; |
|
385
|
3 |
|
} else if ($result->doBreak) { |
|
386
|
2 |
|
$skipping[$i] = $result; |
|
387
|
1 |
|
} else if ($result->removeNode) { |
|
388
|
177 |
|
return $result; |
|
389
|
|
|
} |
|
390
|
414 |
|
} else if ($result !== null) { |
|
391
|
|
|
return $result; |
|
392
|
|
|
} |
|
393
|
|
|
} |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
519 |
|
}, |
|
397
|
|
|
'leave' => function ($node) use ($visitors, $skipping, $visitorsCount) { |
|
398
|
519 |
|
for ($i = 0; $i < $visitorsCount; $i++) { |
|
399
|
519 |
|
if (empty($skipping[$i])) { |
|
400
|
516 |
|
$fn = self::getVisitFn($visitors[$i], $node->kind, /* isLeaving */ true); |
|
|
|
|
|
|
401
|
|
|
|
|
402
|
516 |
|
if ($fn) { |
|
403
|
235 |
|
$result = call_user_func_array($fn, func_get_args()); |
|
|
|
|
|
|
404
|
235 |
|
if ($result instanceof VisitorOperation) { |
|
405
|
12 |
|
if ($result->doBreak) { |
|
406
|
2 |
|
$skipping[$i] = $result; |
|
407
|
10 |
|
} else if ($result->removeNode) { |
|
408
|
12 |
|
return $result; |
|
409
|
|
|
} |
|
410
|
235 |
|
} else if ($result !== null) { |
|
411
|
516 |
|
return $result; |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
178 |
|
} else if ($skipping[$i] === $node) { |
|
415
|
174 |
|
$skipping[$i] = null; |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
519 |
|
} |
|
419
|
|
|
]; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Creates a new visitor instance which maintains a provided TypeInfo instance |
|
424
|
|
|
* along with visiting visitor. |
|
425
|
|
|
*/ |
|
426
|
515 |
|
static function visitWithTypeInfo(TypeInfo $typeInfo, $visitor) |
|
|
|
|
|
|
427
|
|
|
{ |
|
428
|
|
|
return [ |
|
429
|
|
|
'enter' => function ($node) use ($typeInfo, $visitor) { |
|
430
|
515 |
|
$typeInfo->enter($node); |
|
431
|
515 |
|
$fn = self::getVisitFn($visitor, $node->kind, false); |
|
|
|
|
|
|
432
|
|
|
|
|
433
|
515 |
|
if ($fn) { |
|
|
|
|
|
|
434
|
515 |
|
$result = call_user_func_array($fn, func_get_args()); |
|
435
|
515 |
|
if ($result) { |
|
436
|
1 |
|
$typeInfo->leave($node); |
|
437
|
1 |
|
if ($result instanceof Node) { |
|
438
|
1 |
|
$typeInfo->enter($result); |
|
439
|
|
|
} |
|
440
|
|
|
} |
|
441
|
515 |
|
return $result; |
|
442
|
|
|
} |
|
443
|
150 |
|
return null; |
|
444
|
515 |
|
}, |
|
445
|
515 |
|
'leave' => function ($node) use ($typeInfo, $visitor) { |
|
446
|
515 |
|
$fn = self::getVisitFn($visitor, $node->kind, true); |
|
|
|
|
|
|
447
|
515 |
|
$result = $fn ? call_user_func_array($fn, func_get_args()) : null; |
|
|
|
|
|
|
448
|
515 |
|
$typeInfo->leave($node); |
|
449
|
515 |
|
return $result; |
|
450
|
515 |
|
} |
|
451
|
|
|
]; |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
/** |
|
455
|
|
|
* @param $visitor |
|
456
|
|
|
* @param $kind |
|
457
|
|
|
* @param $isLeaving |
|
458
|
|
|
* @return null |
|
459
|
|
|
*/ |
|
460
|
561 |
|
public static function getVisitFn($visitor, $kind, $isLeaving) |
|
461
|
|
|
{ |
|
462
|
561 |
|
if (!$visitor) { |
|
463
|
118 |
|
return null; |
|
464
|
|
|
} |
|
465
|
561 |
|
$kindVisitor = isset($visitor[$kind]) ? $visitor[$kind] : null; |
|
466
|
|
|
|
|
467
|
561 |
|
if (!$isLeaving && is_callable($kindVisitor)) { |
|
468
|
|
|
// { Kind() {} } |
|
469
|
445 |
|
return $kindVisitor; |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
561 |
|
if (is_array($kindVisitor)) { |
|
473
|
230 |
|
if ($isLeaving) { |
|
474
|
230 |
|
$kindSpecificVisitor = isset($kindVisitor['leave']) ? $kindVisitor['leave'] : null; |
|
475
|
|
|
} else { |
|
476
|
230 |
|
$kindSpecificVisitor = isset($kindVisitor['enter']) ? $kindVisitor['enter'] : null; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
230 |
|
if ($kindSpecificVisitor && is_callable($kindSpecificVisitor)) { |
|
480
|
|
|
// { Kind: { enter() {}, leave() {} } } |
|
481
|
230 |
|
return $kindSpecificVisitor; |
|
482
|
|
|
} |
|
483
|
172 |
|
return null; |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
560 |
|
$visitor += ['leave' => null, 'enter' => null]; |
|
487
|
560 |
|
$specificVisitor = $isLeaving ? $visitor['leave'] : $visitor['enter']; |
|
488
|
|
|
|
|
489
|
560 |
|
if ($specificVisitor) { |
|
490
|
558 |
|
if (is_callable($specificVisitor)) { |
|
491
|
|
|
// { enter() {}, leave() {} } |
|
492
|
532 |
|
return $specificVisitor; |
|
493
|
|
|
} |
|
494
|
81 |
|
$specificKindVisitor = isset($specificVisitor[$kind]) ? $specificVisitor[$kind] : null; |
|
495
|
|
|
|
|
496
|
81 |
|
if (is_callable($specificKindVisitor)) { |
|
497
|
|
|
// { enter: { Kind() {} }, leave: { Kind() {} } } |
|
498
|
81 |
|
return $specificKindVisitor; |
|
499
|
|
|
} |
|
500
|
|
|
} |
|
501
|
534 |
|
return null; |
|
502
|
|
|
} |
|
503
|
|
|
} |
|
504
|
|
|
|