4 * Inline Runtime Methods render, setSourceByUid, setupSubTemplate
7 * @subpackage PluginsInternal
11 class Smarty_Internal_Runtime_UpdateCache
14 * check client side cache
16 * @param \Smarty_Template_Cached $cached
17 * @param Smarty_Internal_Template $_template
18 * @param string $content
20 public function cacheModifiedCheck(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
25 * Sanitize content and write it to cache resource
27 * @param \Smarty_Template_Cached $cached
28 * @param Smarty_Internal_Template $_template
29 * @param bool $no_output_filter
31 * @throws \SmartyException
33 public function removeNoCacheHash(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template,
36 $content = ob_get_clean();
37 unset($cached->hashes[ $_template->compiled->nocache_hash ]);
38 if (!empty($cached->hashes)) {
39 $hash_array = array();
40 foreach ($cached->hashes as $hash => $foo) {
41 $hash_array[] = "/{$hash}/";
43 $content = preg_replace($hash_array, $_template->compiled->nocache_hash, $content);
45 $_template->cached->has_nocache_code = false;
46 // get text between non-cached items
48 preg_split("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
50 // get non-cached items
51 preg_match_all("!/\*%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%\*/!s",
52 $content, $cache_parts);
54 // loop over items, stitch back together
55 foreach ($cache_split as $curr_idx => $curr_split) {
56 // escape PHP tags in template content
57 $content .= preg_replace('/(<%|%>|<\?php|<\?|\?>|<script\s+language\s*=\s*[\"\']?\s*php\s*[\"\']?\s*>)/',
58 "<?php echo '\$1'; ?>\n", $curr_split);
59 if (isset($cache_parts[ 0 ][ $curr_idx ])) {
60 $_template->cached->has_nocache_code = true;
61 $content .= $cache_parts[ 1 ][ $curr_idx ];
64 if (!$no_output_filter && !$_template->cached->has_nocache_code &&
65 (isset($_template->smarty->autoload_filters[ 'output' ]) ||
66 isset($_template->smarty->registered_filters[ 'output' ]))
68 $content = $_template->smarty->ext->_filterHandler->runFilter('output', $content, $_template);
70 // write cache file content
71 $this->writeCachedContent($cached, $_template, $content);
75 * Cache was invalid , so render from compiled and write to cache
77 * @param \Smarty_Template_Cached $cached
78 * @param \Smarty_Internal_Template $_template
79 * @param $no_output_filter
83 public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
86 if (!isset($_template->compiled)) {
87 $_template->loadCompiled();
89 $_template->compiled->render($_template);
90 if ($_template->smarty->debugging) {
91 $_template->smarty->_debug->start_cache($_template);
93 $this->removeNoCacheHash($cached, $_template, $no_output_filter);
94 $compile_check = $_template->smarty->compile_check;
95 $_template->smarty->compile_check = false;
96 if ($_template->_isSubTpl()) {
97 $_template->compiled->unifunc = $_template->parent->compiled->unifunc;
99 if (!$_template->cached->processed) {
100 $_template->cached->process($_template, true);
102 $_template->smarty->compile_check = $compile_check;
103 $cached->getRenderedTemplateCode($_template);
104 if ($_template->smarty->debugging) {
105 $_template->smarty->_debug->end_cache($_template);
110 * Writes the content to cache resource
112 * @param \Smarty_Template_Cached $cached
113 * @param Smarty_Internal_Template $_template
114 * @param string $content
118 public function writeCachedContent(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
120 if ($_template->source->handler->recompiled || !($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
121 $_template->caching == Smarty::CACHING_LIFETIME_SAVED)
123 // don't write cache file
126 $content = $_template->smarty->ext->_codeFrame->create($_template, $content, '', true);
127 return $this->write($cached, $_template, $content);
131 * Write this cache object to handler
133 * @param \Smarty_Template_Cached $cached
134 * @param Smarty_Internal_Template $_template template object
135 * @param string $content content to cache
137 * @return bool success
139 public function write(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $content)
141 if (!$_template->source->handler->recompiled) {
142 if ($cached->handler->writeCachedContent($_template, $content)) {
143 $cached->content = null;
144 $cached->timestamp = time();
145 $cached->exists = true;
146 $cached->valid = true;
147 $cached->cache_lifetime = $_template->cache_lifetime;
148 $cached->processed = false;
149 if ($_template->smarty->cache_locking) {
150 $cached->handler->releaseLock($_template->smarty, $cached);
155 $cached->content = null;
156 $cached->timestamp = false;
157 $cached->exists = false;
158 $cached->valid = false;
159 $cached->processed = false;