From: Martin Hason Date: Thu, 6 Jan 2011 22:26:54 +0000 (+0100) Subject: added support for line number in Twig_Template::getAttribute() X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7e53fbc99e92feb1470de7b59c4c22874363e3e4;p=web%2Fkonrad%2Ftwig.git added support for line number in Twig_Template::getAttribute() --- diff --git a/lib/Twig/Node/Expression/GetAttr.php b/lib/Twig/Node/Expression/GetAttr.php index 815d9fd..0c66f04 100644 --- a/lib/Twig/Node/Expression/GetAttr.php +++ b/lib/Twig/Node/Expression/GetAttr.php @@ -39,12 +39,9 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression $compiler ->raw('), ') - ->repr($this->getAttribute('type')); - - if ($this->hasAttribute('is_defined_test')) { - $compiler->raw(', true'); - } - - $compiler->raw(')'); + ->repr($this->getAttribute('type')) + ->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false') + ->raw(sprintf(', %d', $this->lineno)) + ->raw(')'); } } diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 2fdd88e..829d583 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -205,8 +205,9 @@ abstract class Twig_Template implements Twig_TemplateInterface * @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 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) + protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false, $line = -1) { // array if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) { @@ -220,10 +221,10 @@ abstract class Twig_Template implements Twig_TemplateInterface } if (is_object($object)) { - throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); // array } else { - throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), $line, $this->getTemplateName()); } } } @@ -232,7 +233,7 @@ abstract class Twig_Template implements Twig_TemplateInterface if (!$this->env->isStrictVariables() || $noStrictCheck) { return null; } - throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), $line, $this->getTemplateName()); } // get some information about the object @@ -275,7 +276,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName()); } if ($this->env->hasExtension('sandbox')) { diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index e72180e..a4fbb96 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase $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); - $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "any")'); + $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); - $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "array")'); + $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "array", false, 0)'); $args = new Twig_Node(array( @@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase new Twig_Node_Expression_Constant('bar', 0), )); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_METHOD, 0); - $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", ), "method")'); + $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; }