ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
NodeTraverser.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
22 {
23  protected $env;
24  protected $visitors = array();
25 
30  public function __construct(Twig_Environment $env, array $visitors = array())
31  {
32  $this->env = $env;
33  foreach ($visitors as $visitor) {
34  $this->addVisitor($visitor);
35  }
36  }
37 
38  public function addVisitor(Twig_NodeVisitorInterface $visitor)
39  {
40  if (!isset($this->visitors[$visitor->getPriority()])) {
41  $this->visitors[$visitor->getPriority()] = array();
42  }
43 
44  $this->visitors[$visitor->getPriority()][] = $visitor;
45  }
46 
52  public function traverse(Twig_NodeInterface $node)
53  {
54  ksort($this->visitors);
55  foreach ($this->visitors as $visitors) {
56  foreach ($visitors as $visitor) {
57  $node = $this->traverseForVisitor($visitor, $node);
58  }
59  }
60 
61  return $node;
62  }
63 
64  protected function traverseForVisitor(Twig_NodeVisitorInterface $visitor, Twig_NodeInterface $node = null)
65  {
66  if (null === $node) {
67  return;
68  }
69 
70  $node = $visitor->enterNode($node, $this->env);
71 
72  foreach ($node as $k => $n) {
73  if (false !== $n = $this->traverseForVisitor($visitor, $n)) {
74  $node->setNode($k, $n);
75  } else {
76  $node->removeNode($k);
77  }
78  }
79 
80  return $visitor->leaveNode($node, $this->env);
81  }
82 }
83 
84 class_alias('Twig_NodeTraverser', 'Twig\NodeTraverser', false);
Represents a node in the AST.
Twig_NodeTraverser is a node traverser.
enterNode(Twig_NodeInterface $node, Twig_Environment $env)
Called before child nodes are visited.
traverseForVisitor(Twig_NodeVisitorInterface $visitor, Twig_NodeInterface $node=null)
getPriority()
Returns the priority for this visitor.
Twig_NodeVisitorInterface is the interface the all node visitor classes must implement.
addVisitor(Twig_NodeVisitorInterface $visitor)
$n
Definition: RandomTest.php:85
traverse(Twig_NodeInterface $node)
Traverses a node and calls the registered visitors.
Stores the Twig configuration.
Definition: Environment.php:17
leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
Called after child nodes are visited.
__construct(Twig_Environment $env, array $visitors=array())