3 * Smarty Internal Plugin CacheResource File
12 * This class does contain all necessary methods for the HTML cache on file system
13 * Implements the file system as resource for the HTML cache Version ussing nocache inserts.
18 class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
21 * populate Cached Object with meta data from Resource
23 * @param Smarty_Template_Cached $cached cached object
24 * @param Smarty_Internal_Template $_template template object
28 public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
30 $source = &$_template->source;
31 $smarty = &$_template->smarty;
32 $_compile_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
33 $_filepath = sha1($source->uid . $smarty->_joined_template_dir);
34 $cached->filepath = $smarty->getCacheDir();
35 if (isset($_template->cache_id)) {
36 $cached->filepath .= preg_replace(array('![^\w|]+!', '![|]+!'), array('_', $_compile_dir_sep),
37 $_template->cache_id) . $_compile_dir_sep;
39 if (isset($_template->compile_id)) {
40 $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
42 // if use_sub_dirs, break file into directories
43 if ($smarty->use_sub_dirs) {
44 $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . $smarty->ds . $_filepath[ 2 ] . $_filepath[ 3 ] . $smarty->ds .
45 $_filepath[ 4 ] . $_filepath[ 5 ] . $smarty->ds;
47 $cached->filepath .= $_filepath;
48 $basename = $source->handler->getBasename($source);
49 if (!empty($basename)) {
50 $cached->filepath .= '.' . $basename;
52 if ($smarty->cache_locking) {
53 $cached->lock_id = $cached->filepath . '.lock';
55 $cached->filepath .= '.php';
56 $cached->timestamp = $cached->exists = is_file($cached->filepath);
57 if ($cached->exists) {
58 $cached->timestamp = filemtime($cached->filepath);
63 * populate Cached Object with timestamp and exists from Resource
65 * @param Smarty_Template_Cached $cached cached object
69 public function populateTimestamp(Smarty_Template_Cached $cached)
71 $cached->timestamp = $cached->exists = is_file($cached->filepath);
72 if ($cached->exists) {
73 $cached->timestamp = filemtime($cached->filepath);
78 * Read the cached template and process its header
80 * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
81 * @param Smarty_Template_Cached $cached cached object
82 * @param bool $update flag if called because cache update
84 * @return boolean true or false if the cached content does not exist
86 public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
89 $_smarty_tpl->cached->valid = false;
90 if ($update && defined('HHVM_VERSION')) {
91 eval("?>" . file_get_contents($_smarty_tpl->cached->filepath));
94 return @include $_smarty_tpl->cached->filepath;
99 * Write the rendered template output to cache
101 * @param Smarty_Internal_Template $_template template object
102 * @param string $content content to cache
104 * @return boolean success
106 public function writeCachedContent(Smarty_Internal_Template $_template, $content)
108 if ($_template->smarty->ext->_writeFile->writeFile($_template->cached->filepath, $content,
109 $_template->smarty) === true
111 if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
112 opcache_invalidate($_template->cached->filepath, true);
113 } elseif (function_exists('apc_compile_file')) {
114 apc_compile_file($_template->cached->filepath);
116 $cached = $_template->cached;
117 $cached->timestamp = $cached->exists = is_file($cached->filepath);
118 if ($cached->exists) {
119 $cached->timestamp = filemtime($cached->filepath);
127 * Read cached template from cache
129 * @param Smarty_Internal_Template $_template template object
131 * @return string content
133 public function readCachedContent(Smarty_Internal_Template $_template)
135 if (is_file($_template->cached->filepath)) {
136 return file_get_contents($_template->cached->filepath);
144 * @param Smarty $smarty
145 * @param integer $exp_time expiration time (number of seconds, not timestamp)
147 * @return integer number of cache files deleted
149 public function clearAll(Smarty $smarty, $exp_time = null)
151 return $smarty->ext->_cacheResourceFile->clear($smarty, null, null, null, $exp_time);
155 * Empty cache for a specific template
157 * @param Smarty $smarty
158 * @param string $resource_name template name
159 * @param string $cache_id cache id
160 * @param string $compile_id compile id
161 * @param integer $exp_time expiration time (number of seconds, not timestamp)
163 * @return integer number of cache files deleted
165 public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
167 return $smarty->ext->_cacheResourceFile->clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time);
171 * Check is cache is locked for this template
173 * @param Smarty $smarty Smarty object
174 * @param Smarty_Template_Cached $cached cached object
176 * @return boolean true or false if cache is locked
178 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
180 if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
181 clearstatcache(true, $cached->lock_id);
185 if (is_file($cached->lock_id)) {
186 $t = filemtime($cached->lock_id);
187 return $t && (time() - $t < $smarty->locking_timeout);
194 * Lock cache for this template
196 * @param Smarty $smarty Smarty object
197 * @param Smarty_Template_Cached $cached cached object
201 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
203 $cached->is_locked = true;
204 touch($cached->lock_id);
208 * Unlock cache for this template
210 * @param Smarty $smarty Smarty object
211 * @param Smarty_Template_Cached $cached cached object
215 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
217 $cached->is_locked = false;
218 @unlink($cached->lock_id);