/php/extlib/smarty/libs/sysplugins/smarty_internal_runtime_updatecache.php

https://github.com/usualoma/movabletype · PHP · 183 lines · 124 code · 5 blank · 54 comment · 19 complexity · d905543c3be641b18306f6706efa827e MD5 · raw file

  1. <?php
  2. /**
  3. * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. **/
  9. class Smarty_Internal_Runtime_UpdateCache
  10. {
  11. /**
  12. * check client side cache
  13. *
  14. * @param \Smarty_Template_Cached $cached
  15. * @param Smarty_Internal_Template $_template
  16. * @param string $content
  17. */
  18. public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
  19. {
  20. }
  21. /**
  22. * Cache was invalid , so render from compiled and write to cache
  23. *
  24. * @param \Smarty_Template_Cached $cached
  25. * @param \Smarty_Internal_Template $_template
  26. * @param $no_output_filter
  27. *
  28. * @throws \Exception
  29. */
  30. public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
  31. {
  32. ob_start();
  33. if (!isset($_template->compiled)) {
  34. $_template->loadCompiled();
  35. }
  36. $_template->compiled->render($_template);
  37. if ($_template->smarty->debugging) {
  38. $_template->smarty->_debug->start_cache($_template);
  39. }
  40. $this->removeNoCacheHash($cached, $_template, $no_output_filter);
  41. $compile_check = (int)$_template->compile_check;
  42. $_template->compile_check = Smarty::COMPILECHECK_OFF;
  43. if ($_template->_isSubTpl()) {
  44. $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
  45. }
  46. if (!$_template->cached->processed) {
  47. $_template->cached->process($_template, true);
  48. }
  49. $_template->compile_check = $compile_check;
  50. $cached->getRenderedTemplateCode($_template);
  51. if ($_template->smarty->debugging) {
  52. $_template->smarty->_debug->end_cache($_template);
  53. }
  54. }
  55. /**
  56. * Sanitize content and write it to cache resource
  57. *
  58. * @param \Smarty_Template_Cached $cached
  59. * @param Smarty_Internal_Template $_template
  60. * @param bool $no_output_filter
  61. *
  62. * @throws \SmartyException
  63. */
  64. public function removeNoCacheHash(
  65. Smarty_Template_Cached $cached,
  66. Smarty_Internal_Template $_template,
  67. $no_output_filter
  68. ) {
  69. $php_pattern = '/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/';
  70. $content = ob_get_clean();
  71. $hash_array = $cached->hashes;
  72. $hash_array[ $_template->compiled->nocache_hash ] = true;
  73. $hash_array = array_keys($hash_array);
  74. $nocache_hash = '(' . implode('|', $hash_array) . ')';
  75. $_template->cached->has_nocache_code = false;
  76. // get text between non-cached items
  77. $cache_split =
  78. preg_split(
  79. "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
  80. $content
  81. );
  82. // get non-cached items
  83. preg_match_all(
  84. "!/\*%%SmartyNocache:{$nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$nocache_hash}%%\*/!s",
  85. $content,
  86. $cache_parts
  87. );
  88. $content = '';
  89. // loop over items, stitch back together
  90. foreach ($cache_split as $curr_idx => $curr_split) {
  91. if (preg_match($php_pattern, $curr_split)) {
  92. // escape PHP tags in template content
  93. $php_split = preg_split(
  94. $php_pattern,
  95. $curr_split
  96. );
  97. preg_match_all(
  98. $php_pattern,
  99. $curr_split,
  100. $php_parts
  101. );
  102. foreach ($php_split as $idx_php => $curr_php) {
  103. $content .= $curr_php;
  104. if (isset($php_parts[ 0 ][ $idx_php ])) {
  105. $content .= "<?php echo '{$php_parts[ 1 ][ $idx_php ]}'; ?>\n";
  106. }
  107. }
  108. } else {
  109. $content .= $curr_split;
  110. }
  111. if (isset($cache_parts[ 0 ][ $curr_idx ])) {
  112. $_template->cached->has_nocache_code = true;
  113. $content .= $cache_parts[ 2 ][ $curr_idx ];
  114. }
  115. }
  116. if (!$no_output_filter && !$_template->cached->has_nocache_code
  117. && (isset($_template->smarty->autoload_filters[ 'output' ])
  118. || isset($_template->smarty->registered_filters[ 'output' ]))
  119. ) {
  120. $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
  121. }
  122. // write cache file content
  123. $this->writeCachedContent($_template, $content);
  124. }
  125. /**
  126. * Writes the content to cache resource
  127. *
  128. * @param Smarty_Internal_Template $_template
  129. * @param string $content
  130. *
  131. * @return bool
  132. */
  133. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  134. {
  135. if ($_template->source->handler->recompiled || !$_template->caching
  136. ) {
  137. // don't write cache file
  138. return false;
  139. }
  140. if (!isset($_template->cached)) {
  141. $_template->loadCached();
  142. }
  143. $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
  144. return $this->write($_template, $content);
  145. }
  146. /**
  147. * Write this cache object to handler
  148. *
  149. * @param Smarty_Internal_Template $_template template object
  150. * @param string $content content to cache
  151. *
  152. * @return bool success
  153. */
  154. public function write(Smarty_Internal_Template $_template, $content)
  155. {
  156. if (!$_template->source->handler->recompiled) {
  157. $cached = $_template->cached;
  158. if ($cached->handler->writeCachedContent($_template, $content)) {
  159. $cached->content = null;
  160. $cached->timestamp = time();
  161. $cached->exists = true;
  162. $cached->valid = true;
  163. $cached->cache_lifetime = $_template->cache_lifetime;
  164. $cached->processed = false;
  165. if ($_template->smarty->cache_locking) {
  166. $cached->handler->releaseLock($_template->smarty, $cached);
  167. }
  168. return true;
  169. }
  170. $cached->content = null;
  171. $cached->timestamp = false;
  172. $cached->exists = false;
  173. $cached->valid = false;
  174. $cached->processed = false;
  175. }
  176. return false;
  177. }
  178. }