* 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)
}
/**
+ * 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
} 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 {
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');
));
$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;
}
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;
+ }
}
));
$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;
}
{
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;
+ }
}
$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;
}
{
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;
+ }
}