Upgrade Twig library

This commit is contained in:
Michael Foster 2013-08-01 15:20:12 -04:00
parent 22f3a95e0e
commit 0fe5528574
133 changed files with 5080 additions and 1386 deletions

View file

@ -13,16 +13,17 @@
/**
* Default base class for compiled templates.
*
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Twig_Template implements Twig_TemplateInterface
{
static protected $cache = array();
protected static $cache = array();
protected $parent;
protected $parents;
protected $env;
protected $blocks;
protected $traits;
/**
* Constructor.
@ -33,6 +34,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
{
$this->env = $env;
$this->blocks = array();
$this->traits = array();
}
/**
@ -43,9 +45,7 @@ abstract class Twig_Template implements Twig_TemplateInterface
abstract public function getTemplateName();
/**
* Returns the Twig environment.
*
* @return Twig_Environment The Twig environment
* {@inheritdoc}
*/
public function getEnvironment()
{
@ -55,10 +55,17 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Returns the parent template.
*
* This method is for internal use only and should never be called
* directly.
*
* @return Twig_TemplateInterface|false The parent template or false if there is no parent
*/
public function getParent(array $context)
{
if (null !== $this->parent) {
return $this->parent;
}
$parent = $this->doGetParent($context);
if (false === $parent) {
return false;
@ -73,33 +80,53 @@ abstract class Twig_Template implements Twig_TemplateInterface
return $this->parents[$parent];
}
abstract protected function doGetParent(array $context);
protected function doGetParent(array $context)
{
return false;
}
public function isTraitable()
{
return true;
}
/**
* Displays a parent block.
*
* This method is for internal use only and should never be called
* directly.
*
* @param string $name The block name to display from the parent
* @param array $context The context
* @param array $blocks The current set of blocks
*/
public function displayParentBlock($name, array $context, array $blocks = array())
{
if (false !== $parent = $this->getParent($context)) {
$name = (string) $name;
if (isset($this->traits[$name])) {
$this->traits[$name][0]->displayBlock($name, $context, $blocks);
} elseif (false !== $parent = $this->getParent($context)) {
$parent->displayBlock($name, $context, $blocks);
} else {
throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
throw new Twig_Error_Runtime(sprintf('The template has no parent and no traits defining the "%s" block', $name), -1, $this->getTemplateName());
}
}
/**
* Displays a block.
*
* This method is for internal use only and should never be called
* directly.
*
* @param string $name The block name to display
* @param array $context The context
* @param array $blocks The current set of blocks
*/
public function displayBlock($name, array $context, array $blocks = array())
{
$name = (string) $name;
if (isset($blocks[$name])) {
$b = $blocks;
unset($b[$name]);
@ -114,6 +141,9 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Renders a parent block.
*
* This method is for internal use only and should never be called
* directly.
*
* @param string $name The block name to render from the parent
* @param array $context The context
* @param array $blocks The current set of blocks
@ -131,6 +161,9 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Renders a block.
*
* This method is for internal use only and should never be called
* directly.
*
* @param string $name The block name to render
* @param array $context The context
* @param array $blocks The current set of blocks
@ -148,19 +181,34 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Returns whether a block exists or not.
*
* This method is for internal use only and should never be called
* directly.
*
* This method does only return blocks defined in the current template
* or defined in "used" traits.
*
* It does not return blocks from parent templates as the parent
* template name can be dynamic, which is only known based on the
* current context.
*
* @param string $name The block name
*
* @return Boolean true if the block exists, false otherwise
*/
public function hasBlock($name)
{
return isset($this->blocks[$name]);
return isset($this->blocks[(string) $name]);
}
/**
* Returns all block names.
*
* This method is for internal use only and should never be called
* directly.
*
* @return array An array of block names
*
* @see hasBlock
*/
public function getBlockNames()
{
@ -170,7 +218,12 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Returns all blocks.
*
* This method is for internal use only and should never be called
* directly.
*
* @return array An array of blocks
*
* @see hasBlock
*/
public function getBlocks()
{
@ -178,28 +231,15 @@ abstract class Twig_Template implements Twig_TemplateInterface
}
/**
* Displays the template with the given context.
*
* @param array $context An array of parameters to pass to the template
* @param array $blocks An array of blocks to pass to the template
* {@inheritdoc}
*/
public function display(array $context, array $blocks = array())
{
try {
$this->doDisplay($context, $blocks);
} catch (Twig_Error $e) {
throw $e;
} catch (Exception $e) {
throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
}
$this->displayWithErrorHandling($this->env->mergeGlobals($context), $blocks);
}
/**
* Renders the template with the given context and returns it as string.
*
* @param array $context An array of parameters to pass to the template
*
* @return string The rendered template
* {@inheritdoc}
*/
public function render(array $context)
{
@ -218,6 +258,28 @@ abstract class Twig_Template implements Twig_TemplateInterface
return ob_get_clean();
}
protected function displayWithErrorHandling(array $context, array $blocks = array())
{
try {
$this->doDisplay($context, $blocks);
} catch (Twig_Error $e) {
if (!$e->getTemplateFile()) {
$e->setTemplateFile($this->getTemplateName());
}
// this is mostly useful for Twig_Error_Loader exceptions
// see Twig_Error_Loader
if (false === $e->getTemplateLine()) {
$e->setTemplateLine(-1);
$e->guess();
}
throw $e;
} catch (Exception $e) {
throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
}
}
/**
* Auto-generated method to display the template with the given context.
*
@ -229,21 +291,30 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Returns a variable from the context.
*
* @param array $context The context
* @param string $item The variable to return from the context
* This method is for internal use only and should never be called
* directly.
*
* This method should not be overridden in a sub-class as this is an
* implementation detail that has been introduced to optimize variable
* access for versions of PHP before 5.4. This is not a way to override
* the way to get a variable value.
*
* @param array $context The context
* @param string $item The variable to return from the context
* @param Boolean $ignoreStrictCheck Whether to ignore the strict variable check or not
*
* @return The content of the context variable
*
* @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
*/
protected function getContext($context, $item)
final protected function getContext($context, $item, $ignoreStrictCheck = false)
{
if (!array_key_exists($item, $context)) {
if (!$this->env->isStrictVariables()) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), -1, $this->getTemplateName());
}
return $context[$item];
@ -252,40 +323,50 @@ abstract class Twig_Template implements Twig_TemplateInterface
/**
* Returns the attribute value for a given array/object.
*
* @param mixed $object The object or array from where to get the item
* @param mixed $item The item to get from the array or object
* @param array $arguments An array of arguments to pass if the item is an object method
* @param string $type The type of attribute (@see Twig_TemplateInterface)
* @param Boolean $isDefinedTest Whether this is only a defined check
* @param mixed $object The object or array from where to get the item
* @param mixed $item The item to get from the array or object
* @param array $arguments An array of arguments to pass if the item is an object method
* @param string $type The type of attribute (@see Twig_TemplateInterface)
* @param Boolean $isDefinedTest Whether this is only a defined check
* @param Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not
*
* @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
*
* @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
*/
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
{
// array
if (Twig_TemplateInterface::METHOD_CALL !== $type) {
if ((is_array($object) && array_key_exists($item, $object))
|| ($object instanceof ArrayAccess && isset($object[$item]))
$arrayItem = is_bool($item) || is_float($item) ? (int) $item : $item;
if ((is_array($object) && array_key_exists($arrayItem, $object))
|| ($object instanceof ArrayAccess && isset($object[$arrayItem]))
) {
if ($isDefinedTest) {
return true;
}
return $object[$item];
return $object[$arrayItem];
}
if (Twig_TemplateInterface::ARRAY_CALL === $type) {
if (Twig_TemplateInterface::ARRAY_CALL === $type || !is_object($object)) {
if ($isDefinedTest) {
return false;
}
if (!$this->env->isStrictVariables()) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return null;
}
if (is_object($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
// array
throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $arrayItem, get_class($object)), -1, $this->getTemplateName());
} elseif (is_array($object)) {
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $arrayItem, implode(', ', array_keys($object))), -1, $this->getTemplateName());
} elseif (Twig_TemplateInterface::ARRAY_CALL === $type) {
throw new Twig_Error_Runtime(sprintf('Impossible to access a key ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
} else {
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
throw new Twig_Error_Runtime(sprintf('Impossible to access an attribute ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
}
}
}
@ -295,32 +376,18 @@ abstract class Twig_Template implements Twig_TemplateInterface
return false;
}
if (!$this->env->isStrictVariables()) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object));
throw new Twig_Error_Runtime(sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object), -1, $this->getTemplateName());
}
// 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'][$item])
|| isset($object->$item) || array_key_exists($item, $object)
) {
if (isset($object->$item) || array_key_exists((string) $item, $object)) {
if ($isDefinedTest) {
return true;
}
@ -334,25 +401,29 @@ abstract class Twig_Template implements Twig_TemplateInterface
}
// object method
if (!isset(self::$cache[$class]['methods'])) {
self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
}
$lcItem = strtolower($item);
if (isset(self::$cache[$class]['methods'][$lcItem])) {
$method = $item;
$method = (string) $item;
} elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
$method = 'get'.$item;
} elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
$method = 'is'.$item;
} elseif (isset(self::$cache[$class]['methods']['__call'])) {
$method = $item;
$method = (string) $item;
} else {
if ($isDefinedTest) {
return false;
}
if (!$this->env->isStrictVariables()) {
if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
return null;
}
throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
}
if ($isDefinedTest) {
@ -365,10 +436,20 @@ abstract class Twig_Template implements Twig_TemplateInterface
$ret = call_user_func_array(array($object, $method), $arguments);
// useful when calling a template method from a template
// this is not supported but unfortunately heavily used in the Symfony profiler
if ($object instanceof Twig_TemplateInterface) {
return new Twig_Markup($ret);
return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset());
}
return $ret;
}
/**
* This method is only useful when testing Twig. Do not use it.
*/
public static function clearCache()
{
self::$cache = array();
}
}