From: Fabien Potencier Date: Tue, 29 Jun 2010 05:26:49 +0000 (+0200) Subject: fixed Twig_Template::getAttribute() for protected properties (closes #80) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=74dc1596df9636a1b18703f4388ecbe473ef39ef;p=web%2Fkonrad%2Ftwig.git fixed Twig_Template::getAttribute() for protected properties (closes #80) --- diff --git a/CHANGELOG b/CHANGELOG index 24153ad..981832c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ * 0.9.9 + * fixed Twig_Template::getAttribute() for protected properties + * 0.9.8 (2010-06-28) Backward incompatibilities: diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 84d8ae5..cafccc0 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -103,9 +103,23 @@ abstract class Twig_Template implements Twig_TemplateInterface throw new InvalidArgumentException(sprintf('Item "%s" for "%s" does not exist.', $item, $object)); } + // get some information about the object + $class = get_class($object); + if (!isset($this->cache[$class])) { + $r = new ReflectionClass($class); + $this->cache[$class] = array(); + foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) as $method) { + $this->cache[$class]['methods'][strtolower($method->getName())] = true; + } + + foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { + $this->cache[$class]['properties'][strtolower($property->getName())] = true; + } + } + // object property if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) { - if (property_exists($object, $item) || isset($object->$item)) { + if (isset($this->cache[$class]['properties'][$item]) || isset($object->$item)) { if ($this->env->hasExtension('sandbox')) { $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); } @@ -115,22 +129,12 @@ abstract class Twig_Template implements Twig_TemplateInterface } // object method - $class = get_class($object); - - if (!isset($this->cache[$class])) { - $r = new ReflectionClass($class); - $this->cache[$class] = array(); - foreach ($r->getMethods(ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_FINAL) as $method) { - $this->cache[$class][strtolower($method->getName())] = true; - } - } - $item = strtolower($item); - if (isset($this->cache[$class][$item])) { + if (isset($this->cache[$class]['methods'][$item])) { $method = $item; - } elseif (isset($this->cache[$class]['get'.$item])) { + } elseif (isset($this->cache[$class]['methods']['get'.$item])) { $method = 'get'.$item; - } elseif (isset($this->cache[$class]['__call'])) { + } elseif (isset($this->cache[$class]['methods']['__call'])) { $method = $item; } else { if (!$this->env->isStrictVariables()) { diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index d8a3157..92b6f6f 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -42,6 +42,8 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase array('bar', $object, 'bar', array(), $methodType), array('bar', $object, 'getBar', array(), $methodType), array('foobar', $object, 'foobar', array(), $methodType), + array('babar', $object, 'babar', array(), $methodType), + array('babarStatic', $object, 'babarStatic', array(), $methodType), array('__call_baz', $objectMagic, 'baz', array(), $methodType), // ANY @@ -78,6 +80,18 @@ class Twig_TemplateObject { public $foo = 'foo'; public $null = null; + protected $babar = 'babar...'; + static protected $babarStatic = 'babarStatic...'; + + static public function getBabarStatic() + { + return 'babarStatic'; + } + + public function getBabar() + { + return 'babar'; + } public function getNull() {