You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_template_source.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Smarty Resource Data Object
  4. * Meta Data Container for Template Files
  5. *
  6. * @package Smarty
  7. * @subpackage TemplateResources
  8. * @author Rodney Rehm
  9. *
  10. */
  11. class Smarty_Template_Source
  12. {
  13. /**
  14. * Unique Template ID
  15. *
  16. * @var string
  17. */
  18. public $uid = null;
  19. /**
  20. * Template Resource (Smarty_Internal_Template::$template_resource)
  21. *
  22. * @var string
  23. */
  24. public $resource = null;
  25. /**
  26. * Resource Type
  27. *
  28. * @var string
  29. */
  30. public $type = null;
  31. /**
  32. * Resource Name
  33. *
  34. * @var string
  35. */
  36. public $name = null;
  37. /**
  38. * Unique Resource Name
  39. *
  40. * @var string
  41. */
  42. public $unique_resource = null;
  43. /**
  44. * Source Filepath
  45. *
  46. * @var string
  47. */
  48. public $filepath = null;
  49. /**
  50. * Source Timestamp
  51. *
  52. * @var integer
  53. */
  54. public $timestamp = null;
  55. /**
  56. * Source Existence
  57. *
  58. * @var boolean
  59. */
  60. public $exists = false;
  61. /**
  62. * Source File Base name
  63. *
  64. * @var string
  65. */
  66. public $basename = null;
  67. /**
  68. * The Components an extended template is made of
  69. *
  70. * @var \Smarty_Template_Source[]
  71. */
  72. public $components = null;
  73. /**
  74. * Resource Handler
  75. *
  76. * @var \Smarty_Resource
  77. */
  78. public $handler = null;
  79. /**
  80. * Smarty instance
  81. *
  82. * @var Smarty
  83. */
  84. public $smarty = null;
  85. /**
  86. * Resource is source
  87. *
  88. * @var bool
  89. */
  90. public $isConfig = false;
  91. /**
  92. * cache for Smarty_Template_Compiled instances
  93. *
  94. * @var Smarty_Template_Compiled[]
  95. */
  96. public $compileds = array();
  97. /**
  98. * Template source content eventually set by default handler
  99. *
  100. * @var string
  101. */
  102. public $content = null;
  103. /**
  104. * create Source Object container
  105. *
  106. * @param Smarty_Resource $handler Resource Handler this source object communicates with
  107. * @param Smarty $smarty Smarty instance this source object belongs to
  108. * @param string $resource full template_resource
  109. * @param string $type type of resource
  110. * @param string $name resource name
  111. *
  112. */
  113. public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name)
  114. {
  115. $this->handler = $handler; // Note: prone to circular references
  116. $this->smarty = $smarty;
  117. $this->resource = $resource;
  118. $this->type = $type;
  119. $this->name = $name;
  120. }
  121. /**
  122. * initialize Source Object for given resource
  123. * Either [$_template] or [$smarty, $template_resource] must be specified
  124. *
  125. * @param Smarty_Internal_Template $_template template object
  126. * @param Smarty $smarty smarty object
  127. * @param string $template_resource resource identifier
  128. *
  129. * @return Smarty_Template_Source Source Object
  130. * @throws SmartyException
  131. */
  132. public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
  133. $template_resource = null)
  134. {
  135. if ($_template) {
  136. $smarty = $_template->smarty;
  137. $template_resource = $_template->template_resource;
  138. }
  139. if (empty($template_resource)) {
  140. throw new SmartyException('Missing template name');
  141. }
  142. // parse resource_name, load resource handler, identify unique resource name
  143. if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]([\s\S]*)$/', $template_resource, $match)) {
  144. $type = $match[1];
  145. $name = $match[2];
  146. } else {
  147. // no resource given, use default
  148. // or single character before the colon is not a resource type, but part of the filepath
  149. $type = $smarty->default_resource_type;
  150. $name = $template_resource;
  151. }
  152. $handler = isset($smarty->_cache['resource_handlers'][$type]) ?
  153. $smarty->_cache['resource_handlers'][$type] :
  154. Smarty_Resource::load($smarty, $type);
  155. // if resource is not recompiling and resource name is not dotted we can check the source cache
  156. if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$handler->recompiled &&
  157. !(isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'))
  158. ) {
  159. $unique_resource = $handler->buildUniqueResourceName($smarty, $name);
  160. if (isset($smarty->_cache['source_objects'][$unique_resource])) {
  161. return $smarty->_cache['source_objects'][$unique_resource];
  162. }
  163. } else {
  164. $unique_resource = null;
  165. }
  166. // create new source object
  167. $source = new Smarty_Template_Source($handler, $smarty, $template_resource, $type, $name);
  168. $handler->populate($source, $_template);
  169. if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
  170. Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
  171. }
  172. // on recompiling resources we are done
  173. if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$handler->recompiled) {
  174. // may by we have already $unique_resource
  175. $is_relative = false;
  176. if (!isset($unique_resource)) {
  177. $is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') &&
  178. ($type == 'file' ||
  179. (isset($_template->parent->source) && $_template->parent->source->type == 'extends'));
  180. $unique_resource =
  181. $handler->buildUniqueResourceName($smarty, $is_relative ? $source->filepath . $name : $name);
  182. }
  183. $source->unique_resource = $unique_resource;
  184. // save in runtime cache if not relative
  185. if (!$is_relative) {
  186. $smarty->_cache['source_objects'][$unique_resource] = $source;
  187. }
  188. }
  189. return $source;
  190. }
  191. /**
  192. * render the uncompiled source
  193. *
  194. * @param Smarty_Internal_Template $_template template object
  195. *
  196. * @return string
  197. * @throws \Exception
  198. */
  199. public function renderUncompiled(Smarty_Internal_Template $_template)
  200. {
  201. $this->handler->renderUncompiled($_template->source, $_template);
  202. }
  203. /**
  204. * Render uncompiled source
  205. *
  206. * @param \Smarty_Internal_Template $_template
  207. */
  208. public function render(Smarty_Internal_Template $_template)
  209. {
  210. if ($_template->source->handler->uncompiled) {
  211. if ($_template->smarty->debugging) {
  212. $_template->smarty->_debug->start_render($_template);
  213. }
  214. $this->handler->renderUncompiled($_template->source, $_template);
  215. if (isset($_template->parent) && $_template->parent->_objType == 2 && !empty($_template->tpl_function)) {
  216. $_template->parent->tpl_function =
  217. array_merge($_template->parent->tpl_function, $_template->tpl_function);
  218. }
  219. if ($_template->smarty->debugging) {
  220. $_template->smarty->_debug->end_render($_template);
  221. }
  222. }
  223. }
  224. /**
  225. * Get source time stamp
  226. *
  227. * @return int
  228. */
  229. public function getTimeStamp()
  230. {
  231. if (!isset($this->timestamp)) {
  232. $this->handler->populateTimestamp($this);
  233. }
  234. return $this->timestamp;
  235. }
  236. /**
  237. * Get source content
  238. *
  239. * @return string
  240. */
  241. public function getContent()
  242. {
  243. return isset($this->content) ? $this->content : $this->handler->getContent($this);
  244. }
  245. }