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.6KB

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