Skip to content

Commit

Permalink
removed Twig_Macro and Twig_Resource as they are not needed anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 12, 2010
1 parent 698b522 commit 300f2b1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 120 deletions.
13 changes: 0 additions & 13 deletions lib/Twig/Macro.php

This file was deleted.

104 changes: 0 additions & 104 deletions lib/Twig/Resource.php

This file was deleted.

91 changes: 88 additions & 3 deletions lib/Twig/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -61,4 +68,82 @@ public function render(array $context)

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);
}
}

0 comments on commit 300f2b1

Please sign in to comment.