core.read_cache_file.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * read a cache file, determine if it needs to be
  9. * regenerated or not
  10. *
  11. * @param string $tpl_file
  12. * @param string $cache_id
  13. * @param string $compile_id
  14. * @param string $results
  15. * @return boolean
  16. */
  17. // $tpl_file, $cache_id, $compile_id, &$results
  18. function smarty_core_read_cache_file(&$params, &$smarty)
  19. {
  20. static $content_cache = array();
  21. if ($smarty->force_compile) {
  22. // force compile enabled, always regenerate
  23. return false;
  24. }
  25. if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) {
  26. list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']];
  27. return true;
  28. }
  29. if (!empty($smarty->cache_handler_func)) {
  30. // use cache_handler function
  31. call_user_func_array($smarty->cache_handler_func,
  32. array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null));
  33. } else {
  34. // use local cache file
  35. $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']);
  36. $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id);
  37. $params['results'] = $smarty->_read_file($_cache_file);
  38. }
  39. if (empty($params['results'])) {
  40. // nothing to parse (error?), regenerate cache
  41. return false;
  42. }
  43. $_contents = $params['results'];
  44. $_info_start = strpos($_contents, "\n") + 1;
  45. $_info_len = (int)substr($_contents, 0, $_info_start - 1);
  46. $_cache_info = unserialize(substr($_contents, $_info_start, $_info_len));
  47. $params['results'] = substr($_contents, $_info_start + $_info_len);
  48. if ($smarty->caching == 2 && isset ($_cache_info['expires'])){
  49. // caching by expiration time
  50. if ($_cache_info['expires'] > -1 && (time() > $_cache_info['expires'])) {
  51. // cache expired, regenerate
  52. return false;
  53. }
  54. } else {
  55. // caching by lifetime
  56. if ($smarty->cache_lifetime > -1 && (time() - $_cache_info['timestamp'] > $smarty->cache_lifetime)) {
  57. // cache expired, regenerate
  58. return false;
  59. }
  60. }
  61. if ($smarty->compile_check) {
  62. $_params = array('get_source' => false, 'quiet'=>true);
  63. foreach (array_keys($_cache_info['template']) as $_template_dep) {
  64. $_params['resource_name'] = $_template_dep;
  65. if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
  66. // template file has changed, regenerate cache
  67. return false;
  68. }
  69. }
  70. if (isset($_cache_info['config'])) {
  71. $_params = array('resource_base_path' => $smarty->config_dir, 'get_source' => false, 'quiet'=>true);
  72. foreach (array_keys($_cache_info['config']) as $_config_dep) {
  73. $_params['resource_name'] = $_config_dep;
  74. if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) {
  75. // config file has changed, regenerate cache
  76. return false;
  77. }
  78. }
  79. }
  80. }
  81. $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info);
  82. $smarty->_cache_info = $_cache_info;
  83. return true;
  84. }
  85. /* vim: set expandtab: */
  86. ?>