PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/libs/smarty/sysplugins/smarty_cacheresource_custom.php

https://gitlab.com/adamlwalker/generatedata
PHP | 237 lines | 99 code | 19 blank | 119 comment | 7 complexity | 30a9c1480db001fd4a3a7c80e7dc5a15 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. * fetch cached content and its modification time from data source
  18. *
  19. * @param string $id unique cache content identifier
  20. * @param string $name template name
  21. * @param string $cache_id cache id
  22. * @param string $compile_id compile id
  23. * @param string $content cached content
  24. * @param integer $mtime cache modification timestamp (epoch)
  25. * @return void
  26. */
  27. protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
  28. /**
  29. * Fetch cached content's modification timestamp from data source
  30. *
  31. * {@internal implementing this method is optional.
  32. * Only implement it if modification times can be accessed faster than loading the complete cached content.}}
  33. *
  34. * @param string $id unique cache content identifier
  35. * @param string $name template name
  36. * @param string $cache_id cache id
  37. * @param string $compile_id compile id
  38. * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
  39. */
  40. protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
  41. {
  42. return null;
  43. }
  44. /**
  45. * Save content to cache
  46. *
  47. * @param string $id unique cache content identifier
  48. * @param string $name template name
  49. * @param string $cache_id cache id
  50. * @param string $compile_id compile id
  51. * @param integer|null $exp_time seconds till expiration or null
  52. * @param string $content content to cache
  53. * @return boolean success
  54. */
  55. protected abstract function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
  56. /**
  57. * Delete content from cache
  58. *
  59. * @param string $name template name
  60. * @param string $cache_id cache id
  61. * @param string $compile_id compile id
  62. * @param integer|null $exp_time seconds till expiration time in seconds or null
  63. * @return integer number of deleted caches
  64. */
  65. protected abstract function delete($name, $cache_id, $compile_id, $exp_time);
  66. /**
  67. * populate Cached Object with meta data from Resource
  68. *
  69. * @param Smarty_Template_Cached $cached cached object
  70. * @param Smarty_Internal_Template $_template template object
  71. * @return void
  72. */
  73. public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
  74. {
  75. $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
  76. $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w\|]+!', '_', $cached->compile_id) : null;
  77. $cached->filepath = sha1($cached->source->filepath . $_cache_id . $_compile_id);
  78. $this->populateTimestamp($cached);
  79. }
  80. /**
  81. * populate Cached Object with timestamp and exists from Resource
  82. *
  83. * @param Smarty_Template_Cached $source cached object
  84. * @return void
  85. */
  86. public function populateTimestamp(Smarty_Template_Cached $cached)
  87. {
  88. $mtime = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
  89. if ($mtime !== null) {
  90. $cached->timestamp = $mtime;
  91. $cached->exists = !!$cached->timestamp;
  92. return;
  93. }
  94. $timestamp = null;
  95. $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
  96. $cached->timestamp = isset($timestamp) ? $timestamp : false;
  97. $cached->exists = !!$cached->timestamp;
  98. }
  99. /**
  100. * Read the cached template and process the header
  101. *
  102. * @param Smarty_Internal_Template $_template template object
  103. * @param Smarty_Template_Cached $cached cached object
  104. * @return booelan true or false if the cached content does not exist
  105. */
  106. public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
  107. {
  108. if (!$cached) {
  109. $cached = $_template->cached;
  110. }
  111. $content = $cached->content ? $cached->content : null;
  112. $timestamp = $cached->timestamp ? $cached->timestamp : null;
  113. if ($content === null || !$timestamp) {
  114. $this->fetch(
  115. $_template->cached->filepath,
  116. $_template->source->name,
  117. $_template->cache_id,
  118. $_template->compile_id,
  119. $content,
  120. $timestamp
  121. );
  122. }
  123. if (isset($content)) {
  124. $_smarty_tpl = $_template;
  125. eval("?>" . $content);
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Write the rendered template output to cache
  132. *
  133. * @param Smarty_Internal_Template $_template template object
  134. * @param string $content content to cache
  135. * @return boolean success
  136. */
  137. public function writeCachedContent(Smarty_Internal_Template $_template, $content)
  138. {
  139. return $this->save(
  140. $_template->cached->filepath,
  141. $_template->source->name,
  142. $_template->cache_id,
  143. $_template->compile_id,
  144. $_template->properties['cache_lifetime'],
  145. $content
  146. );
  147. }
  148. /**
  149. * Empty cache
  150. *
  151. * @param Smarty $smarty Smarty object
  152. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  153. * @return integer number of cache files deleted
  154. */
  155. public function clearAll(Smarty $smarty, $exp_time=null)
  156. {
  157. $this->cache = array();
  158. return $this->delete(null, null, null, $exp_time);
  159. }
  160. /**
  161. * Empty cache for a specific template
  162. *
  163. * @param Smarty $smarty Smarty object
  164. * @param string $resource_name template name
  165. * @param string $cache_id cache id
  166. * @param string $compile_id compile id
  167. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  168. * @return integer number of cache files deleted
  169. */
  170. public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
  171. {
  172. $this->cache = array();
  173. return $this->delete($resource_name, $cache_id, $compile_id, $exp_time);
  174. }
  175. /**
  176. * Check is cache is locked for this template
  177. *
  178. * @param Smarty $smarty Smarty object
  179. * @param Smarty_Template_Cached $cached cached object
  180. * @return booelan true or false if cache is locked
  181. */
  182. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  183. {
  184. $id = $cached->filepath;
  185. $name = $cached->source->name . '.lock';
  186. $mtime = $this->fetchTimestamp($id, $name, null, null);
  187. if ($mtime === null) {
  188. $this->fetch($id, $name, null, null, $content, $mtime);
  189. }
  190. return $mtime && time() - $mtime < $smarty->locking_timeout;
  191. }
  192. /**
  193. * Lock cache for this template
  194. *
  195. * @param Smarty $smarty Smarty object
  196. * @param Smarty_Template_Cached $cached cached object
  197. */
  198. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  199. {
  200. $cached->is_locked = true;
  201. $id = $cached->filepath;
  202. $name = $cached->source->name . '.lock';
  203. $this->save($id, $name, null, null, $smarty->locking_timeout, '');
  204. }
  205. /**
  206. * Unlock cache for this template
  207. *
  208. * @param Smarty $smarty Smarty object
  209. * @param Smarty_Template_Cached $cached cached object
  210. */
  211. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  212. {
  213. $cached->is_locked = false;
  214. $name = $cached->source->name . '.lock';
  215. $this->delete($name, null, null, null);
  216. }
  217. }
  218. ?>