PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/twig/lib/Twig/Template.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 177 lines | 129 code | 28 blank | 20 comment | 26 complexity | 5a2f70fc8cefd1429cabeca3fb8d0e1b MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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. abstract class Twig_Template implements Twig_TemplateInterface
  12. {
  13. static protected $cache = array();
  14. protected $env;
  15. protected $blocks;
  16. public function __construct(Twig_Environment $env)
  17. {
  18. $this->env = $env;
  19. $this->blocks = array();
  20. }
  21. public function getTemplateName()
  22. {
  23. return 'n/a';
  24. }
  25. public function getEnvironment()
  26. {
  27. return $this->env;
  28. }
  29. public function getParent(array $context)
  30. {
  31. return false;
  32. }
  33. public function getParentBlock($name, array $context, array $blocks = array())
  34. {
  35. if (false !== $parent = $this->getParent($context)) {
  36. return $parent->getBlock($name, $context, $blocks);
  37. } else {
  38. throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName());
  39. }
  40. }
  41. public function getBlock($name, array $context, array $blocks = array())
  42. {
  43. if (isset($blocks[$name])) {
  44. $b = $blocks;
  45. unset($b[$name]);
  46. return call_user_func($blocks[$name], $context, $b);
  47. } elseif (isset($this->blocks[$name])) {
  48. return call_user_func($this->blocks[$name], $context, $blocks);
  49. }
  50. if (false !== $parent = $this->getParent($context)) {
  51. return $parent->getBlock($name, $context, array_merge($this->blocks, $blocks));
  52. }
  53. }
  54. public function hasBlock($name)
  55. {
  56. return isset($this->blocks[$name]);
  57. }
  58. public function getBlockNames()
  59. {
  60. return array_keys($this->blocks);
  61. }
  62. /**
  63. * Renders the template with the given context and returns it as string.
  64. *
  65. * @param array $context An array of parameters to pass to the template
  66. *
  67. * @return string The rendered template
  68. */
  69. public function render(array $context)
  70. {
  71. ob_start();
  72. try {
  73. $this->display($context);
  74. } catch (Exception $e) {
  75. ob_end_clean();
  76. throw $e;
  77. }
  78. return ob_get_clean();
  79. }
  80. protected function getContext($context, $item, $line = -1)
  81. {
  82. if (!array_key_exists($item, $context)) {
  83. throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item), $line, $this->getTemplateName());
  84. }
  85. return $context[$item];
  86. }
  87. protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY, $noStrictCheck = false)
  88. {
  89. // array
  90. if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
  91. if ((is_array($object) || is_object($object) && $object instanceof ArrayAccess) && isset($object[$item])) {
  92. return $object[$item];
  93. }
  94. if (Twig_Node_Expression_GetAttr::TYPE_ARRAY === $type) {
  95. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  96. return null;
  97. }
  98. throw new Twig_Error_Runtime(sprintf('Key "%s" for array "%s" does not exist', $item, $object), -1, $this->getTemplateName());
  99. }
  100. }
  101. if (!is_object($object)) {
  102. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  103. return null;
  104. }
  105. throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object), -1, $this->getTemplateName());
  106. }
  107. // get some information about the object
  108. $class = get_class($object);
  109. if (!isset(self::$cache[$class])) {
  110. $r = new ReflectionClass($class);
  111. self::$cache[$class] = array('methods' => array(), 'properties' => array());
  112. foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
  113. self::$cache[$class]['methods'][strtolower($method->getName())] = true;
  114. }
  115. foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
  116. self::$cache[$class]['properties'][strtolower($property->getName())] = true;
  117. }
  118. }
  119. // object property
  120. if (Twig_Node_Expression_GetAttr::TYPE_METHOD !== $type) {
  121. if (isset(self::$cache[$class]['properties'][strtolower($item)]) || isset($object->$item)) {
  122. if ($this->env->hasExtension('sandbox')) {
  123. $this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item);
  124. }
  125. return $object->$item;
  126. }
  127. }
  128. // object method
  129. $lcItem = strtolower($item);
  130. if (isset(self::$cache[$class]['methods'][$lcItem])) {
  131. $method = $item;
  132. } elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) {
  133. $method = 'get'.$item;
  134. } elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) {
  135. $method = 'is'.$item;
  136. } elseif (isset(self::$cache[$class]['methods']['__call'])) {
  137. $method = $item;
  138. } else {
  139. if (!$this->env->isStrictVariables() || $noStrictCheck) {
  140. return null;
  141. }
  142. throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object)), -1, $this->getTemplateName());
  143. }
  144. if ($this->env->hasExtension('sandbox')) {
  145. $this->env->getExtension('sandbox')->checkMethodAllowed($object, $method);
  146. }
  147. return call_user_func_array(array($object, $method), $arguments);
  148. }
  149. }