‪TYPO3CMS  ‪main
AstTraverser.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
30 final class ‪AstTraverser
31 {
35  public function ‪traverse(‪RootNode $rootNode, array $visitors): void
36  {
37  foreach ($visitors as $visitor) {
38  if (!$visitor instanceof ‪AstVisitorInterface) {
39  throw new \RuntimeException(
40  'Visitors must implement AstTreeVisitorInterface',
41  1689244842
42  );
43  }
44  }
45  $currentObjectPath = new ‪CurrentObjectPath();
46  $this->‪traverseRecursive($visitors, $rootNode, $rootNode, $currentObjectPath, 0);
47  }
48 
52  private function ‪traverseRecursive(array $visitors, ‪RootNode $nodeRoot, ‪NodeInterface $node, ‪CurrentObjectPath $currentObjectPath, int $currentDepth): void
53  {
54  $currentObjectPath->‪append($node);
55  foreach ($visitors as $visitor) {
56  $visitor->visitBeforeChildren($nodeRoot, $node, $currentObjectPath, $currentDepth);
57  }
58  foreach ($node->‪getNextChild() as $child) {
59  $this->‪traverseRecursive($visitors, $nodeRoot, $child, $currentObjectPath, $currentDepth + 1);
60  foreach ($visitors as $visitor) {
61  $visitor->visit($nodeRoot, $child, $currentObjectPath, $currentDepth);
62  }
63  }
64  foreach ($visitors as $visitor) {
65  $visitor->visitAfterChildren($nodeRoot, $node, $currentObjectPath, $currentDepth);
66  }
67  $currentObjectPath->‪removeLast();
68  }
69 }
‪TYPO3\CMS\Core\TypoScript\AST\Traverser
Definition: AstTraverser.php:18
‪TYPO3\CMS\Core\TypoScript\AST\Node\NodeInterface\getNextChild
‪iterable< ChildNodeInterface > getNextChild()
‪TYPO3\CMS\Core\TypoScript\AST\CurrentObjectPath\CurrentObjectPath\removeLast
‪removeLast()
Definition: CurrentObjectPath.php:95
‪TYPO3\CMS\Core\TypoScript\AST\Node\NodeInterface
Definition: NodeInterface.php:35
‪TYPO3\CMS\Core\TypoScript\AST\Traverser\AstTraverser\traverse
‪traverse(RootNode $rootNode, array $visitors)
Definition: AstTraverser.php:35
‪TYPO3\CMS\Core\TypoScript\AST\Visitor\AstVisitorInterface
Definition: AstVisitorInterface.php:30
‪TYPO3\CMS\Core\TypoScript\AST\CurrentObjectPath\CurrentObjectPath\append
‪append(NodeInterface $node)
Definition: CurrentObjectPath.php:43
‪TYPO3\CMS\Core\TypoScript\AST\CurrentObjectPath\CurrentObjectPath
Definition: CurrentObjectPath.php:32
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Core\TypoScript\AST\Traverser\AstTraverser\traverseRecursive
‪traverseRecursive(array $visitors, RootNode $nodeRoot, NodeInterface $node, CurrentObjectPath $currentObjectPath, int $currentDepth)
Definition: AstTraverser.php:52
‪TYPO3\CMS\Core\TypoScript\AST\Traverser\AstTraverser
Definition: AstTraverser.php:31