added Twig_Test_Node to allow more complex tests to have their own Node class (sameas has been converted as an example)

This commit is contained in:
Fabien Potencier
2011-10-31 20:57:30 +01:00
parent 083d839000
commit b1d06ccbc9
5 changed files with 81 additions and 3 deletions
+1
View File
@@ -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)
+10 -3
View File
@@ -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());
}
/**
+30
View File
@@ -0,0 +1,30 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Checks if a variable is the same as another one (=== in PHP).
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
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(')')
;
}
}
+5
View File
@@ -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);
+35
View File
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2010 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Represents a template test as a Node.
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
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()
{
}
}