From 7bc07dc132f96027289c994ef3eeb618ebc2ec3d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 21 Nov 2011 10:36:47 +0100 Subject: [PATCH] removed usage of Reflection in Twig_Template::getAttribute() (thanks Pierre Joye for the property tip) --- CHANGELOG | 1 + lib/Twig/Template.php | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 158cfdc..23bb977 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0-RC2 + * removed usage of Reflection in Twig_Template::getAttribute() * added the Twig C extension (replaces the implementation of Twig_Template::getAttribute() by a C function) * added negative timestamp support to the date filter diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 27d28bb..f05c9b0 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -352,22 +352,16 @@ abstract class Twig_Template implements Twig_TemplateInterface throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object)); } - // 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; - } - - foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { - self::$cache[$class]['properties'][$property->getName()] = true; - } - } // object property if (Twig_TemplateInterface::METHOD_CALL !== $type) { + if (!isset(self::$cache[$class]['properties'])) { + foreach ($object as $k => $v) { + self::$cache[$class]['properties'][$k] = true; + } + } + if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item) || array_key_exists($item, $object) ) { @@ -384,6 +378,12 @@ abstract class Twig_Template implements Twig_TemplateInterface } // object method + if (!isset(self::$cache[$class]['methods'])) { + foreach (get_class_methods($object) as $method) { + self::$cache[$class]['methods'][strtolower($method)] = true; + } + } + $lcItem = strtolower($item); if (isset(self::$cache[$class]['methods'][$lcItem])) { $method = $item; -- 1.7.2.5