From e48b1f3bdf0120ea9297428082f06ada059b1620 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 4 Jan 2013 22:19:42 +0100 Subject: [PATCH] fixed registration of tests and functions as anonymous functions (closes #946) --- CHANGELOG | 3 ++- lib/Twig/Environment.php | 20 ++++++++++++++++++++ lib/Twig/Node/Expression/Call.php | 3 ++- lib/Twig/Node/Expression/Test.php | 3 +-- test/Twig/Tests/Node/Expression/FilterTest.php | 12 ++++++++++++ test/Twig/Tests/Node/Expression/FunctionTest.php | 12 ++++++++++++ test/Twig/Tests/Node/Expression/TestTest.php | 13 ++++++++++++- 7 files changed, 61 insertions(+), 5 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 22d5f9f..a152b91 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ * 1.12.0 (2012-XX-XX) - * n/a + * fixed registration of tests and functions as anonymous functions + * fixed global management * 1.12.0-RC1 (2012-12-29) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 562df4f..83b1e61 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -873,6 +873,26 @@ class Twig_Environment } /** + * Gets a test by name. + * + * @param string $name The test name + * + * @return Twig_Test|false A Twig_Test instance or false if the test does not exist + */ + public function getTest($name) + { + if (!$this->extensionInitialized) { + $this->initExtensions(); + } + + if (isset($this->tests[$name])) { + return $this->tests[$name]; + } + + return false; + } + + /** * Registers a Function. * * @param string|Twig_SimpleFunction $name The function name or a Twig_SimpleFunction instance diff --git a/lib/Twig/Node/Expression/Call.php b/lib/Twig/Node/Expression/Call.php index e014955..a97b3b5 100644 --- a/lib/Twig/Node/Expression/Call.php +++ b/lib/Twig/Node/Expression/Call.php @@ -21,7 +21,8 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression } elseif (is_array($callable) && $callable[0] instanceof Twig_ExtensionInterface) { $compiler->raw(sprintf('$this->env->getExtension(\'%s\')->%s', $callable[0]->getName(), $callable[1])); } else { - $compiler->raw(sprintf('call_user_func($this->env->getFilter(\'%s\')->getCallable(), ', $this->getAttribute('name'))); + $type = ucfirst($this->getAttribute('type')); + $compiler->raw(sprintf('call_user_func_array($this->env->get%s(\'%s\')->getCallable(), array', $type, $this->getAttribute('name'))); $closingParenthesis = true; } } else { diff --git a/lib/Twig/Node/Expression/Test.php b/lib/Twig/Node/Expression/Test.php index 9ec54f3..639f501 100644 --- a/lib/Twig/Node/Expression/Test.php +++ b/lib/Twig/Node/Expression/Test.php @@ -18,8 +18,7 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression_Call public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); - $testMap = $compiler->getEnvironment()->getTests(); - $test = $testMap[$name]; + $test = $compiler->getEnvironment()->getTest($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'test'); diff --git a/test/Twig/Tests/Node/Expression/FilterTest.php b/test/Twig/Tests/Node/Expression/FilterTest.php index 86cb834..5eb776c 100644 --- a/test/Twig/Tests/Node/Expression/FilterTest.php +++ b/test/Twig/Tests/Node/Expression/FilterTest.php @@ -75,6 +75,10 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase )); $tests[] = array($node, 'twig_reverse_filter($this->env, "abc", true)'); + // filter as an anonymous function + $node = $this->createFilter(new Twig_Node_Expression_Constant('foo', 1), 'anonymous'); + $tests[] = array($node, 'call_user_func_array($this->env->getFilter(\'anonymous\')->getCallable(), array("foo"))'); + return $tests; } @@ -115,4 +119,12 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase return new Twig_Node_Expression_Filter($node, $name, $arguments, 1); } + + protected function getEnvironment() + { + $env = parent::getEnvironment(); + $env->addFilter(new Twig_SimpleFilter('anonymous', function () {})); + + return $env; + } } diff --git a/test/Twig/Tests/Node/Expression/FunctionTest.php b/test/Twig/Tests/Node/Expression/FunctionTest.php index 50f41f5..0a69621 100644 --- a/test/Twig/Tests/Node/Expression/FunctionTest.php +++ b/test/Twig/Tests/Node/Expression/FunctionTest.php @@ -74,6 +74,10 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase )); $tests[] = array($node, 'twig_date_converter($this->env, 0, "America/Chicago")'); + // function as an anonymous function + $node = $this->createFunction('anonymous', array(new Twig_Node_Expression_Constant('foo', 1))); + $tests[] = array($node, 'call_user_func_array($this->env->getFunction(\'anonymous\')->getCallable(), array("foo"))'); + return $tests; } @@ -81,4 +85,12 @@ class Twig_Tests_Node_Expression_FunctionTest extends Twig_Test_NodeTestCase { return new Twig_Node_Expression_Function($name, new Twig_Node($arguments), 1); } + + protected function getEnvironment() + { + $env = parent::getEnvironment(); + $env->addFunction(new Twig_SimpleFunction('anonymous', function () {})); + + return $env; + } } diff --git a/test/Twig/Tests/Node/Expression/TestTest.php b/test/Twig/Tests/Node/Expression/TestTest.php index 01a188b..988c94e 100644 --- a/test/Twig/Tests/Node/Expression/TestTest.php +++ b/test/Twig/Tests/Node/Expression/TestTest.php @@ -41,9 +41,12 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase $expr = new Twig_Node_Expression_Constant('foo', 1); $node = new Twig_Node_Expression_Test_Null($expr, 'null', new Twig_Node(array()), 1); - $tests[] = array($node, '(null === "foo")'); + // test as an anonymous function + $node = $this->createTest(new Twig_Node_Expression_Constant('foo', 1), 'anonymous', array(new Twig_Node_Expression_Constant('foo', 1))); + $tests[] = array($node, 'call_user_func_array($this->env->getTest(\'anonymous\')->getCallable(), array("foo", "foo"))'); + return $tests; } @@ -51,4 +54,12 @@ class Twig_Tests_Node_Expression_TestTest extends Twig_Test_NodeTestCase { return new Twig_Node_Expression_Test($node, $name, new Twig_Node($arguments), 1); } + + protected function getEnvironment() + { + $env = parent::getEnvironment(); + $env->addTest(new Twig_SimpleTest('anonymous', function () {})); + + return $env; + } } -- 1.7.2.5