PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/smarty/sysplugins/smarty_cacheresource.php

https://gitlab.com/staging06/myproject
PHP | 442 lines | 176 code | 42 blank | 224 comment | 49 complexity | a7df8da2238da979063eff64af6dd7bf 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
  16. {
  17. /**
  18. * cache for Smarty_CacheResource instances
  19. *
  20. * @var array
  21. */
  22. public static $resources = array();
  23. /**
  24. * resource types provided by the core
  25. *
  26. * @var array
  27. */
  28. protected static $sysplugins = array(
  29. 'file' => true,
  30. );
  31. /**
  32. * populate Cached Object with meta data from Resource
  33. *
  34. * @param Smarty_Template_Cached $cached cached object
  35. * @param Smarty_Internal_Template $_template template object
  36. *
  37. * @return void
  38. */
  39. abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
  40. /**
  41. * populate Cached Object with timestamp and exists from Resource
  42. *
  43. * @param Smarty_Template_Cached $cached
  44. *
  45. * @return void
  46. */
  47. abstract public function populateTimestamp(Smarty_Template_Cached $cached);
  48. /**
  49. * Read the cached template and process header
  50. *
  51. * @param Smarty_Internal_Template $_template template object
  52. * @param Smarty_Template_Cached $cached cached object
  53. *
  54. * @return boolean true or false if the cached content does not exist
  55. */
  56. abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null);
  57. /**
  58. * Write the rendered template output to cache
  59. *
  60. * @param Smarty_Internal_Template $_template template object
  61. * @param string $content content to cache
  62. *
  63. * @return boolean success
  64. */
  65. abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content);
  66. /**
  67. * Return cached content
  68. *
  69. * @param Smarty_Internal_Template $_template template object
  70. *
  71. * @return null|string
  72. */
  73. public function getCachedContent(Smarty_Internal_Template $_template)
  74. {
  75. if ($_template->cached->handler->process($_template)) {
  76. ob_start();
  77. $_template->properties['unifunc']($_template);
  78. return ob_get_clean();
  79. }
  80. return null;
  81. }
  82. /**
  83. * Empty cache
  84. *
  85. * @param Smarty $smarty Smarty object
  86. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  87. *
  88. * @return integer number of cache files deleted
  89. */
  90. abstract public function clearAll(Smarty $smarty, $exp_time = null);
  91. /**
  92. * Empty cache for a specific template
  93. *
  94. * @param Smarty $smarty Smarty object
  95. * @param string $resource_name template name
  96. * @param string $cache_id cache id
  97. * @param string $compile_id compile id
  98. * @param integer $exp_time expiration time (number of seconds, not timestamp)
  99. *
  100. * @return integer number of cache files deleted
  101. */
  102. abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
  103. /**
  104. * @param Smarty $smarty
  105. * @param Smarty_Template_Cached $cached
  106. *
  107. * @return bool|null
  108. */
  109. public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
  110. {
  111. // theoretically locking_timeout should be checked against time_limit (max_execution_time)
  112. $start = microtime(true);
  113. $hadLock = null;
  114. while ($this->hasLock($smarty, $cached)) {
  115. $hadLock = true;
  116. if (microtime(true) - $start > $smarty->locking_timeout) {
  117. // abort waiting for lock release
  118. return false;
  119. }
  120. sleep(1);
  121. }
  122. return $hadLock;
  123. }
  124. /**
  125. * Check is cache is locked for this template
  126. *
  127. * @param Smarty $smarty
  128. * @param Smarty_Template_Cached $cached
  129. *
  130. * @return bool
  131. */
  132. public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
  133. {
  134. // check if lock exists
  135. return false;
  136. }
  137. /**
  138. * Lock cache for this template
  139. *
  140. * @param Smarty $smarty
  141. * @param Smarty_Template_Cached $cached
  142. *
  143. * @return bool
  144. */
  145. public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
  146. {
  147. // create lock
  148. return true;
  149. }
  150. /**
  151. * Unlock cache for this template
  152. *
  153. * @param Smarty $smarty
  154. * @param Smarty_Template_Cached $cached
  155. *
  156. * @return bool
  157. */
  158. public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
  159. {
  160. // release lock
  161. return true;
  162. }
  163. /**
  164. * Load Cache Resource Handler
  165. *
  166. * @param Smarty $smarty Smarty object
  167. * @param string $type name of the cache resource
  168. *
  169. * @throws SmartyException
  170. * @return Smarty_CacheResource Cache Resource Handler
  171. */
  172. public static function load(Smarty $smarty, $type = null)
  173. {
  174. if (!isset($type)) {
  175. $type = $smarty->caching_type;
  176. }
  177. // try smarty's cache
  178. if (isset($smarty->_cacheresource_handlers[$type])) {
  179. return $smarty->_cacheresource_handlers[$type];
  180. }
  181. // try registered resource
  182. if (isset($smarty->registered_cache_resources[$type])) {
  183. // do not cache these instances as they may vary from instance to instance
  184. return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type];
  185. }
  186. // try sysplugins dir
  187. if (isset(self::$sysplugins[$type])) {
  188. if (!isset(self::$resources[$type])) {
  189. $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
  190. self::$resources[$type] = new $cache_resource_class();
  191. }
  192. return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
  193. }
  194. // try plugins dir
  195. $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
  196. if ($smarty->loadPlugin($cache_resource_class)) {
  197. if (!isset(self::$resources[$type])) {
  198. self::$resources[$type] = new $cache_resource_class();
  199. }
  200. return $smarty->_cacheresource_handlers[$type] = self::$resources[$type];
  201. }
  202. // give up
  203. throw new SmartyException("Unable to load cache resource '{$type}'");
  204. }
  205. /**
  206. * Invalid Loaded Cache Files
  207. *
  208. * @param Smarty $smarty Smarty object
  209. */
  210. public static function invalidLoadedCache(Smarty $smarty)
  211. {
  212. foreach ($smarty->template_objects as $tpl) {
  213. if (isset($tpl->cached)) {
  214. $tpl->cached->valid = false;
  215. $tpl->cached->processed = false;
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Smarty Resource Data Object
  222. * Cache Data Container for Template Files
  223. *
  224. * @package Smarty
  225. * @subpackage TemplateResources
  226. * @author Rodney Rehm
  227. */
  228. class Smarty_Template_Cached
  229. {
  230. /**
  231. * Source Filepath
  232. *
  233. * @var string
  234. */
  235. public $filepath = false;
  236. /**
  237. * Source Content
  238. *
  239. * @var string
  240. */
  241. public $content = null;
  242. /**
  243. * Source Timestamp
  244. *
  245. * @var integer
  246. */
  247. public $timestamp = false;
  248. /**
  249. * Source Existence
  250. *
  251. * @var boolean
  252. */
  253. public $exists = false;
  254. /**
  255. * Cache Is Valid
  256. *
  257. * @var boolean
  258. */
  259. public $valid = false;
  260. /**
  261. * Cache was processed
  262. *
  263. * @var boolean
  264. */
  265. public $processed = false;
  266. /**
  267. * CacheResource Handler
  268. *
  269. * @var Smarty_CacheResource
  270. */
  271. public $handler = null;
  272. /**
  273. * Template Compile Id (Smarty_Internal_Template::$compile_id)
  274. *
  275. * @var string
  276. */
  277. public $compile_id = null;
  278. /**
  279. * Template Cache Id (Smarty_Internal_Template::$cache_id)
  280. *
  281. * @var string
  282. */
  283. public $cache_id = null;
  284. /**
  285. * Id for cache locking
  286. *
  287. * @var string
  288. */
  289. public $lock_id = null;
  290. /**
  291. * flag that cache is locked by this instance
  292. *
  293. * @var bool
  294. */
  295. public $is_locked = false;
  296. /**
  297. * Source Object
  298. *
  299. * @var Smarty_Template_Source
  300. */
  301. public $source = null;
  302. /**
  303. * create Cached Object container
  304. *
  305. * @param Smarty_Internal_Template $_template template object
  306. */
  307. public function __construct(Smarty_Internal_Template $_template)
  308. {
  309. $this->compile_id = $_template->compile_id;
  310. $this->cache_id = $_template->cache_id;
  311. $this->source = $_template->source;
  312. $_template->cached = $this;
  313. $smarty = $_template->smarty;
  314. //
  315. // load resource handler
  316. //
  317. $this->handler = $handler = Smarty_CacheResource::load($smarty); // Note: prone to circular references
  318. //
  319. // check if cache is valid
  320. //
  321. if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || $_template->source->recompiled) {
  322. $handler->populate($this, $_template);
  323. return;
  324. }
  325. while (true) {
  326. while (true) {
  327. $handler->populate($this, $_template);
  328. if ($this->timestamp === false || $smarty->force_compile || $smarty->force_cache) {
  329. $this->valid = false;
  330. } else {
  331. $this->valid = true;
  332. }
  333. if ($this->valid && $_template->caching == Smarty::CACHING_LIFETIME_CURRENT && $_template->cache_lifetime >= 0 && time() > ($this->timestamp + $_template->cache_lifetime)) {
  334. // lifetime expired
  335. $this->valid = false;
  336. }
  337. if ($this->valid || !$_template->smarty->cache_locking) {
  338. break;
  339. }
  340. if (!$this->handler->locked($_template->smarty, $this)) {
  341. $this->handler->acquireLock($_template->smarty, $this);
  342. break 2;
  343. }
  344. }
  345. if ($this->valid) {
  346. if (!$_template->smarty->cache_locking || $this->handler->locked($_template->smarty, $this) === null) {
  347. // load cache file for the following checks
  348. if ($smarty->debugging) {
  349. Smarty_Internal_Debug::start_cache($_template);
  350. }
  351. if ($handler->process($_template, $this) === false) {
  352. $this->valid = false;
  353. } else {
  354. $this->processed = true;
  355. }
  356. if ($smarty->debugging) {
  357. Smarty_Internal_Debug::end_cache($_template);
  358. }
  359. } else {
  360. continue;
  361. }
  362. } else {
  363. return;
  364. }
  365. if ($this->valid && $_template->caching === Smarty::CACHING_LIFETIME_SAVED && $_template->properties['cache_lifetime'] >= 0 && (time() > ($_template->cached->timestamp + $_template->properties['cache_lifetime']))) {
  366. $this->valid = false;
  367. }
  368. if (!$this->valid && $_template->smarty->cache_locking) {
  369. $this->handler->acquireLock($_template->smarty, $this);
  370. return;
  371. } else {
  372. return;
  373. }
  374. }
  375. }
  376. /**
  377. * Write this cache object to handler
  378. *
  379. * @param Smarty_Internal_Template $_template template object
  380. * @param string $content content to cache
  381. *
  382. * @return boolean success
  383. */
  384. public function write(Smarty_Internal_Template $_template, $content)
  385. {
  386. if (!$_template->source->recompiled) {
  387. if ($this->handler->writeCachedContent($_template, $content)) {
  388. $this->content = null;
  389. $this->timestamp = time();
  390. $this->exists = true;
  391. $this->valid = true;
  392. if ($_template->smarty->cache_locking) {
  393. $this->handler->releaseLock($_template->smarty, $this);
  394. }
  395. return true;
  396. }
  397. }
  398. return false;
  399. }
  400. }