/src/vendor/twig/lib/Twig/Template.php

https://github.com/jackbravo/symfony-sandbox · PHP · 293 lines · 148 code · 34 blank · 111 comment · 28 complexity · 56d5c5f8869f5fe3f313a0c41de0b6e7 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Default base class for compiled templates.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. abstract class Twig_Template implements Twig_TemplateInterface
  18. {
  19. static protected $cache = array();
  20. protected $env;
  21. protected $blocks;
  22. /**
  23. * Constructor.
  24. *
  25. * @param Twig_Environment $env A Twig_Environment instance
  26. */
  27. public function __construct(Twig_Environment $env)
  28. {
  29. $this->env = $env;
  30. $this->blocks = array();
  31. }
  32. /**
  33. * Returns the template name.
  34. *
  35. * @return string The template name
  36. */
  37. public function getTemplateName()
  38. {
  39. return null;
  40. }
  41. /**
  42. * Returns the Twig environment.
  43. *
  44. * @return Twig_Environment The Twig environment
  45. */
  46. public function getEnvironment()
  47. {
  48. return $this->env;
  49. }
  50. /**
  51. * Returns the parent template.
  52. *
  53. * @return Twig_TemplateInterface|false The parent template or false if there is no parent
  54. */
  55. public function getParent(array $context)
  56. {
  57. return false;
  58. }
  59. /**
  60. * Displays a parent block.
  61. *
  62. * @param string $name The block name to display from the parent
  63. * @param array $context The context
  64. * @param array $blocks The current set of blocks
  65. */
  66. public function displayParentBlock($name, array $context, array $blocks = array())
  67. {
  68. if (false !== $parent = $this->getParent($context)) {
  69. $parent->displayBlock($name, $context, $blocks);
  70. } else {
  71. throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
  72. }
  73. }
  74. /**
  75. * Displays a block.
  76. *
  77. * @param string $name The block name to display
  78. * @param array $context The context
  79. * @param array $blocks The current set of blocks
  80. */
  81. public function displayBlock($name, array $context, array $blocks = array())
  82. {
  83. if (isset($blocks[$name])) {
  84. $b = $blocks;
  85. unset($b[$name]);
  86. call_user_func($blocks[$name], $context, $b);
  87. } elseif (isset($this->blocks[$name])) {
  88. call_user_func($this->blocks[$name], $context, $blocks);
  89. } elseif (false !== $parent = $this->getParent($context)) {
  90. $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
  91. }
  92. }
  93. /**
  94. * Renders a parent block.
  95. *
  96. * @param string $name The block name to render from the parent
  97. * @param array $context The context
  98. * @param array $blocks The current set of blocks
  99. *
  100. * @return string The rendered block
  101. */
  102. public function renderParentBlock($name, array $context, array $blocks = array())
  103. {
  104. ob_start();
  105. $this->displayParentBlock($name, $context, $blocks);
  106. return new Twig_Markup(ob_get_clean());
  107. }
  108. /**
  109. * Renders a block.
  110. *
  111. * @param string $name The block name to render
  112. * @param array $context The context
  113. * @param array $blocks The current set of blocks
  114. *
  115. * @return string The rendered block
  116. */
  117. public function renderBlock($name, array $context, array $blocks = array())
  118. {
  119. ob_start();
  120. $this->displayBlock($name, $context, $blocks);
  121. return new Twig_Markup(ob_get_clean());
  122. }
  123. /**
  124. * Returns whether a block exists or not.
  125. *
  126. * @param string $name The block name
  127. *
  128. * @return Boolean true if the block exists, false otherwise
  129. */
  130. public function hasBlock($name)
  131. {
  132. return isset($this->blocks[$name]);
  133. }
  134. /**
  135. * Returns all block names.
  136. *
  137. * @return array An array of block names
  138. */
  139. public function getBlockNames()
  140. {
  141. return array_keys($this->blocks);
  142. }
  143. /**
  144. * Renders the template with the given context and returns it as string.
  145. *
  146. * @param array $context An array of parameters to pass to the template
  147. *
  148. * @return string The rendered template
  149. */
  150. public function render(array $context)
  151. {
  152. ob_start();
  153. try {
  154. $this->display($context);
  155. } catch (Exception $e) {
  156. ob_end_clean();
  157. throw $e;
  158. }
  159. return ob_get_clean();
  160. }
  161. /**
  162. * Returns a variable from the context.
  163. *
  164. * @param array $context The context
  165. * @param string $item The variable to return from the context
  166. * @param integer $line The line where the variable is get
  167. *
  168. * @param mixed The variable value in the context
  169. *
  170. * @throws Twig_Error_Runtime if the variable does not exist
  171. */
  172. protected function getContext($context, $item, $line = -1)
  173. {
  174. if (!array_key_exists($item, $context)) {
  175. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), $line, $this->getTemplateName());
  176. }
  177. return $context[$item];
  178. }
  179. /**
  180. * Returns the attribute value for a given array/object.
  181. *
  182. * @param mixed $object The object or array from where to get the item
  183. * @param mixed $item The item to get from the array or object
  184. * @param array $arguments An array of arguments to pass if the item is an object method
  185. * @param integer $type The type of attribute (@see Twig_Node_Expression_GetAttr)
  186. * @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
  187. */
  188. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false)
  189. {
  190. // array
  191. if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
  192. if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
  193. return $object[$item];
  194. }
  195. if (Twig_Node_Expression_GetAttr::TYPE_ARRAY === $type) {
  196. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  197. return null;
  198. }
  199. if (is_object($object)) {
  200. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
  201. // array
  202. } else {
  203. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), -1, $this->getTemplateName());
  204. }
  205. }
  206. }
  207. if (!is_object($object)) {
  208. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  209. return null;
  210. }
  211. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), -1, $this->getTemplateName());
  212. }
  213. // get some information about the object
  214. $class = get_class($object);
  215. if (!isset(self::$cache[$class])) {
  216. $r = new ReflectionClass($class);
  217. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  218. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  219. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  220. }
  221. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  222. self::$cache[$class]['properties'][strtolower($property->getName())] = true;
  223. }
  224. }
  225. // object property
  226. if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
  227. if (isset(self::$cache[$class]['properties'][strtolower($item)]) || isset($object->$item)) {
  228. if ($this->env->hasExtension('sandbox')) {
  229. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  230. }
  231. return $object->$item;
  232. }
  233. }
  234. // object method
  235. $lcItem = strtolower($item);
  236. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  237. $method = $item;
  238. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  239. $method = 'get'.$item;
  240. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  241. $method = 'is'.$item;
  242. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  243. $method = $item;
  244. } else {
  245. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  246. return null;
  247. }
  248. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
  249. }
  250. if ($this->env->hasExtension('sandbox')) {
  251. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  252. }
  253. $ret = call_user_func_array(array($object, $method), $arguments);
  254. if ($object instanceof Twig_TemplateInterface) {
  255. return new Twig_Markup($ret);
  256. }
  257. return $ret;
  258. }
  259. }