smarty_template_cached.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * Flag if this is a cache resource
  68. *
  69. * @var bool
  70. */
  71. public $isCache = true;
  72. /**
  73. * create Cached Object container
  74. *
  75. * @param Smarty_Internal_Template $_template template object
  76. */
  77. public function __construct(Smarty_Internal_Template $_template)
  78. {
  79. $this->compile_id = $_template->compile_id;
  80. $this->cache_id = $_template->cache_id;
  81. $this->source = $_template->source;
  82. if (!class_exists('Smarty_CacheResource', false)) {
  83. require SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  84. }
  85. $this->handler = Smarty_CacheResource::load($_template->smarty);
  86. }
  87. /**
  88. * @param Smarty_Internal_Template $_template
  89. *
  90. * @return Smarty_Template_Cached
  91. */
  92. static function load(Smarty_Internal_Template $_template)
  93. {
  94. $_template->cached = new Smarty_Template_Cached($_template);
  95. $_template->cached->handler->populate($_template->cached, $_template);
  96. // caching enabled ?
  97. if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT ||
  98. $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->handler->recompiled
  99. ) {
  100. $_template->cached->valid = false;
  101. }
  102. return $_template->cached;
  103. }
  104. /**
  105. * Render cache template
  106. *
  107. * @param \Smarty_Internal_Template $_template
  108. * @param bool $no_output_filter
  109. *
  110. * @throws \Exception
  111. */
  112. public function render(Smarty_Internal_Template $_template, $no_output_filter = true)
  113. {
  114. if ($this->isCached($_template)) {
  115. if ($_template->smarty->debugging) {
  116. if (!isset($_template->smarty->_debug)) {
  117. $_template->smarty->_debug = new Smarty_Internal_Debug();
  118. }
  119. $_template->smarty->_debug->start_cache($_template);
  120. }
  121. if (!$this->processed) {
  122. $this->process($_template);
  123. }
  124. $this->getRenderedTemplateCode($_template);
  125. if ($_template->smarty->debugging) {
  126. $_template->smarty->_debug->end_cache($_template);
  127. }
  128. return;
  129. } else {
  130. $_template->smarty->ext->_updateCache->updateCache($this, $_template, $no_output_filter);
  131. }
  132. }
  133. /**
  134. * Check if cache is valid, lock cache if required
  135. *
  136. * @param \Smarty_Internal_Template $_template
  137. *
  138. * @return bool flag true if cache is valid
  139. */
  140. public function isCached(Smarty_Internal_Template $_template)
  141. {
  142. if ($this->valid !== null) {
  143. return $this->valid;
  144. }
  145. while (true) {
  146. while (true) {
  147. if ($this->exists === false || $_template->smarty->force_compile || $_template->smarty->force_cache) {
  148. $this->valid = false;
  149. } else {
  150. $this->valid = true;
  151. }
  152. if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT &&
  153. $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)
  154. ) {
  155. // lifetime expired
  156. $this->valid = false;
  157. }
  158. if ($this->valid && $_template->smarty->compile_check == 1 &&
  159. $_template->source->getTimeStamp() > $this->timestamp
  160. ) {
  161. $this->valid = false;
  162. }
  163. if ($this->valid || !$_template->smarty->cache_locking) {
  164. break;
  165. }
  166. if (!$this->handler->locked($_template->smarty, $this)) {
  167. $this->handler->acquireLock($_template->smarty, $this);
  168. break 2;
  169. }
  170. $this->handler->populate($this, $_template);
  171. }
  172. if ($this->valid) {
  173. if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
  174. // load cache file for the following checks
  175. if ($_template->smarty->debugging) {
  176. $_template->smarty->_debug->start_cache($_template);
  177. }
  178. if ($this->handler->process($_template, $this) === false) {
  179. $this->valid = false;
  180. } else {
  181. $this->processed = true;
  182. }
  183. if ($_template->smarty->debugging) {
  184. $_template->smarty->_debug->end_cache($_template);
  185. }
  186. } else {
  187. $this->is_locked = true;
  188. continue;
  189. }
  190. } else {
  191. return $this->valid;
  192. }
  193. if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED &&
  194. $_template->cached->cache_lifetime >= 0 &&
  195. (time() > ($_template->cached->timestamp + $_template->cached->cache_lifetime))
  196. ) {
  197. $this->valid = false;
  198. }
  199. if ($_template->smarty->cache_locking) {
  200. if (!$this->valid) {
  201. $this->handler->acquireLock($_template->smarty, $this);
  202. } elseif ($this->is_locked) {
  203. $this->handler->releaseLock($_template->smarty, $this);
  204. }
  205. }
  206. return $this->valid;
  207. }
  208. return $this->valid;
  209. }
  210. /**
  211. * Process cached template
  212. *
  213. * @param Smarty_Internal_Template $_template template object
  214. * @param bool $update flag if called because cache update
  215. */
  216. public function process(Smarty_Internal_Template $_template, $update = false)
  217. {
  218. if ($this->handler->process($_template, $this, $update) === false) {
  219. $this->valid = false;
  220. }
  221. if ($this->valid) {
  222. $this->processed = true;
  223. } else {
  224. $this->processed = false;
  225. }
  226. }
  227. /**
  228. * Read cache content from handler
  229. *
  230. * @param Smarty_Internal_Template $_template template object
  231. *
  232. * @return string|false content
  233. */
  234. public function read(Smarty_Internal_Template $_template)
  235. {
  236. if (!$_template->source->handler->recompiled) {
  237. return $this->handler->readCachedContent($_template);
  238. }
  239. return false;
  240. }
  241. }