moved the defined test to a Twig_Test_Node class

This commit is contained in:
Fabien Potencier
2011-11-01 08:43:09 +01:00
parent b1d06ccbc9
commit c749c6b822
4 changed files with 57 additions and 51 deletions
+1 -21
View File
@@ -110,7 +110,7 @@ class Twig_Extension_Core extends Twig_Extension
return array(
'even' => new Twig_Test_Function('twig_test_even'),
'odd' => new Twig_Test_Function('twig_test_odd'),
'defined' => new Twig_Test_Function('twig_test_defined'),
'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'),
'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'),
@@ -837,26 +837,6 @@ function twig_test_constant($value, $constant)
return constant($constant) === $value;
}
/**
* Checks if a variable is defined in the current context.
*
* <pre>
* {# defined works with variable names #}
* {% if foo is defined %}
* {# ... #}
* {% endif %}
* </pre>
*
* @param mixed $name A PHP variable
* @param array $context The current context
*
* @return Boolean true if the value is defined, false otherwise
*/
function twig_test_defined($name, $context)
{
return array_key_exists($name, $context);
}
/**
* Checks if a variable is empty.
*
+1 -1
View File
@@ -16,7 +16,7 @@ class Twig_Node_Expression_DefaultFilter extends Twig_Node_Expression
throw new Twig_Error('The default filter node cannot be created from the given node.', $node->getLine());
}
$test = new Twig_Node_Expression_Test(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
$test = new Twig_Node_Expression_Test_Defined(clone $node->getNode('node'), 'defined', new Twig_Node(), $node->getLine());
$default = count($node->getNode('arguments')) ? $node->getNode('arguments')->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
$node = new Twig_Node_Expression_Conditional($test, $node, $default, $node->getLine());
-29
View File
@@ -13,28 +13,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
{
parent::__construct(array('node' => $node, 'arguments' => $arguments), array('name' => $name), $lineno);
// defined is a special case
if ('defined' === $name) {
if ($node instanceof Twig_Node_Expression_Name) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof Twig_Node_Expression_GetAttr) {
$node->setAttribute('is_defined_test', true);
$this->changeIgnoreStrictCheck($node);
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
}
}
protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
{
$node->setAttribute('ignore_strict_check', true);
if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
$this->changeIgnoreStrictCheck($node->getNode('node'));
}
}
public function compile(Twig_Compiler $compiler)
@@ -47,13 +25,6 @@ class Twig_Node_Expression_Test extends Twig_Node_Expression
$name = $this->getAttribute('name');
$node = $this->getNode('node');
// defined is a special case
if ('defined' === $name) {
$compiler->subcompile($node);
return;
}
$compiler
->raw($testMap[$name]->compile().'(')
->subcompile($node)
+55
View File
@@ -0,0 +1,55 @@
<?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 defined in the current context.
*
* <pre>
* {# defined works with variable names and variable attributes #}
* {% if foo is defined %}
* {# ... #}
* {% endif %}
* </pre>
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
class Twig_Node_Expression_Test_Defined extends Twig_Node_Expression_Test
{
public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface $arguments = null, $lineno)
{
parent::__construct($node, $name, $arguments, $lineno);
if ($node instanceof Twig_Node_Expression_Name) {
$node->setAttribute('is_defined_test', true);
} elseif ($node instanceof Twig_Node_Expression_GetAttr) {
$node->setAttribute('is_defined_test', true);
$this->changeIgnoreStrictCheck($node);
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
}
}
protected function changeIgnoreStrictCheck(Twig_Node_Expression_GetAttr $node)
{
$node->setAttribute('ignore_strict_check', true);
if ($node->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
$this->changeIgnoreStrictCheck($node->getNode('node'));
}
}
public function compile(Twig_Compiler $compiler)
{
$compiler->subcompile($this->getNode('node'));
}
}