From b1d06ccbc9a597c5b0dd6727a79fffca8dbee857 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 31 Oct 2011 20:57:30 +0100 Subject: [PATCH] added Twig_Test_Node to allow more complex tests to have their own Node class (sameas has been converted as an example) --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 13 ++++++++-- lib/Twig/Node/Expression/Test/Sameas.php | 30 +++++++++++++++++++++++++ lib/Twig/Parser.php | 5 ++++ lib/Twig/Test/Node.php | 35 ++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 lib/Twig/Node/Expression/Test/Sameas.php create mode 100644 lib/Twig/Test/Node.php diff --git a/CHANGELOG b/CHANGELOG index cb519ca..341b57c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0 + * added Twig_Test_Node to allow more complex tests to have their own Node class * added a better error message when a template is empty but contain a BOM * fixed in operator for empty strings * fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 1b89aa8..e39b9f4 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -111,7 +111,7 @@ class Twig_Extension_Core extends Twig_Extension 'even' => new Twig_Test_Function('twig_test_even'), 'odd' => new Twig_Test_Function('twig_test_odd'), 'defined' => new Twig_Test_Function('twig_test_defined'), - 'sameas' => new Twig_Test_Function('twig_test_sameas'), + 'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'), 'none' => new Twig_Test_Function('twig_test_none'), 'null' => new Twig_Test_Function('twig_test_none'), 'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'), @@ -170,13 +170,20 @@ class Twig_Extension_Core extends Twig_Extension public function parseTestExpression(Twig_Parser $parser, $node) { $stream = $parser->getStream(); - $name = $stream->expect(Twig_Token::NAME_TYPE); + $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue(); $arguments = null; if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) { $arguments = $parser->getExpressionParser()->parseArguments(); } - return new Twig_Node_Expression_Test($node, $name->getValue(), $arguments, $parser->getCurrentToken()->getLine()); + $testMap = $parser->getEnvironment()->getTests(); + if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) { + $class = $testMap[$name]->getClass(); + } else { + $class = 'Twig_Node_Expression_Test'; + } + + return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine()); } /** diff --git a/lib/Twig/Node/Expression/Test/Sameas.php b/lib/Twig/Node/Expression/Test/Sameas.php new file mode 100644 index 0000000..8639b96 --- /dev/null +++ b/lib/Twig/Node/Expression/Test/Sameas.php @@ -0,0 +1,30 @@ + + */ +class Twig_Node_Expression_Test_Sameas extends Twig_Node_Expression_Test +{ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->raw('(') + ->subcompile($this->getNode('node')) + ->raw(' === ') + ->subcompile($this->getNode('arguments')->getNode(0)) + ->raw(')') + ; + } +} diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 72910b5..8fcf9c4 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -42,6 +42,11 @@ class Twig_Parser implements Twig_ParserInterface $this->env = $env; } + public function getEnvironment() + { + return $this->env; + } + public function getVarName() { return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount); diff --git a/lib/Twig/Test/Node.php b/lib/Twig/Test/Node.php new file mode 100644 index 0000000..47a978e --- /dev/null +++ b/lib/Twig/Test/Node.php @@ -0,0 +1,35 @@ + + */ +class Twig_Test_Node implements Twig_TestInterface +{ + protected $class; + + public function __construct($class) + { + $this->class = $class; + } + + public function getClass() + { + return $this->class; + } + + public function compile() + { + } +} -- 1.7.2.5