fixed registration of tests and functions as anonymous functions (closes #946)
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 4 Jan 2013 21:19:42 +0000 (22:19 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 4 Jan 2013 21:20:09 +0000 (22:20 +0100)
CHANGELOG
lib/Twig/Environment.php
lib/Twig/Node/Expression/Call.php
lib/Twig/Node/Expression/Test.php
test/Twig/Tests/Node/Expression/FilterTest.php
test/Twig/Tests/Node/Expression/FunctionTest.php
test/Twig/Tests/Node/Expression/TestTest.php

index 22d5f9f..a152b91 100644 (file)
--- 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)
 
index 562df4f..83b1e61 100644 (file)
@@ -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
index e014955..a97b3b5 100644 (file)
@@ -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 {
index 9ec54f3..639f501 100644 (file)
@@ -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');
index 86cb834..5eb776c 100644 (file)
@@ -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;
+    }
 }
index 50f41f5..0a69621 100644 (file)
@@ -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;
+    }
 }
index 01a188b..988c94e 100644 (file)
@@ -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;
+    }
 }