removed Twig_Macro and Twig_Resource as they are not needed anymore

This commit is contained in:
Fabien Potencier
2010-06-12 17:36:03 +02:00
parent 698b522eda
commit 300f2b1560
3 changed files with 88 additions and 120 deletions
-13
View File
@@ -1,13 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class Twig_Macro extends Twig_Resource
{
}
-104
View File
@@ -1,104 +0,0 @@
<?php
/*
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class Twig_Resource
{
protected $env;
protected $cache;
public function __construct(Twig_Environment $env)
{
$this->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);
}
}
+88 -3
View File
@@ -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);
}
}