From: Fabien Potencier Date: Sat, 12 Jun 2010 15:36:03 +0000 (+0200) Subject: removed Twig_Macro and Twig_Resource as they are not needed anymore X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=300f2b1560f2de57c8e1e444e6e77fd770c1e5ed;p=web%2Fkonrad%2Ftwig.git removed Twig_Macro and Twig_Resource as they are not needed anymore --- diff --git a/lib/Twig/Macro.php b/lib/Twig/Macro.php deleted file mode 100644 index 8f18a15..0000000 --- a/lib/Twig/Macro.php +++ /dev/null @@ -1,13 +0,0 @@ -env = $env; - $this->cache = array(); - } - - public function getEnvironment() - { - return $this->env; - } - - protected function getContext($context, $item) - { - if (!array_key_exists($item, $context)) { - throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item)); - } - - return $context[$item]; - } - - protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY) - { - // array - if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $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 (!$this->env->isStrictVariables()) { - return null; - } - - throw new InvalidArgumentException(sprintf('Key "%s" for array "%s" does not exist.', $item, $object)); - } - } - - if (!is_object($object)) { - if (!$this->env->isStrictVariables()) { - return null; - } - - throw new InvalidArgumentException(sprintf('Item "%s" for "%s" does not exist.', $item, $object)); - } - - // object property - if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) { - if (property_exists($object, $item)) { - if ($this->env->hasExtension('sandbox')) { - $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); - } - - return $object->$item; - } - } - - // 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])) { - $method = $item; - } elseif (isset($this->cache[$class]['get'.$item])) { - $method = 'get'.$item; - } elseif (isset($this->cache[$class]['__call'])) { - $method = $item; - } else { - if (!$this->env->isStrictVariables()) { - return null; - } - - throw new InvalidArgumentException(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object))); - } - - if ($this->env->hasExtension('sandbox')) { - $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); - } - - return call_user_func_array(array($object, $method), $arguments); - } -} diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 17faea2..71819e7 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -9,17 +9,24 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -abstract class Twig_Template extends Twig_Resource implements Twig_TemplateInterface +abstract class Twig_Template implements Twig_TemplateInterface { + protected $env; + protected $cache; protected $blocks; public function __construct(Twig_Environment $env) { - parent::__construct($env); - + $this->env = $env; + $this->cache = array(); $this->blocks = array(); } + public function getEnvironment() + { + return $this->env; + } + protected function getBlock($name, array $context) { return call_user_func($this->blocks[$name][0], $context, array_slice($this->blocks[$name], 1)); @@ -61,4 +68,82 @@ abstract class Twig_Template extends Twig_Resource implements Twig_TemplateInter return ob_get_clean(); } + + protected function getContext($context, $item) + { + if (!array_key_exists($item, $context)) { + throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item)); + } + + return $context[$item]; + } + + protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY) + { + // array + if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $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 (!$this->env->isStrictVariables()) { + return null; + } + + throw new InvalidArgumentException(sprintf('Key "%s" for array "%s" does not exist.', $item, $object)); + } + } + + if (!is_object($object)) { + if (!$this->env->isStrictVariables()) { + return null; + } + + throw new InvalidArgumentException(sprintf('Item "%s" for "%s" does not exist.', $item, $object)); + } + + // object property + if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) { + if (property_exists($object, $item)) { + if ($this->env->hasExtension('sandbox')) { + $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); + } + + return $object->$item; + } + } + + // 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])) { + $method = $item; + } elseif (isset($this->cache[$class]['get'.$item])) { + $method = 'get'.$item; + } elseif (isset($this->cache[$class]['__call'])) { + $method = $item; + } else { + if (!$this->env->isStrictVariables()) { + return null; + } + + throw new InvalidArgumentException(sprintf('Method "%s" for object "%s" does not exist.', $item, get_class($object))); + } + + if ($this->env->hasExtension('sandbox')) { + $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); + } + + return call_user_func_array(array($object, $method), $arguments); + } }