mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 10:56:38 +00:00
removed coupling between Twig_Node and Twig_Template
This coupling was bad as Twig_Node should only be useful during compilation. So, the Twig_Node_Expression_GetAttr constants have been moved to Twig_TemplateInterface.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
Changes:
|
||||
|
||||
* removed coupling between Twig_Node and Twig_Template
|
||||
* fixed the ternary operator precedence rule
|
||||
|
||||
* 1.0.0-RC1 (2011-01-09)
|
||||
|
||||
@@ -249,7 +249,7 @@ class Twig_ExpressionParser
|
||||
}
|
||||
|
||||
if (null !== $alias = $this->parser->getImportedFunction($node->getAttribute('name'))) {
|
||||
return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $args, $node->getLine(), Twig_Node_Expression_GetAttr::TYPE_METHOD);
|
||||
return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $args, $node->getLine(), Twig_TemplateInterface::METHOD_CALL);
|
||||
}
|
||||
|
||||
return new Twig_Node_Expression_Function($node, $args, $node->getLine());
|
||||
@@ -260,7 +260,7 @@ class Twig_ExpressionParser
|
||||
$token = $this->parser->getStream()->next();
|
||||
$lineno = $token->getLine();
|
||||
$arguments = new Twig_Node();
|
||||
$type = Twig_Node_Expression_GetAttr::TYPE_ANY;
|
||||
$type = Twig_TemplateInterface::ANY_CALL;
|
||||
if ($token->getValue() == '.') {
|
||||
$token = $this->parser->getStream()->next();
|
||||
if (
|
||||
@@ -273,7 +273,7 @@ class Twig_ExpressionParser
|
||||
$arg = new Twig_Node_Expression_Constant($token->getValue(), $lineno);
|
||||
|
||||
if ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
|
||||
$type = Twig_Node_Expression_GetAttr::TYPE_METHOD;
|
||||
$type = Twig_TemplateInterface::METHOD_CALL;
|
||||
$arguments = $this->parseArguments();
|
||||
} else {
|
||||
$arguments = new Twig_Node();
|
||||
@@ -282,7 +282,7 @@ class Twig_ExpressionParser
|
||||
throw new Twig_Error_Syntax('Expected name or number', $lineno);
|
||||
}
|
||||
} else {
|
||||
$type = Twig_Node_Expression_GetAttr::TYPE_ARRAY;
|
||||
$type = Twig_TemplateInterface::ARRAY_CALL;
|
||||
|
||||
$arg = $this->parseExpression();
|
||||
$this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ']');
|
||||
|
||||
@@ -11,10 +11,6 @@
|
||||
*/
|
||||
class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
|
||||
{
|
||||
const TYPE_ANY = 'any';
|
||||
const TYPE_ARRAY = 'array';
|
||||
const TYPE_METHOD = 'method';
|
||||
|
||||
public function __construct(Twig_Node_Expression $node, Twig_Node_Expression $attribute, Twig_NodeInterface $arguments, $type, $lineno)
|
||||
{
|
||||
parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type), $lineno);
|
||||
|
||||
@@ -203,19 +203,19 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
* @param mixed $object The object or array from where to get the item
|
||||
* @param mixed $item The item to get from the array or object
|
||||
* @param array $arguments An array of arguments to pass if the item is an object method
|
||||
* @param integer $type The type of attribute (@see Twig_Node_Expression_GetAttr)
|
||||
* @param integer $type The type of attribute (@see Twig_TemplateInterface)
|
||||
* @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
|
||||
* @param integer $line The line where the attribute is get
|
||||
*/
|
||||
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false, $line = -1)
|
||||
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $noStrictCheck = false, $line = -1)
|
||||
{
|
||||
// array
|
||||
if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
|
||||
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
|
||||
if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
|
||||
return $object[$item];
|
||||
}
|
||||
|
||||
if (Twig_Node_Expression_GetAttr::TYPE_ARRAY === $type) {
|
||||
if (Twig_TemplateInterface::ARRAY_CALL === $type) {
|
||||
if (!$this->env->isStrictVariables() || $noStrictCheck) {
|
||||
return null;
|
||||
}
|
||||
@@ -251,7 +251,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
}
|
||||
|
||||
// object property
|
||||
if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
|
||||
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
|
||||
if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item)) {
|
||||
if ($this->env->hasExtension('sandbox')) {
|
||||
$this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
|
||||
|
||||
@@ -17,6 +17,10 @@
|
||||
*/
|
||||
interface Twig_TemplateInterface
|
||||
{
|
||||
const ANY_CALL = 'any';
|
||||
const ARRAY_CALL = 'array';
|
||||
const METHOD_CALL = 'method';
|
||||
|
||||
/**
|
||||
* Renders the template with the given context and returns it as string.
|
||||
*
|
||||
|
||||
@@ -24,12 +24,12 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
|
||||
new Twig_Node_Expression_Name('foo', 0),
|
||||
new Twig_Node_Expression_Constant('bar', 0),
|
||||
));
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ARRAY, 0);
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
|
||||
|
||||
$this->assertEquals($expr, $node->getNode('node'));
|
||||
$this->assertEquals($attr, $node->getNode('attribute'));
|
||||
$this->assertEquals($args, $node->getNode('arguments'));
|
||||
$this->assertEquals(Twig_Node_Expression_GetAttr::TYPE_ARRAY, $node->getAttribute('type'));
|
||||
$this->assertEquals(Twig_TemplateInterface::ARRAY_CALL, $node->getAttribute('type'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,10 +48,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
|
||||
$expr = new Twig_Node_Expression_Name('foo', 0);
|
||||
$attr = new Twig_Node_Expression_Constant('bar', 0);
|
||||
$args = new Twig_Node();
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ANY, 0);
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0);
|
||||
$tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "any", false, 0)');
|
||||
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ARRAY, 0);
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0);
|
||||
$tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "array", false, 0)');
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
|
||||
new Twig_Node_Expression_Name('foo', 0),
|
||||
new Twig_Node_Expression_Constant('bar', 0),
|
||||
));
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_METHOD, 0);
|
||||
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0);
|
||||
$tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", ), "method", false, 0)');
|
||||
|
||||
return $tests;
|
||||
|
||||
@@ -29,7 +29,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
$env = new Twig_Environment(null, array('strict_variables' => true));
|
||||
$template = new Twig_TemplateTest($env);
|
||||
|
||||
$template->getAttribute($array, 'unknown', array(), Twig_Node_Expression_GetAttr::TYPE_ARRAY);
|
||||
$template->getAttribute($array, 'unknown', array(), Twig_TemplateInterface::ARRAY_CALL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,9 +49,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
$objectArray = new Twig_TemplateObjectArrayAccess();
|
||||
$objectMagic = new Twig_TemplateObjectMagic();
|
||||
|
||||
$anyType = Twig_Node_Expression_GetAttr::TYPE_ANY;
|
||||
$methodType = Twig_Node_Expression_GetAttr::TYPE_METHOD;
|
||||
$arrayType = Twig_Node_Expression_GetAttr::TYPE_ARRAY;
|
||||
$anyType = Twig_TemplateInterface::ANY_CALL;
|
||||
$methodType = Twig_TemplateInterface::METHOD_CALL;
|
||||
$arrayType = Twig_TemplateInterface::ARRAY_CALL;
|
||||
|
||||
$tests = array(
|
||||
// ARRAY
|
||||
@@ -95,7 +95,7 @@ class Twig_TemplateTest extends Twig_Template
|
||||
{
|
||||
}
|
||||
|
||||
public function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false, $lineno = -1)
|
||||
public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $noStrictCheck = false, $lineno = -1)
|
||||
{
|
||||
return parent::getAttribute($object, $item, $arguments, $type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user