From 846a2c134d3fc368eb6b9d4b270d5c8eac3eb442 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 16 Oct 2011 05:46:09 +0200 Subject: [PATCH] added a Twig_Node_Expression_DefaultFilter to replace the current way of dealing with default filters --- lib/Twig/ExpressionParser.php | 10 +----- lib/Twig/Node/Expression/DefaultFilter.php | 48 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 lib/Twig/Node/Expression/DefaultFilter.php diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index b7ff70d..e218e40 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -320,14 +320,8 @@ class Twig_ExpressionParser $node = new Twig_Node_Expression_Filter($node, $name, $arguments, $token->getLine(), $tag); - // The default filter is intercepted when the filtered value - // is a name (like obj) or an attribute (like obj.attr) - // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} - if ('default' === $token->getValue() && ($node->getNode('node') instanceof Twig_Node_Expression_Name || $node->getNode('node') instanceof Twig_Node_Expression_GetAttr)) { - $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine()); - $default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); - - $node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine()); + if (Twig_Node_Expression_DefaultFilter::isDefaultFilter($node)) { + $node = new Twig_Node_Expression_DefaultFilter($node); } if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '|')) { diff --git a/lib/Twig/Node/Expression/DefaultFilter.php b/lib/Twig/Node/Expression/DefaultFilter.php new file mode 100644 index 0000000..b184f1e --- /dev/null +++ b/lib/Twig/Node/Expression/DefaultFilter.php @@ -0,0 +1,48 @@ +getLine()); + } + + $test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine()); + $default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine()); + + $node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine()); + + parent::__construct(array('node' => $node), array(), $node->getLine()); + } + + public function compile(Twig_Compiler $compiler) + { + $compiler->subcompile($this->getNode('node')); + } + + /** + * Checks whether a node is a default filter that needs to be wrapped with Twig_Node_Expression_DefaultFilter. + * + * The default filter needs to be wrapped with an instance of Twig_Node_Expression_DefaultFilter + * when the filtered value is a name (like obj) or an attribute (like obj.attr). + * + * In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} + * + * @param Twig_NodeInterface $node A Twig_NodeInterface instance + * + * @return Boolean true if the node must be wrapped with a Twig_Node_Expression_DefaultFilter, false otherwise + */ + static public function isDefaultFilter(Twig_NodeInterface $node) + { + return $node instanceof Twig_Node_Expression_Filter && 'default' === $node->getNode('filter')->getAttribute('value') && ($node->getNode('node') instanceof Twig_Node_Expression_Name || $node->getNode('node') instanceof Twig_Node_Expression_GetAttr); + } +} -- 1.7.2.5