You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_template_cached.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Uwe Tews
  5. * Date: 04.12.2014
  6. * Time: 06:08
  7. */
  8. /**
  9. * Smarty Resource Data Object
  10. * Cache Data Container for Template Files
  11. *
  12. * @package Smarty
  13. * @subpackage TemplateResources
  14. * @author Rodney Rehm
  15. */
  16. class Smarty_Template_Cached extends Smarty_Template_Resource_Base
  17. {
  18. /**
  19. * Cache Is Valid
  20. *
  21. * @var boolean
  22. */
  23. public $valid = null;
  24. /**
  25. * CacheResource Handler
  26. *
  27. * @var Smarty_CacheResource
  28. */
  29. public $handler = null;
  30. /**
  31. * Template Cache Id (Smarty_Internal_Template::$cache_id)
  32. *
  33. * @var string
  34. */
  35. public $cache_id = null;
  36. /**
  37. * saved cache lifetime in seconds
  38. *
  39. * @var integer
  40. */
  41. public $cache_lifetime = 0;
  42. /**
  43. * Id for cache locking
  44. *
  45. * @var string
  46. */
  47. public $lock_id = null;
  48. /**
  49. * flag that cache is locked by this instance
  50. *
  51. * @var bool
  52. */
  53. public $is_locked = false;
  54. /**
  55. * Source Object
  56. *
  57. * @var Smarty_Template_Source
  58. */
  59. public $source = null;
  60. /**
  61. * Nocache hash codes of processed compiled templates
  62. *
  63. * @var array
  64. */
  65. public $hashes = array();
  66. /**
  67. * create Cached Object container
  68. *
  69. * @param Smarty_Internal_Template $_template template object
  70. */
  71. public function __construct(Smarty_Internal_Template $_template)
  72. {
  73. $this->compile_id = $_template->compile_id;
  74. $this->cache_id = $_template->cache_id;
  75. $this->source = $_template->source;
  76. if (!class_exists('Smarty_CacheResource', false)) {
  77. require SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  78. }
  79. $this->handler = Smarty_CacheResource::load($_template->smarty);
  80. }
  81. /**
  82. * @param Smarty_Internal_Template $_template
  83. *
  84. * @return Smarty_Template_Cached
  85. */
  86. static function load(Smarty_Internal_Template $_template)
  87. {
  88. $_template->cached = new Smarty_Template_Cached($_template);
  89. $_template->cached->handler->populate($_template->cached, $_template);
  90. // caching enabled ?
  91. if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
  92. $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->handler->recompiled
  93. ) {
  94. $_template->cached->valid = false;
  95. }
  96. return $_template->cached;
  97. }
  98. /**
  99. * Render cache template
  100. *
  101. * @param \Smarty_Internal_Template $_template
  102. * @param bool $no_output_filter
  103. *
  104. * @throws \Exception
  105. */
  106. public function render(Smarty_Internal_Template $_template, $no_output_filter = true)
  107. {
  108. if ($this->isCached($_template)) {
  109. if ($_template->smarty->debugging) {
  110. $_template->smarty->_debug->start_cache($_template);
  111. }
  112. if (!$this->processed) {
  113. $this->process($_template);
  114. }
  115. $this->getRenderedTemplateCode($_template);
  116. if ($_template->smarty->debugging) {
  117. $_template->smarty->_debug->end_cache($_template);
  118. }
  119. return;
  120. } else {
  121. $_template->smarty->ext->_updateCache->updateCache($this, $_template, $no_output_filter);
  122. }
  123. }
  124. /**
  125. * Check if cache is valid, lock cache if required
  126. *
  127. * @param \Smarty_Internal_Template $_template
  128. *
  129. * @return bool flag true if cache is valid
  130. */
  131. public function isCached(Smarty_Internal_Template $_template)
  132. {
  133. if ($this->valid !== null) {
  134. return $this->valid;
  135. }
  136. while (true) {
  137. while (true) {
  138. if ($this->exists === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
  139. $this->valid = false;
  140. } else {
  141. $this->valid = true;
  142. }
  143. if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT &&
  144. $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)
  145. ) {
  146. // lifetime expired
  147. $this->valid = false;
  148. }
  149. if ($this->valid && $_template->smarty->compile_check == 1 &&
  150. $_template->source->getTimeStamp() > $this->timestamp
  151. ) {
  152. $this->valid = false;
  153. }
  154. if ($this->valid || !$_template->smarty->cache_locking) {
  155. break;
  156. }
  157. if (!$this->handler->locked($_template->smarty, $this)) {
  158. $this->handler->acquireLock($_template->smarty, $this);
  159. break 2;
  160. }
  161. $this->handler->populate($this, $_template);
  162. }
  163. if ($this->valid) {
  164. if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
  165. // load cache file for the following checks
  166. if ($_template->smarty->debugging) {
  167. $_template->smarty->_debug->start_cache($_template);
  168. }
  169. if ($this->handler->process($_template, $this) === false) {
  170. $this->valid = false;
  171. } else {
  172. $this->processed = true;
  173. }
  174. if ($_template->smarty->debugging) {
  175. $_template->smarty->_debug->end_cache($_template);
  176. }
  177. } else {
  178. $this->is_locked = true;
  179. continue;
  180. }
  181. } else {
  182. return $this->valid;
  183. }
  184. if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED &&
  185. $_template->cached->cache_lifetime >= 0 &&
  186. (time() > ($_template->cached->timestamp + $_template->cached->cache_lifetime))
  187. ) {
  188. $this->valid = false;
  189. }
  190. if ($_template->smarty->cache_locking) {
  191. if (!$this->valid) {
  192. $this->handler->acquireLock($_template->smarty, $this);
  193. } elseif ($this->is_locked) {
  194. $this->handler->releaseLock($_template->smarty, $this);
  195. }
  196. }
  197. return $this->valid;
  198. }
  199. return $this->valid;
  200. }
  201. /**
  202. * Process cached template
  203. *
  204. * @param Smarty_Internal_Template $_template template object
  205. * @param bool $update flag if called because cache update
  206. */
  207. public function process(Smarty_Internal_Template $_template, $update = false)
  208. {
  209. if ($this->handler->process($_template, $this, $update) === false) {
  210. $this->valid = false;
  211. }
  212. if ($this->valid) {
  213. $this->processed = true;
  214. } else {
  215. $this->processed = false;
  216. }
  217. }
  218. /**
  219. * Read cache content from handler
  220. *
  221. * @param Smarty_Internal_Template $_template template object
  222. *
  223. * @return string content
  224. */
  225. public function read(Smarty_Internal_Template $_template)
  226. {
  227. if (!$_template->source->handler->recompiled) {
  228. return $this->handler->readCachedContent($_template);
  229. }
  230. return false;
  231. }
  232. }