From e84448bc84ef86f12919c4ad817c3636726f7efa Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 12:52:26 +0100 Subject: [PATCH 01/16] fix numeric keys in array --- lib/Twig/Template.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index abc3400ee..c73ad4d81 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -336,18 +336,16 @@ abstract class Twig_Template implements Twig_TemplateInterface */ protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { - $item = ctype_digit((string) $item) ? (int) $item : (string) $item; - // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if ((is_array($object) && array_key_exists($item, $object)) - || ($object instanceof ArrayAccess && isset($object[$item])) + if ((is_array($object) && array_key_exists((string) $item, $object)) + || ($object instanceof ArrayAccess && isset($object[(string) $item])) ) { if ($isDefinedTest) { return true; } - return $object[$item]; + return $object[(string) $item]; } if (Twig_TemplateInterface::ARRAY_CALL === $type) { @@ -385,7 +383,7 @@ abstract class Twig_Template implements Twig_TemplateInterface // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if (isset($object->$item) || array_key_exists($item, $object)) { + if (isset($object->$item) || array_key_exists((string) $item, $object)) { if ($isDefinedTest) { return true; } @@ -405,13 +403,13 @@ abstract class Twig_Template implements Twig_TemplateInterface $lcItem = strtolower($item); if (isset(self::$cache[$class]['methods'][$lcItem])) { - $method = $item; + $method = (string) $item; } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) { $method = 'get'.$item; } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) { $method = 'is'.$item; } elseif (isset(self::$cache[$class]['methods']['__call'])) { - $method = $item; + $method = (string) $item; } else { if ($isDefinedTest) { return false; From c4d5b3ffdb07b2d36ebb52c3b6321bbce6f81037 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 31 Oct 2012 13:42:47 +0100 Subject: [PATCH 02/16] added some unit tests for previous merge --- test/Twig/Tests/TemplateTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 7b821dc35..014e036aa 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -199,6 +199,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase 'null' => null, '1' => 1, 'bar' => true, + '09' => '09', + '+4' => '+4', ); $objectArray = new Twig_TemplateArrayAccessObject(); @@ -224,6 +226,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase array(true, 1, 1.0), array(true, null, 'null'), array(true, true, 'bar'), + array(true, '09', '09'), + array(true, '+4', '+4'), ); $testObjects = array( // array(object, type of fetch) @@ -243,6 +247,10 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase foreach ($basicTests as $test) { // properties cannot be numbers if (($testObject[0] instanceof stdClass || $testObject[0] instanceof Twig_TemplatePropertyObject) && is_numeric($test[2])) { + continue; + } + + if ('+4' === $test[2] && $methodObject === $testObject[0]) { continue; } @@ -380,6 +388,8 @@ class Twig_TemplateArrayAccessObject implements ArrayAccess 'null' => null, '1' => 1, 'bar' => true, + '09' => '09', + '+4' => '+4', ); public function offsetExists($name) @@ -410,6 +420,8 @@ class Twig_TemplateMagicPropertyObject 'null' => null, '1' => 1, 'bar' => true, + '09' => '09', + '+4' => '+4', ); protected $protected = 'protected'; @@ -478,6 +490,11 @@ class Twig_TemplateMethodObject return 1; } + public function get09() + { + return '09'; + } + public function getZero() { return 0; From b6acf9dc53732982aa1516605328894d133494b2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 31 Oct 2012 13:42:55 +0100 Subject: [PATCH 03/16] updated CHANGELOG --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index c83a09761..27c22cd12 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ * added a batch filter * added support for encoding an array as query string in the url_encode filter + * fixed getting a numeric-like item on a variable ('09' for instance) * 1.12.2 (2013-02-09) From da616c516995bb5dbb99670b3115e1ec7fd08fdb Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 17:04:56 +0100 Subject: [PATCH 04/16] fixed getAttribute for array access with a boolean or float key --- lib/Twig/Template.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index c73ad4d81..95e0819e4 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -338,14 +338,16 @@ abstract class Twig_Template implements Twig_TemplateInterface { // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if ((is_array($object) && array_key_exists((string) $item, $object)) - || ($object instanceof ArrayAccess && isset($object[(string) $item])) + $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; + + if ((is_array($object) && array_key_exists($arrayItem, $object)) + || ($object instanceof ArrayAccess && isset($object[$arrayItem])) ) { if ($isDefinedTest) { return true; } - return $object[(string) $item]; + return $object[$arrayItem]; } if (Twig_TemplateInterface::ARRAY_CALL === $type) { @@ -358,11 +360,11 @@ 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', $arrayItem, get_class($object)), -1, $this->getTemplateName()); } elseif (is_array($object)) { - 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', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $arrayItem, gettype($object)), -1, $this->getTemplateName()); } } } From fdfd506caf5e72a90b8e5d9742c563bacd3c840b Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 17:51:00 +0100 Subject: [PATCH 05/16] added changelog entry about fixed boolean array access --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 27c22cd12..afdea4975 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ * added a batch filter * added support for encoding an array as query string in the url_encode filter * fixed getting a numeric-like item on a variable ('09' for instance) + * fixed getting a boolean or float key on an array, so it is consistent with PHP's array access: `{{ array[false] }}` behaves the same as `echo $array[false];` (equals `$array[0]`) * 1.12.2 (2013-02-09) From 6fb5afa28f28189e054420eac31e7ae8f3d20871 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 17:11:57 +0100 Subject: [PATCH 06/16] improved error message for non-existent or invalid attributes --- lib/Twig/Template.php | 12 +++++++++--- test/Twig/Tests/TemplateTest.php | 6 +++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 95e0819e4..9d60833f7 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -336,10 +336,10 @@ abstract class Twig_Template implements Twig_TemplateInterface */ protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { + $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; + // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { - $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; - if ((is_array($object) && array_key_exists($arrayItem, $object)) || ($object instanceof ArrayAccess && isset($object[$arrayItem])) ) { @@ -378,7 +378,13 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object), -1, $this->getTemplateName()); + if (Twig_TemplateInterface::METHOD_CALL === $type) { + throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + } elseif (is_array($object)) { + throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); + } else { + throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + } } $class = get_class($object); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 014e036aa..636ae60a9 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -301,9 +301,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase // tests when input is not an array or object $tests = array_merge($tests, array( - array(false, null, 42, 'a', array(), $anyType, false, 'Item "a" for "42" does not exist'), - array(false, null, "string", 'a', array(), $anyType, false, 'Item "a" for "string" does not exist'), - array(false, null, array(), 'a', array(), $anyType, false, 'Item "a" for "Array" does not exist'), + array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "integer" variable'), + array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "string" variable'), + array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'), )); // add twig_template_get_attributes tests From 9ea16ec643cb6de5792508e07a4207bcaaf89f88 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 17:41:29 +0100 Subject: [PATCH 07/16] refactored getAttribute --- lib/Twig/Template.php | 16 +++++----------- test/Twig/Tests/TemplateTest.php | 6 +++--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 9d60833f7..507c6125e 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -336,10 +336,10 @@ abstract class Twig_Template implements Twig_TemplateInterface */ protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false) { - $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; - // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { + $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; + if ((is_array($object) && array_key_exists($arrayItem, $object)) || ($object instanceof ArrayAccess && isset($object[$arrayItem])) ) { @@ -350,7 +350,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return $object[$arrayItem]; } - if (Twig_TemplateInterface::ARRAY_CALL === $type) { + if (Twig_TemplateInterface::ARRAY_CALL === $type || !is_object($object)) { if ($isDefinedTest) { return false; } @@ -364,7 +364,7 @@ abstract class Twig_Template implements Twig_TemplateInterface } elseif (is_array($object)) { throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $arrayItem, gettype($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); } } } @@ -378,13 +378,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - if (Twig_TemplateInterface::METHOD_CALL === $type) { - throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); - } elseif (is_array($object)) { - throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); - } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); - } + throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); } $class = get_class($object); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 636ae60a9..050c37f2a 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -42,11 +42,11 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase public function getAttributeExceptions() { $tests = array( - array('{{ string["a"] }}', 'Impossible to access a key ("a") on a "string" variable in "%s" at line 1', false), + array('{{ string["a"] }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false), array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), - array('{{ string.a }}', 'Item "a" for "foo" does not exist in "%s" at line 1', false), - array('{{ array.a }}', 'Item "a" for "Array" does not exist in "%s" at line 1', false), + array('{{ string.a }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false), + array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), array('{% macro foo(obj) %}{{ obj.missing_method() }}{% endmacro %}{{ _self.foo(array_access) }}', 'Method "missing_method" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), ); From db744d5e51b8da166198ded733428e4ec0f18d42 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Sun, 9 Dec 2012 17:21:53 +0100 Subject: [PATCH 08/16] fix test that that would not fail if no exception is thrown --- test/Twig/Tests/TemplateTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 050c37f2a..26685a660 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -34,8 +34,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase try { $template->render($context); + $this->fail('Accessing an invalid item should throw an exception.'); } catch (Twig_Error_Runtime $e) { - $this->assertEquals(sprintf($message, $name), $e->getMessage()); + $this->assertSame(sprintf($message, $name), $e->getMessage()); } } From 26bac14c760cb754916b08d9332d946142ec0e46 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 20:34:10 +0100 Subject: [PATCH 09/16] added tests for array access with confusable keys array keys like boolean, numeric but string, floats, null --- test/Twig/Tests/TemplateTest.php | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 26685a660..f82319c48 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -47,7 +47,9 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), array('{{ string.a }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false), + array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a "string" variable in "%s" at line 1', false), array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), + array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), array('{% macro foo(obj) %}{{ obj.missing_method() }}{% endmacro %}{{ _self.foo(array_access) }}', 'Method "missing_method" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), ); @@ -140,6 +142,46 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase return $bools; } + /** + * @dataProvider getTestsDependingOnExtensionAvailability + */ + public function testGetAttributeOnArrayWithConfusableKey($useExt = false) + { + $template = new Twig_TemplateTest( + new Twig_Environment(), + $useExt + ); + + $array = array('Zero', 'One', -1 => 'MinusOne', '' => 'EmptyString', '1.5' => 'FloatButString', '01' => 'IntegerButStringWithLeadingZeros'); + + $this->assertSame('Zero', $array[false]); + $this->assertSame('One', $array[true]); + $this->assertSame('One', $array[1.5]); + $this->assertSame('One', $array['1']); + $this->assertSame('MinusOne', $array[-1.5]); + $this->assertSame('FloatButString', $array['1.5']); + $this->assertSame('IntegerButStringWithLeadingZeros', $array['01']); + $this->assertSame('EmptyString', $array[null]); + + $this->assertSame('Zero', $template->getAttribute($array, false), 'false is treated as 0 when accessing an array (equals PHP behavior)'); + $this->assertSame('One', $template->getAttribute($array, true), 'true is treated as 1 when accessing an array (equals PHP behavior)'); + $this->assertSame('One', $template->getAttribute($array, 1.5), 'float is casted to int when accessing an array (equals PHP behavior)'); + $this->assertSame('One', $template->getAttribute($array, '1'), '"1" is treated as integer 1 when accessing an array (equals PHP behavior)'); + $this->assertSame('MinusOne', $template->getAttribute($array, -1.5), 'negative float is casted to int when accessing an array (equals PHP behavior)'); + $this->assertSame('FloatButString', $template->getAttribute($array, '1.5'), '"1.5" is treated as-is when accessing an array (equals PHP behavior)'); + $this->assertSame('IntegerButStringWithLeadingZeros', $template->getAttribute($array, '01'), '"01" is treated as-is when accessing an array (equals PHP behavior)'); + $this->assertSame('EmptyString', $template->getAttribute($array, null), 'null is treated as "" when accessing an array (equals PHP behavior)'); + } + + public function getTestsDependingOnExtensionAvailability() + { + if (function_exists('twig_template_get_attributes')) { + return array(array(false), array(true)); + } + + return array(array(false)); + } + /** * @dataProvider getGetAttributeTests */ From a93f0dca3847f0b68cd1c47a6abac602692d69a2 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Wed, 31 Oct 2012 21:00:51 +0100 Subject: [PATCH 10/16] further improved exception message and distinguish array_call and any_call access --- lib/Twig/Template.php | 6 ++++-- test/Twig/Tests/TemplateTest.php | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 507c6125e..6f3d87dab 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -363,8 +363,10 @@ abstract class Twig_Template implements Twig_TemplateInterface throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $arrayItem, get_class($object)), -1, $this->getTemplateName()); } elseif (is_array($object)) { throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); + } elseif (Twig_TemplateInterface::ARRAY_CALL === $type) { + throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } } } @@ -378,7 +380,7 @@ abstract class Twig_Template implements Twig_TemplateInterface return null; } - throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } $class = get_class($object); diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index f82319c48..4a0c3f6be 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -43,11 +43,11 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase public function getAttributeExceptions() { $tests = array( - array('{{ string["a"] }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false), + array('{{ string["a"] }}', 'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1', false), array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), - array('{{ string.a }}', 'Impossible to access an item ("a") on a "string" variable in "%s" at line 1', false), - array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a "string" variable in "%s" at line 1', false), + array('{{ string.a }}', 'Impossible to access an item ("a") on a string variable ("foo") in "%s" at line 1', false), + array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1', false), array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access.a }}', 'Method "a" for object "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), @@ -344,8 +344,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase // tests when input is not an array or object $tests = array_merge($tests, array( - array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "integer" variable'), - array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a "string" variable'), + array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a integer variable ("42")'), + array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a string variable ("string")'), array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'), )); From e5291e1be13d1e605320728257b7bff173923702 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Sun, 9 Dec 2012 17:48:08 +0100 Subject: [PATCH 11/16] rename item to attribute in the exception message because that's the term that is used in twig, e.g. the function --- lib/Twig/Template.php | 2 +- test/Twig/Tests/TemplateTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 6f3d87dab..a001ca037 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -366,7 +366,7 @@ abstract class Twig_Template implements Twig_TemplateInterface } elseif (Twig_TemplateInterface::ARRAY_CALL === $type) { throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access an item ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } } } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 4a0c3f6be..9ae8147d7 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -34,7 +34,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase try { $template->render($context); - $this->fail('Accessing an invalid item should throw an exception.'); + $this->fail('Accessing an invalid attribute should throw an exception.'); } catch (Twig_Error_Runtime $e) { $this->assertSame(sprintf($message, $name), $e->getMessage()); } @@ -46,7 +46,7 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase array('{{ string["a"] }}', 'Impossible to access a key ("a") on a string variable ("foo") in "%s" at line 1', false), array('{{ array["a"] }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ array_access["a"] }}', 'Key "a" in object (with ArrayAccess) of type "Twig_TemplateArrayAccessObject" does not exist in "%s" at line 1', false), - array('{{ string.a }}', 'Impossible to access an item ("a") on a string variable ("foo") in "%s" at line 1', false), + array('{{ string.a }}', 'Impossible to access an attribute ("a") on a string variable ("foo") in "%s" at line 1', false), array('{{ string.a() }}', 'Impossible to invoke a method ("a") on a string variable ("foo") in "%s" at line 1', false), array('{{ array.a }}', 'Key "a" for array with keys "foo" does not exist in "%s" at line 1', false), array('{{ attribute(array, -10) }}', 'Key "-10" for array with keys "foo" does not exist in "%s" at line 1', false), @@ -344,8 +344,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase // tests when input is not an array or object $tests = array_merge($tests, array( - array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a integer variable ("42")'), - array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an item ("a") on a string variable ("string")'), + array(false, null, 42, 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a integer variable ("42")'), + array(false, null, "string", 'a', array(), $anyType, false, 'Impossible to access an attribute ("a") on a string variable ("string")'), array(false, null, array(), 'a', array(), $anyType, false, 'Key "a" for array with keys "" does not exist'), )); From b41ce6094c619f609d50fea45e96e9327c96504a Mon Sep 17 00:00:00 2001 From: Arjen Brouwer Date: Thu, 28 Feb 2013 22:13:40 +0100 Subject: [PATCH 12/16] updated C extension accordingly --- ext/twig/twig.c | 120 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 37 deletions(-) diff --git a/ext/twig/twig.c b/ext/twig/twig.c index f871d1eba..2924b7b7c 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -76,12 +76,34 @@ zend_module_entry twig_module_entry = { ZEND_GET_MODULE(twig) #endif -int TWIG_ARRAY_KEY_EXISTS(zval *array, char* key, int key_len) +int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { - if (Z_TYPE_P(array) != IS_ARRAY) { - return 0; - } - return zend_symtable_exists(Z_ARRVAL_P(array), key, key_len + 1); + zval temp; + int result; + + if (Z_TYPE_P(array) != IS_ARRAY) { + return 0; + } + + switch (Z_TYPE_P(key)) { + case IS_STRING: + return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); + + case IS_NULL: + return zend_hash_exists(Z_ARRVAL_P(array), "", 1); + + case IS_BOOL: + case IS_DOUBLE: + convert_to_long(key); + return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); + + case IS_LONG: + return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); + + default: + convert_to_string(key); + return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); + } } int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC) @@ -245,7 +267,7 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC) zval **tmp_zval; char *tmp_name; - if (class == NULL || Z_TYPE_P(class) != IS_ARRAY || Z_TYPE_P(prop_name) != IS_STRING) { + if (class == NULL || Z_TYPE_P(class) != IS_ARRAY) { if (class != NULL && Z_TYPE_P(class) == IS_OBJECT && TWIG_INSTANCE_OF(class, zend_ce_arrayaccess TSRMLS_CC)) { // array access object return TWIG_GET_ARRAYOBJECT_ELEMENT(class, prop_name TSRMLS_CC); @@ -253,11 +275,26 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC) return NULL; } - convert_to_string(prop_name); - tmp_name = Z_STRVAL_P(prop_name); - if (zend_symtable_find(HASH_OF(class), tmp_name, strlen(tmp_name)+1, (void**) &tmp_zval) == SUCCESS) { - return *tmp_zval; - } + if (Z_TYPE_P(prop_name) == IS_NULL) { + if (zend_hash_find(HASH_OF(class), "", 1, (void**) &tmp_zval) == SUCCESS) { + return *tmp_zval; + } + + } else if (Z_TYPE_P(prop_name) == IS_BOOL || Z_TYPE_P(prop_name) == IS_DOUBLE || Z_TYPE_P(prop_name) == IS_LONG) { + if (Z_TYPE_P(prop_name) != IS_LONG) { + convert_to_long(prop_name); + } + + if (zend_hash_index_find(HASH_OF(class), Z_LVAL_P(prop_name), (void **) &tmp_zval) == SUCCESS) { + return *tmp_zval; + } + + } else if (Z_TYPE_P(prop_name) == IS_STRING) { + if (zend_symtable_find(HASH_OF(class), Z_STRVAL_P(prop_name), Z_STRLEN_P(prop_name) + 1, (void**) &tmp_zval) == SUCCESS) { + return *tmp_zval; + } + } + return NULL; } @@ -714,7 +751,7 @@ PHP_FUNCTION(twig_template_get_attributes) zval *object; char *item; int item_len; - zval zitem; + zval *zitem, ztmpitem; zval *arguments = NULL; zval *ret = NULL; char *type = NULL; @@ -725,22 +762,17 @@ PHP_FUNCTION(twig_template_get_attributes) zval *tmp_self_cache; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozs|asbb", &template, &object, &item, &item_len, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozz|asbb", &template, &object, &zitem, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { return; } - - INIT_PZVAL(&zitem); - ZVAL_STRINGL(&zitem, item, item_len, 0); - - switch (is_numeric_string(item, item_len, &Z_LVAL(zitem), &Z_DVAL(zitem), 0)) { - case IS_LONG: - Z_TYPE(zitem) = IS_LONG; - break; - case IS_DOUBLE: - Z_TYPE(zitem) = IS_DOUBLE; - convert_to_long(&zitem); - break; - } + + // convert the item to a string + ztmpitem = *zitem; + zval_copy_ctor(&ztmpitem); + convert_to_string(&ztmpitem); + item_len = Z_STRLEN(ztmpitem); + item = estrndup(Z_STRVAL(ztmpitem), item_len); + zval_dtor(&ztmpitem); if (!type) { type = "any"; @@ -759,10 +791,11 @@ PHP_FUNCTION(twig_template_get_attributes) return $object[$item]; } */ + + if (strcmp("method", type) != 0) { -// printf("XXXmethod: %s\n", type); - if ((TWIG_ARRAY_KEY_EXISTS(object, item, item_len)) - || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, &zitem TSRMLS_CC)) + if ((TWIG_ARRAY_KEY_EXISTS(object, zitem)) + || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, zitem TSRMLS_CC)) ) { zval *ret; @@ -770,7 +803,8 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_TRUE; } - ret = TWIG_GET_ARRAY_ELEMENT(object, item, item_len TSRMLS_CC); + ret = TWIG_GET_ARRAY_ELEMENT_ZVAL(object, zitem TSRMLS_CC); + if (!ret) { ret = &EG(uninitialized_zval); } @@ -812,7 +846,11 @@ PHP_FUNCTION(twig_template_get_attributes) } else if (Z_TYPE_P(object) == IS_ARRAY) { TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC)); } else { - TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to access a key (\"%s\") on a \"%s\" variable", item, zend_zval_type_name(object)); + char *type_name = zend_zval_type_name(object); + Z_ADDREF_P(object); + convert_to_string(object); + TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to access a key (\"%s\") on a %s variable (\"%s\")", item, type_name, Z_STRVAL_P(object)); + zval_ptr_dtor(&object); } return; } @@ -839,12 +877,20 @@ PHP_FUNCTION(twig_template_get_attributes) if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) { RETURN_FALSE; } + if (Z_TYPE_P(object) == IS_ARRAY) { - TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Item \"%s\" for \"Array\" does not exist", item); + TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC)); } else { + char *type_name = zend_zval_type_name(object); Z_ADDREF_P(object); convert_to_string_ex(&object); - TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Item \"%s\" for \"%s\" does not exist", item, Z_STRVAL_P(object)); + + TWIG_RUNTIME_ERROR(template TSRMLS_CC, + (strcmp("method", type) == 0) + ? "Impossible to invoke a method (\"%s\") on a %s variable (\"%s\")" + : "Impossible to access an attribute (\"%s\") on a %s variable (\"%s\")", + item, type_name, Z_STRVAL_P(object)); + zval_ptr_dtor(&object); } return; @@ -904,18 +950,18 @@ PHP_FUNCTION(twig_template_get_attributes) efree(class_name); - if (tmp_item || TWIG_HAS_PROPERTY(object, &zitem TSRMLS_CC) || TWIG_HAS_DYNAMIC_PROPERTY(object, item, item_len TSRMLS_CC)) { + if (tmp_item || TWIG_HAS_PROPERTY(object, zitem TSRMLS_CC) || TWIG_HAS_DYNAMIC_PROPERTY(object, item, item_len TSRMLS_CC)) { if (isDefinedTest) { RETURN_TRUE; } if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "sandbox" TSRMLS_CC)) { - TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkPropertyAllowed", object, &zitem TSRMLS_CC); + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkPropertyAllowed", object, zitem TSRMLS_CC); } if (EG(exception)) { return; } - ret = TWIG_PROPERTY(object, &zitem TSRMLS_CC); + ret = TWIG_PROPERTY(object, zitem TSRMLS_CC); RETURN_ZVAL(ret, 1, 0); } } @@ -1001,7 +1047,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (TWIG_CALL_SB(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "hasExtension", "sandbox" TSRMLS_CC)) { - TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkMethodAllowed", object, &zitem TSRMLS_CC); + TWIG_CALL_ZZ(TWIG_CALL_S(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getExtension", "sandbox" TSRMLS_CC), "checkMethodAllowed", object, zitem TSRMLS_CC); } if (EG(exception)) { efree(tmp_method_name_get); From 9347c52b64339d8b8fc85ae58d02057feb716d45 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Tue, 26 Mar 2013 19:43:30 +0100 Subject: [PATCH 13/16] [ext] updated PHP code documenting C code --- ext/twig/twig.c | 74 ++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/ext/twig/twig.c b/ext/twig/twig.c index 2924b7b7c..14d127bbb 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -78,7 +78,7 @@ ZEND_GET_MODULE(twig) int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { - zval temp; + zval temp; int result; if (Z_TYPE_P(array) != IS_ARRAY) { @@ -99,7 +99,7 @@ int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) case IS_LONG: return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); - + default: convert_to_string(key); return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); @@ -283,7 +283,7 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC) } else if (Z_TYPE_P(prop_name) == IS_BOOL || Z_TYPE_P(prop_name) == IS_DOUBLE || Z_TYPE_P(prop_name) == IS_LONG) { if (Z_TYPE_P(prop_name) != IS_LONG) { convert_to_long(prop_name); - } + } if (zend_hash_index_find(HASH_OF(class), Z_LVAL_P(prop_name), (void **) &tmp_zval) == SUCCESS) { return *tmp_zval; @@ -765,13 +765,13 @@ PHP_FUNCTION(twig_template_get_attributes) if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ozz|asbb", &template, &object, &zitem, &arguments, &type, &type_len, &isDefinedTest, &ignoreStrictCheck) == FAILURE) { return; } - + // convert the item to a string ztmpitem = *zitem; zval_copy_ctor(&ztmpitem); convert_to_string(&ztmpitem); item_len = Z_STRLEN(ztmpitem); - item = estrndup(Z_STRVAL(ztmpitem), item_len); + item = estrndup(Z_STRVAL(ztmpitem), item_len); zval_dtor(&ztmpitem); if (!type) { @@ -781,14 +781,16 @@ PHP_FUNCTION(twig_template_get_attributes) /* // array if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if ((is_array($object) && array_key_exists($item, $object)) - || ($object instanceof ArrayAccess && isset($object[$item])) + $arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item; + + if ((is_array($object) && array_key_exists($arrayItem, $object)) + || ($object instanceof ArrayAccess && isset($object[$arrayItem])) ) { if ($isDefinedTest) { return true; } - return $object[$item]; + return $object[$arrayItem]; } */ @@ -832,11 +834,13 @@ PHP_FUNCTION(twig_template_get_attributes) } /* 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', $arrayItem, get_class($object)), -1, $this->getTemplateName()); } elseif (is_array($object)) { - 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', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName()); + } elseif (Twig_TemplateInterface::ARRAY_CALL === $type) { + throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } else { - throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a "%s" variable', $item, gettype($object)), -1, $this->getTemplateName()); + throw new Twig_Error_Runtime(sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } } } @@ -871,7 +875,7 @@ PHP_FUNCTION(twig_template_get_attributes) if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } - throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, implode(', ', array_keys($object)))); + throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } */ if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) { @@ -885,8 +889,8 @@ PHP_FUNCTION(twig_template_get_attributes) Z_ADDREF_P(object); convert_to_string_ex(&object); - TWIG_RUNTIME_ERROR(template TSRMLS_CC, - (strcmp("method", type) == 0) + TWIG_RUNTIME_ERROR(template TSRMLS_CC, + (strcmp("method", type) == 0) ? "Impossible to invoke a method (\"%s\") on a %s variable (\"%s\")" : "Impossible to access an attribute (\"%s\") on a %s variable (\"%s\")", item, type_name, Z_STRVAL_P(object)); @@ -896,19 +900,19 @@ PHP_FUNCTION(twig_template_get_attributes) return; } /* - // get some information about the object - $class = get_class($object); - if (!isset(self::$cache[$class])) { - $r = new ReflectionClass($class); - self::$cache[$class] = array('methods' => array(), 'properties' => array()); - foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - self::$cache[$class]['methods'][strtolower($method->getName())] = true; - } + if (!is_object($object)) { + if ($isDefinedTest) { + return false; + } - foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { - self::$cache[$class]['properties'][$property->getName()] = true; - } + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { + return null; + } + + throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); } + + $class = get_class($object); */ if (Z_TYPE_P(object) == IS_OBJECT) { char *class_name = NULL; @@ -925,12 +929,11 @@ PHP_FUNCTION(twig_template_get_attributes) /* // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { - if (isset(self::$cache[$class]['properties'][$item]) - || isset($object->$item) || array_key_exists($item, $object) - ) { + if (isset($object->$item) || array_key_exists((string) $item, $object)) { if ($isDefinedTest) { return true; } + if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); } @@ -967,15 +970,19 @@ PHP_FUNCTION(twig_template_get_attributes) } /* // object method + if (!isset(self::$cache[$class]['methods'])) { + self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object))); + } + $lcItem = strtolower($item); if (isset(self::$cache[$class]['methods'][$lcItem])) { - $method = $item; + $method = (string) $item; } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) { $method = 'get'.$item; } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) { $method = 'is'.$item; } elseif (isset(self::$cache[$class]['methods']['__call'])) { - $method = $item; + $method = (string) $item; */ { char *lcItem = TWIG_STRTOLOWER(item, item_len); @@ -1011,11 +1018,14 @@ PHP_FUNCTION(twig_template_get_attributes) if ($isDefinedTest) { return false; } + if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { return null; } - throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object))); + + throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName()); } + if ($isDefinedTest) { return true; } @@ -1067,6 +1077,8 @@ PHP_FUNCTION(twig_template_get_attributes) efree(lcItem); } /* + // useful when calling a template method from a template + // this is not supported but unfortunately heavily used in the Symfony profiler if ($object instanceof Twig_TemplateInterface) { return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset()); } From 21f80622b6c97cc76bbbed0ca72713aa7a5df202 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Wed, 3 Apr 2013 08:48:12 +0200 Subject: [PATCH 14/16] [ext] code cleaning --- ext/twig/twig.c | 88 +++++++++++++++++-------------------------------- 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/ext/twig/twig.c b/ext/twig/twig.c index 14d127bbb..b5645c48c 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -799,7 +799,6 @@ PHP_FUNCTION(twig_template_get_attributes) if ((TWIG_ARRAY_KEY_EXISTS(object, zitem)) || (TWIG_INSTANCE_OF(object, zend_ce_arrayaccess TSRMLS_CC) && TWIG_ISSET_ARRAYOBJECT_ELEMENT(object, zitem TSRMLS_CC)) ) { - zval *ret; if (isDefinedTest) { RETURN_TRUE; @@ -825,7 +824,7 @@ PHP_FUNCTION(twig_template_get_attributes) return null; } */ - if (strcmp("array", type) == 0) { + if (strcmp("array", type) == 0 || Z_TYPE_P(object) != IS_OBJECT) { if (isDefinedTest) { RETURN_FALSE; } @@ -850,11 +849,15 @@ PHP_FUNCTION(twig_template_get_attributes) } else if (Z_TYPE_P(object) == IS_ARRAY) { TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC)); } else { - char *type_name = zend_zval_type_name(object); - Z_ADDREF_P(object); - convert_to_string(object); - TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to access a key (\"%s\") on a %s variable (\"%s\")", item, type_name, Z_STRVAL_P(object)); - zval_ptr_dtor(&object); + char *type_name = zend_zval_type_name(object); + Z_ADDREF_P(object); + convert_to_string(object); + TWIG_RUNTIME_ERROR(template TSRMLS_CC, + (strcmp("array", type) == 0) + ? "Impossible to access a key (\"%s\") on a %s variable (\"%s\")" + : "Impossible to access an attribute (\"%s\") on a %s variable (\"%s\")", + item, type_name, Z_STRVAL_P(object)); + zval_ptr_dtor(&object); } return; } @@ -882,49 +885,31 @@ PHP_FUNCTION(twig_template_get_attributes) RETURN_FALSE; } - if (Z_TYPE_P(object) == IS_ARRAY) { - TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Key \"%s\" for array with keys \"%s\" does not exist", item, TWIG_IMPLODE_ARRAY_KEYS(", ", object TSRMLS_CC)); - } else { - char *type_name = zend_zval_type_name(object); - Z_ADDREF_P(object); - convert_to_string_ex(&object); + char *type_name = zend_zval_type_name(object); + Z_ADDREF_P(object); + convert_to_string_ex(&object); - TWIG_RUNTIME_ERROR(template TSRMLS_CC, - (strcmp("method", type) == 0) - ? "Impossible to invoke a method (\"%s\") on a %s variable (\"%s\")" - : "Impossible to access an attribute (\"%s\") on a %s variable (\"%s\")", - item, type_name, Z_STRVAL_P(object)); + TWIG_RUNTIME_ERROR(template TSRMLS_CC, "Impossible to invoke a method (\"%s\") on a %s variable (\"%s\")", item, type_name, Z_STRVAL_P(object)); + + zval_ptr_dtor(&object); - zval_ptr_dtor(&object); - } return; } /* - if (!is_object($object)) { - if ($isDefinedTest) { - return false; - } - - if ($ignoreStrictCheck || !$this->env->isStrictVariables()) { - return null; - } - - throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName()); - } - $class = get_class($object); */ - if (Z_TYPE_P(object) == IS_OBJECT) { - char *class_name = NULL; + char *class_name = NULL; + zval *tmp_class; - class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC); - tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache" TSRMLS_CC); + class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC); + tmp_self_cache = TWIG_GET_STATIC_PROPERTY(template, "cache" TSRMLS_CC); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC); - if (!TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC)) { - twig_add_class_to_cache(tmp_self_cache, object, class_name TSRMLS_CC); - } - efree(class_name); + if (!tmp_class) { + twig_add_class_to_cache(tmp_self_cache, object, class_name TSRMLS_CC); + tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC); } + efree(class_name); /* // object property @@ -943,16 +928,11 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (strcmp("method", type) != 0) { - zval *tmp_class, *tmp_properties, *tmp_item; - char *class_name = NULL; + zval *tmp_properties, *tmp_item; - class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC); - tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC); tmp_properties = TWIG_GET_ARRAY_ELEMENT(tmp_class, "properties", strlen("properties") TSRMLS_CC); tmp_item = TWIG_GET_ARRAY_ELEMENT(tmp_properties, item, item_len TSRMLS_CC); - efree(class_name); - if (tmp_item || TWIG_HAS_PROPERTY(object, zitem TSRMLS_CC) || TWIG_HAS_DYNAMIC_PROPERTY(object, item, item_len TSRMLS_CC)) { if (isDefinedTest) { RETURN_TRUE; @@ -990,10 +970,8 @@ PHP_FUNCTION(twig_template_get_attributes) char *method = NULL; char *tmp_method_name_get; char *tmp_method_name_is; - zval *tmp_class, *tmp_methods; - char *class_name = NULL; + zval *tmp_methods; - class_name = TWIG_GET_CLASS_NAME(object TSRMLS_CC); lcItem_length = strlen(lcItem); tmp_method_name_get = emalloc(4 + lcItem_length); tmp_method_name_is = emalloc(3 + lcItem_length); @@ -1001,9 +979,7 @@ PHP_FUNCTION(twig_template_get_attributes) sprintf(tmp_method_name_get, "get%s", lcItem); sprintf(tmp_method_name_is, "is%s", lcItem); - tmp_class = TWIG_GET_ARRAY_ELEMENT(tmp_self_cache, class_name, strlen(class_name) TSRMLS_CC); tmp_methods = TWIG_GET_ARRAY_ELEMENT(tmp_class, "methods", strlen("methods") TSRMLS_CC); - efree(class_name); if (TWIG_GET_ARRAY_ELEMENT(tmp_methods, lcItem, lcItem_length TSRMLS_CC)) { method = item; @@ -1068,10 +1044,8 @@ PHP_FUNCTION(twig_template_get_attributes) /* $ret = call_user_func_array(array($object, $method), $arguments); */ - if (Z_TYPE_P(object) == IS_OBJECT) { - ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments TSRMLS_CC); - free_ret = 1; - } + ret = TWIG_CALL_USER_FUNC_ARRAY(object, method, arguments TSRMLS_CC); + free_ret = 1; efree(tmp_method_name_get); efree(tmp_method_name_is); efree(lcItem); @@ -1088,9 +1062,7 @@ PHP_FUNCTION(twig_template_get_attributes) // ret can be null, if e.g. the called method throws an exception if (ret) { if (TWIG_INSTANCE_OF_USERLAND(object, "Twig_TemplateInterface" TSRMLS_CC)) { - if (Z_STRLEN_P(ret) == 0) { - free_ret = 1; - } else { + if (Z_STRLEN_P(ret) != 0) { zval *charset = TWIG_CALL_USER_FUNC_ARRAY(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "getCharset", NULL TSRMLS_CC); TWIG_NEW(return_value, "Twig_Markup", ret, charset TSRMLS_CC); zval_ptr_dtor(&charset); From 46012ce3b1c8d3b82d0285d3e9306c6ede0d0aa2 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Mon, 1 Apr 2013 18:24:17 +0200 Subject: [PATCH 15/16] [ext] improvements --- ext/twig/twig.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/ext/twig/twig.c b/ext/twig/twig.c index b5645c48c..0f8d688c1 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -86,17 +86,12 @@ int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) } switch (Z_TYPE_P(key)) { - case IS_STRING: - return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); - case IS_NULL: return zend_hash_exists(Z_ARRVAL_P(array), "", 1); case IS_BOOL: case IS_DOUBLE: convert_to_long(key); - return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); - case IS_LONG: return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); @@ -275,25 +270,22 @@ zval *TWIG_GET_ARRAY_ELEMENT_ZVAL(zval *class, zval *prop_name TSRMLS_DC) return NULL; } - if (Z_TYPE_P(prop_name) == IS_NULL) { - if (zend_hash_find(HASH_OF(class), "", 1, (void**) &tmp_zval) == SUCCESS) { - return *tmp_zval; - } + switch(Z_TYPE_P(prop_name)) { + case IS_NULL: + zend_hash_find(HASH_OF(class), "", 1, (void**) &tmp_zval); + return *tmp_zval; - } else if (Z_TYPE_P(prop_name) == IS_BOOL || Z_TYPE_P(prop_name) == IS_DOUBLE || Z_TYPE_P(prop_name) == IS_LONG) { - if (Z_TYPE_P(prop_name) != IS_LONG) { - convert_to_long(prop_name); - } + case IS_BOOL: + case IS_DOUBLE: + convert_to_long(prop_name); + case IS_LONG: + zend_hash_index_find(HASH_OF(class), Z_LVAL_P(prop_name), (void **) &tmp_zval); + return *tmp_zval; - if (zend_hash_index_find(HASH_OF(class), Z_LVAL_P(prop_name), (void **) &tmp_zval) == SUCCESS) { - return *tmp_zval; - } - - } else if (Z_TYPE_P(prop_name) == IS_STRING) { - if (zend_symtable_find(HASH_OF(class), Z_STRVAL_P(prop_name), Z_STRLEN_P(prop_name) + 1, (void**) &tmp_zval) == SUCCESS) { - return *tmp_zval; - } - } + case IS_STRING: + zend_symtable_find(HASH_OF(class), Z_STRVAL_P(prop_name), Z_STRLEN_P(prop_name) + 1, (void**) &tmp_zval); + return *tmp_zval; + } return NULL; } @@ -882,7 +874,7 @@ PHP_FUNCTION(twig_template_get_attributes) } */ if (ignoreStrictCheck || !TWIG_CALL_BOOLEAN(TWIG_PROPERTY_CHAR(template, "env" TSRMLS_CC), "isStrictVariables" TSRMLS_CC)) { - RETURN_FALSE; + return; } char *type_name = zend_zval_type_name(object); From 17d6cc7f103fd402dabd332f04153530dea3f245 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Mon, 8 Apr 2013 16:42:05 +0200 Subject: [PATCH 16/16] [ext] fixed tabs --- ext/twig/twig.c | 68 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/ext/twig/twig.c b/ext/twig/twig.c index 0f8d688c1..bc999bd75 100644 --- a/ext/twig/twig.c +++ b/ext/twig/twig.c @@ -78,27 +78,27 @@ ZEND_GET_MODULE(twig) int TWIG_ARRAY_KEY_EXISTS(zval *array, zval *key) { - zval temp; - int result; + zval temp; + int result; - if (Z_TYPE_P(array) != IS_ARRAY) { - return 0; - } + if (Z_TYPE_P(array) != IS_ARRAY) { + return 0; + } - switch (Z_TYPE_P(key)) { - case IS_NULL: - return zend_hash_exists(Z_ARRVAL_P(array), "", 1); + switch (Z_TYPE_P(key)) { + case IS_NULL: + return zend_hash_exists(Z_ARRVAL_P(array), "", 1); - case IS_BOOL: - case IS_DOUBLE: - convert_to_long(key); - case IS_LONG: - return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); + case IS_BOOL: + case IS_DOUBLE: + convert_to_long(key); + case IS_LONG: + return zend_hash_index_exists(Z_ARRVAL_P(array), Z_LVAL_P(key)); - default: - convert_to_string(key); - return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); - } + default: + convert_to_string(key); + return zend_symtable_exists(Z_ARRVAL_P(array), Z_STRVAL_P(key), Z_STRLEN_P(key) + 1); + } } int TWIG_INSTANCE_OF(zval *object, zend_class_entry *interface TSRMLS_DC) @@ -123,23 +123,23 @@ int TWIG_INSTANCE_OF_USERLAND(zval *object, char *interface TSRMLS_DC) zval *TWIG_GET_ARRAYOBJECT_ELEMENT(zval *object, zval *offset TSRMLS_DC) { - zend_class_entry *ce = Z_OBJCE_P(object); - zval *retval; + zend_class_entry *ce = Z_OBJCE_P(object); + zval *retval; if (Z_TYPE_P(object) == IS_OBJECT) { SEPARATE_ARG_IF_REF(offset); zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset); - zval_ptr_dtor(&offset); + zval_ptr_dtor(&offset); - if (!retval) { - if (!EG(exception)) { - zend_error(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); - } - return NULL; - } + if (!retval) { + if (!EG(exception)) { + zend_error(E_ERROR, "Undefined offset for object of type %s used as array", ce->name); + } + return NULL; + } - return retval; + return retval; } return NULL; } @@ -758,13 +758,13 @@ PHP_FUNCTION(twig_template_get_attributes) return; } - // convert the item to a string - ztmpitem = *zitem; - zval_copy_ctor(&ztmpitem); - convert_to_string(&ztmpitem); - item_len = Z_STRLEN(ztmpitem); - item = estrndup(Z_STRVAL(ztmpitem), item_len); - zval_dtor(&ztmpitem); + // convert the item to a string + ztmpitem = *zitem; + zval_copy_ctor(&ztmpitem); + convert_to_string(&ztmpitem); + item_len = Z_STRLEN(ztmpitem); + item = estrndup(Z_STRVAL(ztmpitem), item_len); + zval_dtor(&ztmpitem); if (!type) { type = "any";