From 5c5a9d45ea80018ff3d5fd64b769de6bc82c9d39 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 28 Nov 2010 14:17:56 +0100 Subject: [PATCH] added priority to node visitors --- CHANGELOG | 2 ++ lib/Twig/NodeTraverser.php | 19 ++++++++++++++----- lib/Twig/NodeVisitor/Escaper.php | 8 ++++++++ lib/Twig/NodeVisitor/SafeAnalysis.php | 8 ++++++++ lib/Twig/NodeVisitor/Sandbox.php | 8 ++++++++ lib/Twig/NodeVisitorInterface.php | 7 +++++++ 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index df3f065..f19d607 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ * 0.9.10 + * added priority to node visitors + * 0.9.9 (2010-11-28) Backward incompatibilities: diff --git a/lib/Twig/NodeTraverser.php b/lib/Twig/NodeTraverser.php index e2cb7df..1ae31b5 100644 --- a/lib/Twig/NodeTraverser.php +++ b/lib/Twig/NodeTraverser.php @@ -44,7 +44,11 @@ class Twig_NodeTraverser */ public function addVisitor(Twig_NodeVisitorInterface $visitor) { - $this->visitors[] = $visitor; + if (!isset($this->visitors[$visitor->getPriority()])) { + $this->visitors[$visitor->getPriority()] = array(); + } + + $this->visitors[$visitor->getPriority()][] = $visitor; } /** @@ -58,8 +62,11 @@ class Twig_NodeTraverser return null; } - foreach ($this->visitors as $visitor) { - $node = $visitor->enterNode($node, $this->env); + ksort($this->visitors); + foreach ($this->visitors as $visitors) { + foreach ($visitors as $visitor) { + $node = $visitor->enterNode($node, $this->env); + } } foreach ($node as $k => $n) { @@ -70,8 +77,10 @@ class Twig_NodeTraverser } } - foreach ($this->visitors as $visitor) { - $node = $visitor->leaveNode($node, $this->env); + foreach ($this->visitors as $visitors) { + foreach ($visitors as $visitor) { + $node = $visitor->leaveNode($node, $this->env); + } } return $node; diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index 1f998ee..ad0e420 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -149,4 +149,12 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface $args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line))); return new Twig_Node_Expression_Filter($node, $name, $args, $line); } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return 0; + } } diff --git a/lib/Twig/NodeVisitor/SafeAnalysis.php b/lib/Twig/NodeVisitor/SafeAnalysis.php index 3c5b456..b684618 100644 --- a/lib/Twig/NodeVisitor/SafeAnalysis.php +++ b/lib/Twig/NodeVisitor/SafeAnalysis.php @@ -81,4 +81,12 @@ class Twig_NodeVisitor_SafeAnalysis implements Twig_NodeVisitorInterface return array_intersect($a, $b); } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return 0; + } } diff --git a/lib/Twig/NodeVisitor/Sandbox.php b/lib/Twig/NodeVisitor/Sandbox.php index 542b730..7155f58 100644 --- a/lib/Twig/NodeVisitor/Sandbox.php +++ b/lib/Twig/NodeVisitor/Sandbox.php @@ -75,4 +75,12 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface return $node; } + + /** + * {@inheritdoc} + */ + public function getPriority() + { + return 0; + } } diff --git a/lib/Twig/NodeVisitorInterface.php b/lib/Twig/NodeVisitorInterface.php index 21a517e..d6cacc2 100644 --- a/lib/Twig/NodeVisitorInterface.php +++ b/lib/Twig/NodeVisitorInterface.php @@ -36,4 +36,11 @@ interface Twig_NodeVisitorInterface * @param Twig_NodeInterface The modified node */ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env); + + /** + * Returns the priority. + * + * @return $priority The priority + */ + public function getPriority(); } -- 1.7.2.5