From 18a317146b8873ae63cba1a890d4456e26ff917c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 14 Dec 2010 15:10:35 +0100 Subject: [PATCH] added an optimizer to remove the raw filter --- CHANGELOG | 4 ++-- lib/Twig/NodeVisitor/Optimizer.php | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 50fecfd..50efc38 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -23,10 +23,10 @@ Changes: * added the autoescape option to enable/disable autoescaping * removed the newline after a comment (mimicks PHP behavior) * added a syntax error exception when parent block is used on a template that does not extend another one - * made the Escaper and Optimizer extensions enabled by default + * made the Escaper extension enabled by default * fixed sandbox extension when used with auto output escaping * fixed escaper when wrapping a Twig_Node_Print (the original class must be preserved) - * added an Optimizer extension + * added an Optimizer extension (enabled by default; optimizes "for" loops and "raw" filters) * added priority to node visitors * 0.9.9 (2010-11-28) diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index 8dbaa08..c149747 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -22,9 +22,10 @@ */ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface { - const OPTIMIZE_ALL = -1; - const OPTIMIZE_NONE = 0; - const OPTIMIZE_FOR = 2; + const OPTIMIZE_ALL = -1; + const OPTIMIZE_NONE = 0; + const OPTIMIZE_FOR = 2; + const OPTIMIZE_RAW_FILTER = 4; protected $loops = array(); protected $optimizers; @@ -66,6 +67,25 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface $this->leaveOptimizeFor($node, $env); } + if (self::OPTIMIZE_RAW_FILTER === (self::OPTIMIZE_RAW_FILTER & $this->optimizers)) { + $node = $this->optimizeRawFilter($node, $env); + } + + return $node; + } + + /** + * Removes "raw" filters. + * + * @param Twig_NodeInterface $node A Node + * @param Twig_Environment $env The current Twig environment + */ + protected function optimizeRawFilter($node, $env) + { + if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) { + return $node->getNode('node'); + } + return $node; } -- 1.7.2.5