/application/library/Smarty/Template/Compiled.php

https://gitlab.com/flyhope/Hiblog · PHP · 288 lines · 175 code · 14 blank · 99 comment · 38 complexity · 53991633d593865e9f6c7dbf7f6933eb MD5 · raw file

  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. * @property string $content compiled content
  10. */
  11. class Smarty_Template_Compiled
  12. {
  13. /**
  14. * Compiled Filepath
  15. *
  16. * @var string
  17. */
  18. public $filepath = null;
  19. /**
  20. * Compiled Timestamp
  21. *
  22. * @var integer
  23. */
  24. public $timestamp = null;
  25. /**
  26. * Compiled Existence
  27. *
  28. * @var boolean
  29. */
  30. public $exists = false;
  31. /**
  32. * Compiled Content Loaded
  33. *
  34. * @var boolean
  35. */
  36. public $processed = false;
  37. /**
  38. * Code of recompiled template resource
  39. *
  40. * @var string|null
  41. */
  42. public $code = null;
  43. /**
  44. * create Compiled Object container
  45. */
  46. public function __construct()
  47. {
  48. }
  49. /**
  50. * get a Compiled Object of this source
  51. *
  52. * @param Smarty_Internal_Template $_template template object
  53. *
  54. * @return Smarty_Template_Compiled compiled object
  55. */
  56. static function load($_template)
  57. {
  58. if (!isset($_template->source)) {
  59. $_template->loadSource();
  60. }
  61. // check runtime cache
  62. if (!$_template->source->recompiled && $_template->smarty->resource_caching) {
  63. $_cache_key = $_template->source->unique_resource . '#';
  64. if ($_template->caching) {
  65. $_cache_key .= 'caching#';
  66. }
  67. $_cache_key .= $_template->compile_id;
  68. if (isset($_template->source->compileds[$_cache_key])) {
  69. return $_template->source->compileds[$_cache_key];
  70. }
  71. }
  72. $compiled = new Smarty_Template_Compiled();
  73. if (method_exists($_template->source->handler, 'populateCompiledFilepath')) {
  74. $_template->source->handler->populateCompiledFilepath($compiled, $_template);
  75. } else {
  76. $compiled->populateCompiledFilepath($_template);
  77. }
  78. // runtime cache
  79. if (!$_template->source->recompiled && $_template->smarty->resource_caching) {
  80. $_template->source->compileds[$_cache_key] = $compiled;
  81. }
  82. return $compiled;
  83. }
  84. /**
  85. * populate Compiled Object with compiled filepath
  86. *
  87. * @param Smarty_Internal_Template $_template template object
  88. **/
  89. public function populateCompiledFilepath(Smarty_Internal_Template $_template)
  90. {
  91. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  92. if ($_template->source->isConfig) {
  93. $_flag = '_' . ((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2
  94. + (int) $_template->smarty->config_overwrite * 4);
  95. } else {
  96. $_flag = '_' . ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2);
  97. }
  98. $_filepath = $_template->source->uid . $_flag;
  99. // if use_sub_dirs, break file into directories
  100. if ($_template->smarty->use_sub_dirs) {
  101. $_filepath = substr($_filepath, 0, 2) . DS
  102. . substr($_filepath, 2, 2) . DS
  103. . substr($_filepath, 4, 2) . DS
  104. . $_filepath;
  105. }
  106. $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
  107. if (isset($_compile_id)) {
  108. $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
  109. }
  110. // caching token
  111. if ($_template->caching) {
  112. $_cache = '.cache';
  113. } else {
  114. $_cache = '';
  115. }
  116. $_compile_dir = $_template->smarty->getCompileDir();
  117. // set basename if not specified
  118. $_basename = $_template->source->handler->getBasename($_template->source);
  119. if ($_basename === null) {
  120. $_basename = basename(preg_replace('![^\w\/]+!', '_', $_template->source->name));
  121. }
  122. // separate (optional) basename by dot
  123. if ($_basename) {
  124. $_basename = '.' . $_basename;
  125. }
  126. $this->filepath = $_compile_dir . $_filepath . '.' . $_template->source->type . $_basename . $_cache . '.php';
  127. $this->timestamp = $this->exists = is_file($this->filepath);
  128. if ($this->exists) {
  129. $this->timestamp = @filemtime($this->filepath);
  130. }
  131. }
  132. /**
  133. * load compiled template or compile from source
  134. *
  135. * @param Smarty_Internal_Template $_template
  136. *
  137. * @throws Exception
  138. */
  139. public function process(Smarty_Internal_Template $_template)
  140. {
  141. $_smarty_tpl = $_template;
  142. if ($_template->source->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile) {
  143. $this->compileTemplateSource($_template);
  144. $compileCheck = $_template->smarty->compile_check;
  145. $_template->smarty->compile_check = false;
  146. if ($_template->source->recompiled) {
  147. $level = ob_get_level();
  148. ob_start();
  149. try {
  150. eval("?>" . $this->code);
  151. }
  152. catch (Exception $e) {
  153. while (ob_get_level() > $level) {
  154. ob_end_clean();
  155. }
  156. throw $e;
  157. }
  158. ob_get_clean();
  159. $this->code = null;
  160. } else {
  161. include($_template->compiled->filepath);
  162. }
  163. $_template->smarty->compile_check = $compileCheck;
  164. } else {
  165. include($_template->compiled->filepath);
  166. if ($_template->mustCompile) {
  167. $this->compileTemplateSource($_template);
  168. $compileCheck = $_template->smarty->compile_check;
  169. $_template->smarty->compile_check = false;
  170. include($_template->compiled->filepath);
  171. $_template->smarty->compile_check = $compileCheck;
  172. }
  173. }
  174. $this->unifunc = $_template->properties['unifunc'];
  175. $this->processed = true;
  176. }
  177. /**
  178. * render compiled template code
  179. *
  180. * @param Smarty_Internal_Template $_template
  181. *
  182. * @return string
  183. * @throws Exception
  184. */
  185. public function render(Smarty_Internal_Template $_template)
  186. {
  187. if (!$this->processed) {
  188. $this->process($_template);
  189. }
  190. $_template->properties['unifunc'] = $this->unifunc;
  191. return $_template->getRenderedTemplateCode();
  192. }
  193. /**
  194. * compile template from source
  195. *
  196. * @param Smarty_Internal_Template $_template
  197. *
  198. * @return string
  199. * @throws Exception
  200. */
  201. public function compileTemplateSource(Smarty_Internal_Template $_template)
  202. {
  203. if (!$_template->source->recompiled) {
  204. $_template->properties['file_dependency'] = array();
  205. }
  206. // compile locking
  207. if (!$_template->source->recompiled) {
  208. if ($saved_timestamp = $_template->compiled->timestamp) {
  209. touch($_template->compiled->filepath);
  210. }
  211. }
  212. // call compiler
  213. try {
  214. $code = $_template->compiler->compileTemplate($_template);
  215. }
  216. catch (Exception $e) {
  217. // restore old timestamp in case of error
  218. if (!$_template->source->recompiled && $saved_timestamp) {
  219. touch($_template->compiled->filepath, $saved_timestamp);
  220. }
  221. throw $e;
  222. }
  223. // compiling succeeded
  224. if ($_template->compiler->write_compiled_code) {
  225. // write compiled template
  226. $this->write($_template, $code);
  227. $code = '';
  228. }
  229. // release compiler object to free memory
  230. unset($_template->compiler);
  231. return $code;
  232. }
  233. /**
  234. * Write compiled code by handler
  235. *
  236. * @param Smarty_Internal_Template $_template template object
  237. * @param string $code compiled code
  238. *
  239. * @return boolean success
  240. */
  241. public function write(Smarty_Internal_Template $_template, $code)
  242. {
  243. if (!$_template->source->recompiled) {
  244. $obj = new Smarty_Internal_Write_File();
  245. if ($obj->writeFile($this->filepath, $code, $_template->smarty) === true) {
  246. $this->timestamp = $this->exists = is_file($this->filepath);
  247. if ($this->exists) {
  248. $this->timestamp = @filemtime($this->filepath);
  249. return true;
  250. }
  251. }
  252. return false;
  253. } else {
  254. $this->code = $code;
  255. }
  256. $this->timestamp = time();
  257. $this->exists = true;
  258. return true;
  259. }
  260. /**
  261. * Read compiled content from handler
  262. *
  263. * @param Smarty_Internal_Template $_template template object
  264. *
  265. * @return string content
  266. */
  267. public function read(Smarty_Internal_Template $_template)
  268. {
  269. if (!$_template->source->recompiled) {
  270. return file_get_contents($this->filepath);
  271. }
  272. return isset($this->content) ? $this->content : false;
  273. }
  274. }