PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/app/parsers/Twig/Template.php

https://bitbucket.org/thisislawatts/stacey
PHP | 430 lines | 206 code | 52 blank | 172 comment | 40 complexity | 210a5a6ab343975110e7e9f9183221be 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. protected $traits;
  24. /**
  25. * Constructor.
  26. *
  27. * @param Twig_Environment $env A Twig_Environment instance
  28. */
  29. public function __construct(Twig_Environment $env)
  30. {
  31. $this->env = $env;
  32. $this->blocks = array();
  33. $this->traits = array();
  34. }
  35. /**
  36. * Returns the template name.
  37. *
  38. * @return string The template name
  39. */
  40. abstract public function getTemplateName();
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getEnvironment()
  45. {
  46. return $this->env;
  47. }
  48. /**
  49. * Returns the parent template.
  50. *
  51. * This method is for internal use only and should never be called
  52. * directly.
  53. *
  54. * @return Twig_TemplateInterface|false The parent template or false if there is no parent
  55. */
  56. public function getParent(array $context)
  57. {
  58. $parent = $this->doGetParent($context);
  59. if (false === $parent) {
  60. return false;
  61. } elseif ($parent instanceof Twig_Template) {
  62. $name = $parent->getTemplateName();
  63. $this->parents[$name] = $parent;
  64. $parent = $name;
  65. } elseif (!isset($this->parents[$parent])) {
  66. $this->parents[$parent] = $this->env->loadTemplate($parent);
  67. }
  68. return $this->parents[$parent];
  69. }
  70. abstract protected function doGetParent(array $context);
  71. /**
  72. * Displays a parent block.
  73. *
  74. * This method is for internal use only and should never be called
  75. * directly.
  76. *
  77. * @param string $name The block name to display from the parent
  78. * @param array $context The context
  79. * @param array $blocks The current set of blocks
  80. */
  81. public function displayParentBlock($name, array $context, array $blocks = array())
  82. {
  83. if (isset($this->traits[$name])) {
  84. $this->traits[$name][0]->displayBlock($name, $context, $blocks);
  85. } elseif (false !== $parent = $this->getParent($context)) {
  86. $parent->displayBlock($name, $context, $blocks);
  87. } else {
  88. throw new Twig_Error_Runtime(sprintf('The template has no parent and no traits defining the "%s" block', $name), -1, $this->getTemplateName());
  89. }
  90. }
  91. /**
  92. * Displays a block.
  93. *
  94. * This method is for internal use only and should never be called
  95. * directly.
  96. *
  97. * @param string $name The block name to display
  98. * @param array $context The context
  99. * @param array $blocks The current set of blocks
  100. */
  101. public function displayBlock($name, array $context, array $blocks = array())
  102. {
  103. if (isset($blocks[$name])) {
  104. $b = $blocks;
  105. unset($b[$name]);
  106. call_user_func($blocks[$name], $context, $b);
  107. } elseif (isset($this->blocks[$name])) {
  108. call_user_func($this->blocks[$name], $context, $blocks);
  109. } elseif (false !== $parent = $this->getParent($context)) {
  110. $parent->displayBlock($name, $context, array_merge($this->blocks, $blocks));
  111. }
  112. }
  113. /**
  114. * Renders a parent block.
  115. *
  116. * This method is for internal use only and should never be called
  117. * directly.
  118. *
  119. * @param string $name The block name to render from the parent
  120. * @param array $context The context
  121. * @param array $blocks The current set of blocks
  122. *
  123. * @return string The rendered block
  124. */
  125. public function renderParentBlock($name, array $context, array $blocks = array())
  126. {
  127. ob_start();
  128. $this->displayParentBlock($name, $context, $blocks);
  129. return ob_get_clean();
  130. }
  131. /**
  132. * Renders a block.
  133. *
  134. * This method is for internal use only and should never be called
  135. * directly.
  136. *
  137. * @param string $name The block name to render
  138. * @param array $context The context
  139. * @param array $blocks The current set of blocks
  140. *
  141. * @return string The rendered block
  142. */
  143. public function renderBlock($name, array $context, array $blocks = array())
  144. {
  145. ob_start();
  146. $this->displayBlock($name, $context, $blocks);
  147. return ob_get_clean();
  148. }
  149. /**
  150. * Returns whether a block exists or not.
  151. *
  152. * This method is for internal use only and should never be called
  153. * directly.
  154. *
  155. * This method does only return blocks defined in the current template
  156. * or defined in "used" traits.
  157. *
  158. * It does not return blocks from parent templates as the parent
  159. * template name can be dynamic, which is only known based on the
  160. * current context.
  161. *
  162. * @param string $name The block name
  163. *
  164. * @return Boolean true if the block exists, false otherwise
  165. */
  166. public function hasBlock($name)
  167. {
  168. return isset($this->blocks[$name]);
  169. }
  170. /**
  171. * Returns all block names.
  172. *
  173. * This method is for internal use only and should never be called
  174. * directly.
  175. *
  176. * @return array An array of block names
  177. *
  178. * @see hasBlock
  179. */
  180. public function getBlockNames()
  181. {
  182. return array_keys($this->blocks);
  183. }
  184. /**
  185. * Returns all blocks.
  186. *
  187. * This method is for internal use only and should never be called
  188. * directly.
  189. *
  190. * @return array An array of blocks
  191. *
  192. * @see hasBlock
  193. */
  194. public function getBlocks()
  195. {
  196. return $this->blocks;
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function display(array $context, array $blocks = array())
  202. {
  203. // we don't use array_merge as the context being generally
  204. // bigger than globals, this code is faster.
  205. foreach ($this->env->getGlobals() as $key => $value) {
  206. if (!array_key_exists($key, $context)) {
  207. $context[$key] = $value;
  208. }
  209. }
  210. try {
  211. $this->doDisplay($context, $blocks);
  212. } catch (Twig_Error $e) {
  213. throw $e;
  214. } catch (Exception $e) {
  215. throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e);
  216. }
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function render(array $context)
  222. {
  223. $level = ob_get_level();
  224. ob_start();
  225. try {
  226. $this->display($context);
  227. } catch (Exception $e) {
  228. while (ob_get_level() > $level) {
  229. ob_end_clean();
  230. }
  231. throw $e;
  232. }
  233. return ob_get_clean();
  234. }
  235. /**
  236. * Auto-generated method to display the template with the given context.
  237. *
  238. * @param array $context An array of parameters to pass to the template
  239. * @param array $blocks An array of blocks to pass to the template
  240. */
  241. abstract protected function doDisplay(array $context, array $blocks = array());
  242. /**
  243. * Returns a variable from the context.
  244. *
  245. * @param array $context The context
  246. * @param string $item The variable to return from the context
  247. * @param Boolean $ignoreStrictCheck Whether to ignore the strict variable check or not
  248. *
  249. * @return The content of the context variable
  250. *
  251. * @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode
  252. */
  253. protected function getContext($context, $item, $ignoreStrictCheck = false)
  254. {
  255. if (!array_key_exists($item, $context)) {
  256. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  257. return null;
  258. }
  259. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item));
  260. }
  261. return $context[$item];
  262. }
  263. /**
  264. * Returns the attribute value for a given array/object.
  265. *
  266. * @param mixed $object The object or array from where to get the item
  267. * @param mixed $item The item to get from the array or object
  268. * @param array $arguments An array of arguments to pass if the item is an object method
  269. * @param string $type The type of attribute (@see Twig_TemplateInterface)
  270. * @param Boolean $isDefinedTest Whether this is only a defined check
  271. * @param Boolean $ignoreStrictCheck Whether to ignore the strict attribute check or not
  272. *
  273. * @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
  274. *
  275. * @throws Twig_Error_Runtime if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
  276. */
  277. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
  278. {
  279. $item = (string) $item;
  280. // Stacey-specific
  281. // If the attribute is a string path, then we need to pass it through the asset factory to create its page variables
  282. if (is_string($object)) $object =& AssetFactory::get($object);;
  283. // array
  284. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  285. if ((is_array($object) && array_key_exists($item, $object))
  286. || ($object instanceof ArrayAccess && isset($object[$item]))
  287. ) {
  288. if ($isDefinedTest) {
  289. return true;
  290. }
  291. return $object[$item];
  292. }
  293. if (Twig_TemplateInterface::ARRAY_CALL === $type) {
  294. if ($isDefinedTest) {
  295. return false;
  296. }
  297. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  298. return null;
  299. }
  300. if (is_object($object)) {
  301. throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object)));
  302. // array
  303. } else {
  304. throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object))));
  305. }
  306. }
  307. }
  308. if (!is_object($object)) {
  309. if ($isDefinedTest) {
  310. return false;
  311. }
  312. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  313. return null;
  314. }
  315. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, is_array($object) ? 'Array' : $object));
  316. }
  317. $class = get_class($object);
  318. // object property
  319. if (Twig_TemplateInterface::METHOD_CALL !== $type) {
  320. /* apparently, this is not needed as this is already covered by the array_key_exists() call below
  321. if (!isset(self::$cache[$class]['properties'])) {
  322. foreach (get_object_vars($object) as $k => $v) {
  323. self::$cache[$class]['properties'][$k] = true;
  324. }
  325. }
  326. */
  327. if (isset($object->$item) || array_key_exists($item, $object)) {
  328. if ($isDefinedTest) {
  329. return true;
  330. }
  331. if ($this->env->hasExtension('sandbox')) {
  332. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  333. }
  334. return $object->$item;
  335. }
  336. }
  337. // object method
  338. if (!isset(self::$cache[$class]['methods'])) {
  339. self::$cache[$class]['methods'] = array_change_key_case(array_flip(get_class_methods($object)));
  340. }
  341. $lcItem = strtolower($item);
  342. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  343. $method = $item;
  344. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  345. $method = 'get'.$item;
  346. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  347. $method = 'is'.$item;
  348. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  349. $method = $item;
  350. } else {
  351. if ($isDefinedTest) {
  352. return false;
  353. }
  354. if ($ignoreStrictCheck || !$this->env->isStrictVariables()) {
  355. return null;
  356. }
  357. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)));
  358. }
  359. if ($isDefinedTest) {
  360. return true;
  361. }
  362. if ($this->env->hasExtension('sandbox')) {
  363. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  364. }
  365. $ret = call_user_func_array(array($object, $method), $arguments);
  366. if ($object instanceof Twig_TemplateInterface) {
  367. return new Twig_Markup($ret);
  368. }
  369. return $ret;
  370. }
  371. /**
  372. * This method is only useful when testing Twig. Do not use it.
  373. */
  374. static public function clearCache()
  375. {
  376. self::$cache = array();
  377. }
  378. }