PageRenderTime 29ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/smarty/libs/sysplugins/smarty_internal_cacheresource_file.php

https://github.com/phobia/Kohana_Smarty3
PHP | 207 lines | 181 code | 2 blank | 24 comment | 17 complexity | cdf76d10a5b40b5aecb413704e990367 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin CacheResource File
  4. *
  5. * Implements the file system as resource for the HTML cache
  6. * Version ussing nocache inserts
  7. *
  8. * @package Smarty
  9. * @subpackage Cacher
  10. * @author Uwe Tews
  11. */
  12. /**
  13. * This class does contain all necessary methods for the HTML cache on file system
  14. */
  15. class Smarty_Internal_CacheResource_File {
  16. function __construct($smarty)
  17. {
  18. $this->smarty = $smarty;
  19. }
  20. /**
  21. * Returns the filepath of the cached template output
  22. *
  23. * @param object $_template current template
  24. * @return string the cache filepath
  25. */
  26. public function getCachedFilepath($_template)
  27. {
  28. $_source_file_path = str_replace(':', '.', $_template->getTemplateFilepath());
  29. $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;
  30. $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
  31. $_filepath = $_template->templateUid;
  32. // if use_sub_dirs, break file into directories
  33. if ($this->smarty->use_sub_dirs) {
  34. $_filepath = substr($_filepath, 0, 2) . DS
  35. . substr($_filepath, 2, 2) . DS
  36. . substr($_filepath, 4, 2) . DS
  37. . $_filepath;
  38. }
  39. $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
  40. if (isset($_cache_id)) {
  41. $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;
  42. } else {
  43. $_cache_id = '';
  44. }
  45. if (isset($_compile_id)) {
  46. $_compile_id = $_compile_id . $_compile_dir_sep;
  47. } else {
  48. $_compile_id = '';
  49. }
  50. $_cache_dir = $this->smarty->cache_dir;
  51. if (strpos('/\\', substr($_cache_dir, -1)) === false) {
  52. $_cache_dir .= DS;
  53. }
  54. return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';
  55. }
  56. /**
  57. * Returns the timpestamp of the cached template output
  58. *
  59. * @param object $_template current template
  60. * @return integer |booelan the template timestamp or false if the file does not exist
  61. */
  62. public function getCachedTimestamp($_template)
  63. {
  64. // return @filemtime ($_template->getCachedFilepath());
  65. return ($_template->getCachedFilepath() && file_exists($_template->getCachedFilepath())) ? filemtime($_template->getCachedFilepath()) : false ;
  66. }
  67. /**
  68. * Returns the cached template output
  69. *
  70. * @param object $_template current template
  71. * @return string |booelan the template content or false if the file does not exist
  72. */
  73. public function getCachedContents($_template, $no_render = false)
  74. {
  75. if (!$no_render) {
  76. ob_start();
  77. }
  78. $_smarty_tpl = $_template;
  79. include $_template->getCachedFilepath();
  80. if ($no_render) {
  81. return null;
  82. } else {
  83. return ob_get_clean();
  84. }
  85. }
  86. /**
  87. * Writes the rendered template output to cache file
  88. *
  89. * @param object $_template current template
  90. * @return boolean status
  91. */
  92. public function writeCachedContent($_template, $content)
  93. {
  94. if (!$_template->resource_object->isEvaluated) {
  95. if (Smarty_Internal_Write_File::writeFile($_template->getCachedFilepath(), $content, $this->smarty) === true) {
  96. $_template->cached_timestamp = filemtime($_template->getCachedFilepath());
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Empty cache folder
  104. *
  105. * @param integer $exp_time expiration time
  106. * @return integer number of cache files deleted
  107. */
  108. public function clearAll($exp_time = null)
  109. {
  110. return $this->clear(null, null, null, $exp_time);
  111. }
  112. /**
  113. * Empty cache for a specific template
  114. *
  115. * @param string $resource_name template name
  116. * @param string $cache_id cache id
  117. * @param string $compile_id compile id
  118. * @param integer $exp_time expiration time
  119. * @return integer number of cache files deleted
  120. */
  121. public function clear($resource_name, $cache_id, $compile_id, $exp_time)
  122. {
  123. $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;
  124. $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;
  125. $_dir_sep = $this->smarty->use_sub_dirs ? '/' : '^';
  126. $_compile_id_offset = $this->smarty->use_sub_dirs ? 3 : 0;
  127. $_dir = rtrim($this->smarty->cache_dir, '/\\') . DS;
  128. $_dir_length = strlen($_dir);
  129. if (isset($_cache_id)) {
  130. $_cache_id_parts = explode('|', $_cache_id);
  131. $_cache_id_parts_count = count($_cache_id_parts);
  132. if ($this->smarty->use_sub_dirs) {
  133. foreach ($_cache_id_parts as $id_part) {
  134. $_dir .= $id_part . DS;
  135. }
  136. }
  137. }
  138. if (isset($resource_name)) {
  139. $_save_stat = $this->smarty->caching;
  140. $this->smarty->caching = true;
  141. $tpl = new $this->smarty->template_class($resource_name, $this->smarty);
  142. $this->smarty->caching = $_save_stat;
  143. if ($tpl->isExisting()) {
  144. $_resourcename_parts = basename(str_replace('^', '/', $tpl->getCachedFilepath()));
  145. // remove from template cache
  146. unset($this->smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
  147. } else {
  148. // remove from template cache
  149. unset($this->smarty->template_objects[sha1($tpl->template_resource . $tpl->cache_id . $tpl->compile_id)]);
  150. return 0;
  151. }
  152. }
  153. $_count = 0;
  154. if (file_exists($_dir)) {
  155. $_cacheDirs = new RecursiveDirectoryIterator($_dir);
  156. $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
  157. foreach ($_cache as $_file) {
  158. if (substr($_file->getBasename(),0,1) == '.') continue;
  159. // directory ?
  160. if ($_file->isDir()) {
  161. if (!$_cache->isDot()) {
  162. // delete folder if empty
  163. @rmdir($_file->getPathname());
  164. }
  165. } else {
  166. $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));
  167. $_parts_count = count($_parts);
  168. // check name
  169. if (isset($resource_name)) {
  170. if ($_parts[$_parts_count-1] != $_resourcename_parts) {
  171. continue;
  172. }
  173. }
  174. // check compile id
  175. if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
  176. continue;
  177. }
  178. // check cache id
  179. if (isset($_cache_id)) {
  180. // count of cache id parts
  181. $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;
  182. if ($_parts_count < $_cache_id_parts_count) {
  183. continue;
  184. }
  185. for ($i = 0; $i < $_cache_id_parts_count; $i++) {
  186. if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;
  187. }
  188. }
  189. // expired ?
  190. if (isset($exp_time) && time() - @filemtime($_file) < $exp_time) {
  191. continue;
  192. }
  193. $_count += @unlink((string) $_file) ? 1 : 0;
  194. }
  195. }
  196. }
  197. return $_count;
  198. }
  199. }
  200. ?>