libs/sysplugins/smarty_internal_cacheresource_file.php Quellcode

smarty_internal_cacheresource_file.php
gehe zur Dokumentation dieser Datei
1 <?php
19 {
28  public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
29  {
30  $_source_file_path = str_replace(':', '.', $_template->source->filepath);
31  $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
32  $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
33  $_filepath = $_template->source->uid;
34  // if use_sub_dirs, break file into directories
35  if ($_template->smarty->use_sub_dirs) {
36  $_filepath = substr($_filepath, 0, 2) . DS
37  . substr($_filepath, 2, 2) . DS
38  . substr($_filepath, 4, 2) . DS
39  . $_filepath;
40  }
41  $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
42  if (isset($_cache_id)) {
43  $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
44  } else {
45  $_cache_id = '';
46  }
47  if (isset($_compile_id)) {
48  $_compile_id = $_compile_id . $_compile_dir_sep;
49  } else {
50  $_compile_id = '';
51  }
52  $_cache_dir = $_template->smarty->getCacheDir();
53  if ($_template->smarty->cache_locking) {
54  // create locking file name
55  // relative file name?
56  if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {
57  $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;
58  } else {
59  $_lock_dir = $_cache_dir;
60  }
61  $cached->lock_id = $_lock_dir . sha1($_cache_id . $_compile_id . $_template->source->uid) . '.lock';
62  }
63  $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
64  $cached->timestamp = $cached->exists = is_file($cached->filepath);
65  if ($cached->exists) {
66  $cached->timestamp = filemtime($cached->filepath);
67  }
68  }
69 
77  public function populateTimestamp(Smarty_Template_Cached $cached)
78  {
79  $cached->timestamp = $cached->exists = is_file($cached->filepath);
80  if ($cached->exists) {
81  $cached->timestamp = filemtime($cached->filepath);
82  }
83  }
84 
93  public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
94  {
98  $_smarty_tpl = $_template;
99 
100  return @include $_template->cached->filepath;
101  }
102 
111  public function writeCachedContent(Smarty_Internal_Template $_template, $content)
112  {
113  $obj = new Smarty_Internal_Write_File();
114  if ($obj->writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {
115  $cached = $_template->cached;
116  $cached->timestamp = $cached->exists = is_file($cached->filepath);
117  if ($cached->exists) {
118  $cached->timestamp = filemtime($cached->filepath);
119  return true;
120  }
121  }
122  return false;
123  }
124 
132  public function readCachedContent(Smarty_Internal_Template $_template)
133  {
134  if (is_file($_template->cached->filepath)) {
135  return file_get_contents($_template->cached->filepath);
136  }
137  return false;
138  }
139 
148  public function clearAll(Smarty $smarty, $exp_time = null)
149  {
150  return $this->clear($smarty, null, null, null, $exp_time);
151  }
152 
164  public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
165  {
166  $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
167  $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
168  $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';
169  $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;
170  $_dir = realpath($smarty->getCacheDir()) . '/';
171  if ($_dir == '/') { //We should never want to delete this!
172  return 0;
173  }
174  $_dir_length = strlen($_dir);
175  if (isset($_cache_id)) {
176  $_cache_id_parts = explode('|', $_cache_id);
177  $_cache_id_parts_count = count($_cache_id_parts);
178  if ($smarty->use_sub_dirs) {
179  foreach ($_cache_id_parts as $id_part) {
180  $_dir .= $id_part . DS;
181  }
182  }
183  }
184  if (isset($resource_name)) {
185  $_save_stat = $smarty->caching;
186  $smarty->caching = true;
187  $tpl = new $smarty->template_class($resource_name, $smarty);
188  $smarty->caching = $_save_stat;
189 
190  // remove from template cache
191  $tpl->source; // have the template registered before unset()
192  if ($smarty->allow_ambiguous_resources) {
193  $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
194  } else {
195  $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
196  }
197  if (isset($_templateId[150])) {
198  $_templateId = sha1($_templateId);
199  }
200  unset($smarty->template_objects[$_templateId]);
201 
202  if ($tpl->source->exists) {
203  $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));
204  } else {
205  return 0;
206  }
207  }
208  $_count = 0;
209  $_time = time();
210  if (file_exists($_dir)) {
211  $_cacheDirs = new RecursiveDirectoryIterator($_dir);
212  $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
213  foreach ($_cache as $_file) {
214  if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
215  continue;
216  }
217  // directory ?
218  if ($_file->isDir()) {
219  if (!$_cache->isDot()) {
220  // delete folder if empty
221  @rmdir($_file->getPathname());
222  }
223  } else {
224  $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string) $_file, $_dir_length)));
225  $_parts_count = count($_parts);
226  // check name
227  if (isset($resource_name)) {
228  if ($_parts[$_parts_count - 1] != $_resourcename_parts) {
229  continue;
230  }
231  }
232  // check compile id
233  if (isset($_compile_id) && (!isset($_parts[$_parts_count - 2 - $_compile_id_offset]) || $_parts[$_parts_count - 2 - $_compile_id_offset] != $_compile_id)) {
234  continue;
235  }
236  // check cache id
237  if (isset($_cache_id)) {
238  // count of cache id parts
239  $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
240  if ($_parts_count < $_cache_id_parts_count) {
241  continue;
242  }
243  for ($i = 0; $i < $_cache_id_parts_count; $i ++) {
244  if ($_parts[$i] != $_cache_id_parts[$i]) {
245  continue 2;
246  }
247  }
248  }
249  // expired ?
250  if (isset($exp_time)) {
251  if ($exp_time < 0) {
252  preg_match('#\'cache_lifetime\' =>\s*(\d*)#', file_get_contents($_file), $match);
253  if ($_time < (@filemtime($_file) + $match[1])) {
254  continue;
255  }
256  } else {
257  if ($_time - @filemtime($_file) < $exp_time) {
258  continue;
259  }
260  }
261  }
262  $_count += @unlink((string) $_file) ? 1 : 0;
263  }
264  }
265  }
266 
267  return $_count;
268  }
269 
278  public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
279  {
280  if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
281  clearstatcache(true, $cached->lock_id);
282  } else {
283  clearstatcache();
284  }
285  if (is_file($cached->lock_id)) {
286  $t = @filemtime($cached->lock_id);
287  return $t && (time() - $t < $smarty->locking_timeout);
288  } else {
289  return false;
290  }
291  }
292 
301  public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
302  {
303  $cached->is_locked = true;
304  touch($cached->lock_id);
305  }
306 
315  public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
316  {
317  $cached->is_locked = false;
318  @unlink($cached->lock_id);
319  }
320 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.