PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/external/smarty-4.0.4/libs/sysplugins/smarty_cacheresource_custom.php

https://github.com/exponentcms/exponent-cms
PHP | 297 lines | 141 code | 14 blank | 142 comment | 13 complexity | e3a3b5381cedd32c391216cd86831af2 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin
  4. *
  5. * @package Smarty
  6. * @subpackage Cacher
  7. */
  8. /**
  9. * Cache Handler API
  10. *
  11. * @package Smarty
  12. * @subpackage Cacher
  13. * @author Rodney Rehm
  14. */
  15. abstract class Smarty_CacheResource_Custom extends Smarty_CacheResource
  16. {
  17. /**
  18. * fetch cached content and its modification time from data source
  19. *
  20. * @param string $id unique cache content identifier
  21. * @param string $name template name
  22. * @param string $cache_id cache id
  23. * @param string $compile_id compile id
  24. * @param string $content cached content
  25. * @param integer $mtime cache modification timestamp (epoch)
  26. *
  27. * @return void
  28. */
  29. abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
  30. /**
  31. * Fetch cached content's modification timestamp from data source
  32. * {@internal implementing this method is optional.
  33. * Only implement it if modification times can be accessed faster than loading the complete cached content.}}
  34. *
  35. * @param string $id unique cache content identifier
  36. * @param string $name template name
  37. * @param string $cache_id cache id
  38. * @param string $compile_id compile id
  39. *
  40. * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
  41. */
  42. protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
  43. {
  44. return false;
  45. }
  46. /**
  47. * Save content to cache
  48. *
  49. * @param string $id unique cache content identifier
  50. * @param string $name template name
  51. * @param string $cache_id cache id
  52. * @param string $compile_id compile id
  53. * @param integer|null $exp_time seconds till expiration or null
  54. * @param string $content content to cache
  55. *
  56. * @return boolean success
  57. */
  58. abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
  59. /**
  60. * Delete content from cache
  61. *
  62. * @param string|null $name template name
  63. * @param string|null $cache_id cache id
  64. * @param string|null $compile_id compile id
  65. * @param integer|null $exp_time seconds till expiration time in seconds or null
  66. *
  67. * @return integer number of deleted caches
  68. */
  69. abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
  70. /**
  71. * populate Cached Object with meta data from Resource
  72. *
  73. * @param Smarty_Template_Cached $cached cached object
  74. * @param Smarty_Internal_Template $_template template object
  75. *
  76. * @return void
  77. */
  78. public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
  79. {
  80. $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
  81. $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
  82. $path = $cached->source->uid . $_cache_id . $_compile_id;
  83. $cached->filepath = sha1($path);
  84. if ($_template->smarty->cache_locking) {
  85. $cached->lock_id = sha1('lock.' . $path);
  86. }
  87. $this->populateTimestamp($cached);
  88. }
  89. /**
  90. * populate Cached Object with timestamp and exists from Resource
  91. *
  92. * @param Smarty_Template_Cached $cached
  93. *
  94. * @return void
  95. */
  96. public function populateTimestamp(Smarty_Template_Cached $cached)
  97. {
  98. $mtime =
  99. $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
  100. if ($mtime !== null) {
  101. $cached->timestamp = $mtime;
  102. $cached->exists = !!$cached->timestamp;
  103. return;
  104. }
  105. $timestamp = null;
  106. $this->fetch(
  107. $cached->filepath,
  108. $cached->source->name,
  109. $cached->cache_id,
  110. $cached->compile_id,
  111. $cached->content,
  112. $timestamp
  113. );
  114. $cached->timestamp = isset($timestamp) ? $timestamp : false;
  115. $cached->exists = !!$cached->timestamp;
  116. }
  117. /**
  118. * Read the cached template and process the header
  119. *
  120. * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
  121. * @param Smarty_Template_Cached $cached cached object
  122. * @param boolean $update flag if called because cache update
  123. *
  124. * @return boolean true or false if the cached content does not exist
  125. */
  126. public function process(
  127. Smarty_Internal_Template $_smarty_tpl,
  128. Smarty_Template_Cached $cached = null,
  129. $update = false
  130. ) {
  131. if (!$cached) {
  132. $cached = $_smarty_tpl->cached;
  133. }
  134. $content = $cached->content ? $cached->content : null;
  135. $timestamp = $cached->timestamp ? $cached->timestamp : null;
  136. if ($content === null || !$timestamp) {
  137. $this->fetch(
  138. $_smarty_tpl->cached->filepath,
  139. $_smarty_tpl->source->name,
  140. $_smarty_tpl->cache_id,
  141. $_smarty_tpl->compile_id,
  142. $content,
  143. $timestamp
  144. );
  145. }
  146. if (isset($content)) {
  147. eval('?>' . $content);
  148. $cached->content = null;
  149. return true;
  150. }
  151. return false;
  152. }
  153. /**
  154. * Write the rendered template output to cache
  155. *
  156. * @param Smarty_Internal_Template $_template template object
  157. * @param string $content content to cache
  158. *
  159. * @return boolean success
  160. */
  161. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  162. {
  163. return $this->save(
  164. $_template->cached->filepath,
  165. $_template->source->name,
  166. $_template->cache_id,
  167. $_template->compile_id,
  168. $_template->cache_lifetime,
  169. $content
  170. );
  171. }
  172. /**
  173. * Read cached template from cache
  174. *
  175. * @param Smarty_Internal_Template $_template template object
  176. *
  177. * @return string|boolean content
  178. */
  179. public function readCachedContent(Smarty_Internal_Template $_template)
  180. {
  181. $content = $_template->cached->content ? $_template->cached->content : null;
  182. $timestamp = null;
  183. if ($content === null) {
  184. $timestamp = null;
  185. $this->fetch(
  186. $_template->cached->filepath,
  187. $_template->source->name,
  188. $_template->cache_id,
  189. $_template->compile_id,
  190. $content,
  191. $timestamp
  192. );
  193. }
  194. if (isset($content)) {
  195. return $content;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Empty cache
  201. *
  202. * @param Smarty $smarty Smarty object
  203. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  204. *
  205. * @return integer number of cache files deleted
  206. */
  207. public function clearAll(Smarty $smarty, $exp_time = null)
  208. {
  209. return $this->delete(null, null, null, $exp_time);
  210. }
  211. /**
  212. * Empty cache for a specific template
  213. *
  214. * @param Smarty $smarty Smarty object
  215. * @param string $resource_name template name
  216. * @param string $cache_id cache id
  217. * @param string $compile_id compile id
  218. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  219. *
  220. * @return int number of cache files deleted
  221. * @throws \SmartyException
  222. */
  223. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  224. {
  225. $cache_name = null;
  226. if (isset($resource_name)) {
  227. $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
  228. if ($source->exists) {
  229. $cache_name = $source->name;
  230. } else {
  231. return 0;
  232. }
  233. }
  234. return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
  235. }
  236. /**
  237. * Check is cache is locked for this template
  238. *
  239. * @param Smarty $smarty Smarty object
  240. * @param Smarty_Template_Cached $cached cached object
  241. *
  242. * @return boolean true or false if cache is locked
  243. */
  244. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  245. {
  246. $id = $cached->lock_id;
  247. $name = $cached->source->name . '.lock';
  248. $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
  249. if ($mtime === null) {
  250. $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
  251. }
  252. return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
  253. }
  254. /**
  255. * Lock cache for this template
  256. *
  257. * @param Smarty $smarty Smarty object
  258. * @param Smarty_Template_Cached $cached cached object
  259. *
  260. * @return bool|void
  261. */
  262. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  263. {
  264. $cached->is_locked = true;
  265. $id = $cached->lock_id;
  266. $name = $cached->source->name . '.lock';
  267. $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
  268. }
  269. /**
  270. * Unlock cache for this template
  271. *
  272. * @param Smarty $smarty Smarty object
  273. * @param Smarty_Template_Cached $cached cached object
  274. *
  275. * @return bool|void
  276. */
  277. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  278. {
  279. $cached->is_locked = false;
  280. $name = $cached->source->name . '.lock';
  281. $this->delete($name, $cached->cache_id, $cached->compile_id, null);
  282. }
  283. }