PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/kohana-twig/vendor/Twig-1.0/lib/Twig/Template.php

https://bitbucket.org/sapphiriq/assets-example
PHP | 300 lines | 151 code | 34 blank | 115 comment | 31 complexity | f045f4666c4e623e3b914e0806afdb58 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. // the count variable avoids an infinite loop on
  157. // some Windows configurations where ob_get_level()
  158. // never reaches 0
  159. $count = 100;
  160. while (ob_get_level() && --$count) {
  161. ob_end_clean();
  162. }
  163. throw $e;
  164. }
  165. return ob_get_clean();
  166. }
  167. /**
  168. * Returns a variable from the context.
  169. *
  170. * @param array $context The context
  171. * @param string $item The variable to return from the context
  172. * @param integer $line The line where the variable is get
  173. *
  174. * @param mixed The variable value in the context
  175. *
  176. * @throws Twig_Error_Runtime if the variable does not exist
  177. */
  178. protected function getContext($context, $item, $line = -1)
  179. {
  180. if (!array_key_exists($item, $context)) {
  181. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), $line, $this->getTemplateName());
  182. }
  183. return $context[$item];
  184. }
  185. /**
  186. * Returns the attribute value for a given array/object.
  187. *
  188. * @param mixed $object The object or array from where to get the item
  189. * @param mixed $item The item to get from the array or object
  190. * @param array $arguments An array of arguments to pass if the item is an object method
  191. * @param integer $type The type of attribute (@see Twig_TemplateInterface)
  192. * @param Boolean $noStrictCheck Whether to throw an exception if the item does not exist ot not
  193. * @param integer $line The line where the attribute is get
  194. */
  195. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $noStrictCheck = false, $line = -1)
  196. {
  197. // array
  198. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  199. if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
  200. return $object[$item];
  201. }
  202. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  203. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  204. return null;
  205. }
  206. if (is_object($object)) {
  207. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName());
  208. // array
  209. } else {
  210. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))), $line, $this->getTemplateName());
  211. }
  212. }
  213. }
  214. if (!is_object($object)) {
  215. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  216. return null;
  217. }
  218. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), $line, $this->getTemplateName());
  219. }
  220. // get some information about the object
  221. $class = get_class($object);
  222. if (!isset(self::$cache[$class])) {
  223. $r = new ReflectionClass($class);
  224. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  225. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  226. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  227. }
  228. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  229. self::$cache[$class]['properties'][$property->getName()] = true;
  230. }
  231. }
  232. // object property
  233. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  234. if (isset(self::$cache[$class]['properties'][$item]) || isset($object->$item) || isset(self::$cache[$class]['methods']['__get'])) {
  235. if ($this->env->hasExtension('sandbox')) {
  236. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  237. }
  238. return $object->$item;
  239. }
  240. }
  241. // object method
  242. $lcItem = strtolower($item);
  243. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  244. $method = $item;
  245. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  246. $method = 'get'.$item;
  247. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  248. $method = 'is'.$item;
  249. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  250. $method = $item;
  251. } else {
  252. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  253. return null;
  254. }
  255. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), $line, $this->getTemplateName());
  256. }
  257. if ($this->env->hasExtension('sandbox')) {
  258. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  259. }
  260. $ret = call_user_func_array(array($object, $method), $arguments);
  261. if ($object instanceof Twig_TemplateInterface) {
  262. return new Twig_Markup($ret);
  263. }
  264. return $ret;
  265. }
  266. }