/inc/lib/Twig/Template.php

https://bitbucket.org/gerg711/asdfchan · PHP · 374 lines · 200 code · 47 blank · 127 comment · 35 complexity · d49458b108996f76a3f56071f3b71013 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@symfony.com>
  16. */
  17. abstract class Twig_Template implements Twig_TemplateInterface
  18. {
  19. static protected $cache = array();
  20. protected $parents;
  21. protected $env;
  22. protected $blocks;
  23. /**
  24. * Constructor.
  25. *
  26. * @param Twig_Environment $env A Twig_Environment instance
  27. */
  28. public function __construct(Twig_Environment $env)
  29. {
  30. $this->env = $env;
  31. $this->blocks = array();
  32. }
  33. /**
  34. * Returns the template name.
  35. *
  36. * @return string The template name
  37. */
  38. abstract public function getTemplateName();
  39. /**
  40. * Returns the Twig environment.
  41. *
  42. * @return Twig_Environment The Twig environment
  43. */
  44. public function getEnvironment()
  45. {
  46. return $this->env;
  47. }
  48. /**
  49. * Returns the parent template.
  50. *
  51. * @return Twig_TemplateInterface|false The parent template or false if there is no parent
  52. */
  53. public function getParent(array $context)
  54. {
  55. $parent = $this->doGetParent($context);
  56. if (false === $parent) {
  57. return false;
  58. } elseif ($parent instanceof Twig_Template) {
  59. $name = $parent->getTemplateName();
  60. $this->parents[$name] = $parent;
  61. $parent = $name;
  62. } elseif (!isset($this->parents[$parent])) {
  63. $this->parents[$parent] = $this->env->loadTemplate($parent);
  64. }
  65. return $this->parents[$parent];
  66. }
  67. abstract protected function doGetParent(array $context);
  68. /**
  69. * Displays a parent block.
  70. *
  71. * @param string $name The block name to display from the parent
  72. * @param array $context The context
  73. * @param array $blocks The current set of blocks
  74. */
  75. public function displayParentBlock($name, array $context, array $blocks = array())
  76. {
  77. if (false !== $parent = $this->getParent($context)) {
  78. $parent->displayBlock($name, $context, $blocks);
  79. } else {
  80. throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
  81. }
  82. }
  83. /**
  84. * Displays a block.
  85. *
  86. * @param string $name The block name to display
  87. * @param array $context The context
  88. * @param array $blocks The current set of blocks
  89. */
  90. public function displayBlock($name, array $context, array $blocks = array())
  91. {
  92. if (isset($blocks[$name])) {
  93. $b = $blocks;
  94. unset($b[$name]);
  95. call_user_func($blocks[$name], $context, $b);
  96. } elseif (isset($this->blocks[$name])) {
  97. call_user_func($this->blocks[$name], $context, $blocks);
  98. } elseif (false !== $parent = $this->getParent($context)) {
  99. $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
  100. }
  101. }
  102. /**
  103. * Renders a parent block.
  104. *
  105. * @param string $name The block name to render from the parent
  106. * @param array $context The context
  107. * @param array $blocks The current set of blocks
  108. *
  109. * @return string The rendered block
  110. */
  111. public function renderParentBlock($name, array $context, array $blocks = array())
  112. {
  113. ob_start();
  114. $this->displayParentBlock($name, $context, $blocks);
  115. return ob_get_clean();
  116. }
  117. /**
  118. * Renders a block.
  119. *
  120. * @param string $name The block name to render
  121. * @param array $context The context
  122. * @param array $blocks The current set of blocks
  123. *
  124. * @return string The rendered block
  125. */
  126. public function renderBlock($name, array $context, array $blocks = array())
  127. {
  128. ob_start();
  129. $this->displayBlock($name, $context, $blocks);
  130. return ob_get_clean();
  131. }
  132. /**
  133. * Returns whether a block exists or not.
  134. *
  135. * @param string $name The block name
  136. *
  137. * @return Boolean true if the block exists, false otherwise
  138. */
  139. public function hasBlock($name)
  140. {
  141. return isset($this->blocks[$name]);
  142. }
  143. /**
  144. * Returns all block names.
  145. *
  146. * @return array An array of block names
  147. */
  148. public function getBlockNames()
  149. {
  150. return array_keys($this->blocks);
  151. }
  152. /**
  153. * Returns all blocks.
  154. *
  155. * @return array An array of blocks
  156. */
  157. public function getBlocks()
  158. {
  159. return $this->blocks;
  160. }
  161. /**
  162. * Displays the template with the given context.
  163. *
  164. * @param array $context An array of parameters to pass to the template
  165. * @param array $blocks An array of blocks to pass to the template
  166. */
  167. public function display(array $context, array $blocks = array())
  168. {
  169. try {
  170. $this->doDisplay($context, $blocks);
  171. } catch (Twig_Error $e) {
  172. throw $e;
  173. } catch (Exception $e) {
  174. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
  175. }
  176. }
  177. /**
  178. * Renders the template with the given context and returns it as string.
  179. *
  180. * @param array $context An array of parameters to pass to the template
  181. *
  182. * @return string The rendered template
  183. */
  184. public function render(array $context)
  185. {
  186. $level = ob_get_level();
  187. ob_start();
  188. try {
  189. $this->display($context);
  190. } catch (Exception $e) {
  191. while (ob_get_level() > $level) {
  192. ob_end_clean();
  193. }
  194. throw $e;
  195. }
  196. return ob_get_clean();
  197. }
  198. /**
  199. * Auto-generated method to display the template with the given context.
  200. *
  201. * @param array $context An array of parameters to pass to the template
  202. * @param array $blocks An array of blocks to pass to the template
  203. */
  204. abstract protected function doDisplay(array $context, array $blocks = array());
  205. /**
  206. * Returns a variable from the context.
  207. *
  208. * @param array $context The context
  209. * @param string $item The variable to return from the context
  210. *
  211. * @return The content of the context variable
  212. *
  213. * @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
  214. */
  215. protected function getContext($context, $item)
  216. {
  217. if (!array_key_exists($item, $context)) {
  218. if (!$this->env->isStrictVariables()) {
  219. return null;
  220. }
  221. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
  222. }
  223. return $context[$item];
  224. }
  225. /**
  226. * Returns the attribute value for a given array/object.
  227. *
  228. * @param mixed $object The object or array from where to get the item
  229. * @param mixed $item The item to get from the array or object
  230. * @param array $arguments An array of arguments to pass if the item is an object method
  231. * @param string $type The type of attribute (@see Twig_TemplateInterface)
  232. * @param Boolean $isDefinedTest Whether this is only a defined check
  233. */
  234. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
  235. {
  236. // array
  237. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  238. if ((is_array($object) && array_key_exists($item, $object))
  239. || ($object instanceof ArrayAccess && isset($object[$item]))
  240. ) {
  241. if ($isDefinedTest) {
  242. return true;
  243. }
  244. return $object[$item];
  245. }
  246. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  247. if ($isDefinedTest) {
  248. return false;
  249. }
  250. if (!$this->env->isStrictVariables()) {
  251. return null;
  252. }
  253. if (is_object($object)) {
  254. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  255. // array
  256. } else {
  257. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  258. }
  259. }
  260. }
  261. if (!is_object($object)) {
  262. if ($isDefinedTest) {
  263. return false;
  264. }
  265. if (!$this->env->isStrictVariables()) {
  266. return null;
  267. }
  268. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object));
  269. }
  270. // get some information about the object
  271. $class = get_class($object);
  272. if (!isset(self::$cache[$class])) {
  273. $r = new ReflectionClass($class);
  274. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  275. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  276. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  277. }
  278. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  279. self::$cache[$class]['properties'][$property->getName()] = true;
  280. }
  281. }
  282. // object property
  283. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  284. if (isset(self::$cache[$class]['properties'][$item])
  285. || isset($object->$item) || array_key_exists($item, $object)
  286. ) {
  287. if ($isDefinedTest) {
  288. return true;
  289. }
  290. if ($this->env->hasExtension('sandbox')) {
  291. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  292. }
  293. return $object->$item;
  294. }
  295. }
  296. // object method
  297. $lcItem = strtolower($item);
  298. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  299. $method = $item;
  300. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  301. $method = 'get'.$item;
  302. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  303. $method = 'is'.$item;
  304. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  305. $method = $item;
  306. } else {
  307. if ($isDefinedTest) {
  308. return false;
  309. }
  310. if (!$this->env->isStrictVariables()) {
  311. return null;
  312. }
  313. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  314. }
  315. if ($isDefinedTest) {
  316. return true;
  317. }
  318. if ($this->env->hasExtension('sandbox')) {
  319. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  320. }
  321. $ret = call_user_func_array(array($object, $method), $arguments);
  322. if ($object instanceof Twig_TemplateInterface) {
  323. return new Twig_Markup($ret);
  324. }
  325. return $ret;
  326. }
  327. }