PageRenderTime 27ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/clamz/chemin-du-son
PHP | 323 lines | 162 code | 36 blank | 125 comment | 30 complexity | 35fe54ec7bf3894844c1c60acdc91fc2 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. * Displays the template with the given context.
  145. *
  146. * @param array $context An array of parameters to pass to the template
  147. * @param array $blocks An array of blocks to pass to the template
  148. */
  149. public function display(array $context, array $blocks = array())
  150. {
  151. try {
  152. $this->doDisplay($context, $blocks);
  153. } catch (Twig_Error $e) {
  154. throw $e;
  155. } catch (Exception $e) {
  156. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
  157. }
  158. }
  159. /**
  160. * Renders the template with the given context and returns it as string.
  161. *
  162. * @param array $context An array of parameters to pass to the template
  163. *
  164. * @return string The rendered template
  165. */
  166. public function render(array $context)
  167. {
  168. ob_start();
  169. try {
  170. $this->display($context);
  171. } catch (Exception $e) {
  172. // the count variable avoids an infinite loop on
  173. // some Windows configurations where ob_get_level()
  174. // never reaches 0
  175. $count = 100;
  176. while (ob_get_level() && --$count) {
  177. ob_end_clean();
  178. }
  179. throw $e;
  180. }
  181. return ob_get_clean();
  182. }
  183. /**
  184. * Auto-generated method to display the template with the given context.
  185. *
  186. * @param array $context An array of parameters to pass to the template
  187. * @param array $blocks An array of blocks to pass to the template
  188. */
  189. abstract protected function doDisplay(array $context, array $blocks = array());
  190. /**
  191. * Returns a variable from the context.
  192. *
  193. * @param array $context The context
  194. * @param string $item The variable to return from the context
  195. *
  196. * @param mixed The variable value in the context
  197. *
  198. * @throws Twig_Error_Runtime if the variable does not exist
  199. */
  200. protected function getContext($context, $item)
  201. {
  202. if (!array_key_exists($item, $context)) {
  203. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
  204. }
  205. return $context[$item];
  206. }
  207. /**
  208. * Returns the attribute value for a given array/object.
  209. *
  210. * @param mixed $object The object or array from where to get the item
  211. * @param mixed $item The item to get from the array or object
  212. * @param array $arguments An array of arguments to pass if the item is an object method
  213. * @param integer $type The type of attribute (@see Twig_TemplateInterface)
  214. * @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
  215. */
  216. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $noStrictCheck = false)
  217. {
  218. // array
  219. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  220. if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
  221. return $object[$item];
  222. }
  223. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  224. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  225. return null;
  226. }
  227. if (is_object($object)) {
  228. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  229. // array
  230. } else {
  231. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  232. }
  233. }
  234. }
  235. if (!is_object($object)) {
  236. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  237. return null;
  238. }
  239. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object));
  240. }
  241. // get some information about the object
  242. $class = get_class($object);
  243. if (!isset(self::$cache[$class])) {
  244. $r = new ReflectionClass($class);
  245. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  246. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  247. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  248. }
  249. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  250. self::$cache[$class]['properties'][$property->getName()] = true;
  251. }
  252. }
  253. // object property
  254. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  255. if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item)) {
  256. if ($this->env->hasExtension('sandbox')) {
  257. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  258. }
  259. return $object->$item;
  260. }
  261. }
  262. // object method
  263. $lcItem = strtolower($item);
  264. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  265. $method = $item;
  266. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  267. $method = 'get'.$item;
  268. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  269. $method = 'is'.$item;
  270. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  271. $method = $item;
  272. } else {
  273. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  274. return null;
  275. }
  276. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  277. }
  278. if ($this->env->hasExtension('sandbox')) {
  279. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  280. }
  281. $ret = call_user_func_array(array($object, $method), $arguments);
  282. if ($object instanceof Twig_TemplateInterface) {
  283. return new Twig_Markup($ret);
  284. }
  285. return $ret;
  286. }
  287. }