ReadHtmlCacheBehavior.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. use Think\Storage;
  13. /**
  14. * 系统行为扩展:静态缓存读取
  15. */
  16. class ReadHtmlCacheBehavior
  17. {
  18. // 行为扩展的执行入口必须是run
  19. public function run(&$params)
  20. {
  21. // 开启静态缓存
  22. if (IS_GET && C('HTML_CACHE_ON')) {
  23. $cacheTime = $this->requireHtmlCache();
  24. if (false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME, $cacheTime)) {
  25. //静态页面有效
  26. // 读取静态页面输出
  27. echo Storage::read(HTML_FILE_NAME, 'html');
  28. exit();
  29. }
  30. }
  31. }
  32. // 判断是否需要静态缓存
  33. private static function requireHtmlCache()
  34. {
  35. // 分析当前的静态规则
  36. $htmls = C('HTML_CACHE_RULES'); // 读取静态规则
  37. if (!empty($htmls)) {
  38. $htmls = array_change_key_case($htmls);
  39. // 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则')
  40. // 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
  41. // 检测静态规则
  42. $controllerName = strtolower(CONTROLLER_NAME);
  43. $actionName = strtolower(ACTION_NAME);
  44. if (isset($htmls[$controllerName . ':' . $actionName])) {
  45. $html = $htmls[$controllerName . ':' . $actionName]; // 某个控制器的操作的静态规则
  46. } elseif (isset($htmls[$controllerName . ':'])) {
  47. // 某个控制器的静态规则
  48. $html = $htmls[$controllerName . ':'];
  49. } elseif (isset($htmls[$actionName])) {
  50. $html = $htmls[$actionName]; // 所有操作的静态规则
  51. } elseif (isset($htmls['*'])) {
  52. $html = $htmls['*']; // 全局静态规则
  53. }
  54. if (!empty($html)) {
  55. // 解读静态规则
  56. $rule = is_array($html) ? $html[0] : $html;
  57. // 以$_开头的系统变量
  58. $callback = function ($match) {
  59. switch ($match[1]) {
  60. case '_GET':$var = $_GET[$match[2]];
  61. break;
  62. case '_POST':$var = $_POST[$match[2]];
  63. break;
  64. case '_REQUEST':$var = $_REQUEST[$match[2]];
  65. break;
  66. case '_SERVER':$var = $_SERVER[$match[2]];
  67. break;
  68. case '_SESSION':$var = $_SESSION[$match[2]];
  69. break;
  70. case '_COOKIE':$var = $_COOKIE[$match[2]];
  71. break;
  72. }
  73. return (count($match) == 4) ? $match[3]($var) : $var;
  74. };
  75. $rule = preg_replace_callback('/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/', $callback, $rule);
  76. // {ID|FUN} GET变量的简写
  77. $rule = preg_replace_callback('/{(\w+)\|(\w+)}/', function ($match) {return $match[2]($_GET[$match[1]]);}, $rule);
  78. $rule = preg_replace_callback('/{(\w+)}/', function ($match) {return $_GET[$match[1]];}, $rule);
  79. // 特殊系统变量
  80. $rule = str_ireplace(
  81. array('{:controller}', '{:action}', '{:module}'),
  82. array(CONTROLLER_NAME, ACTION_NAME, MODULE_NAME),
  83. $rule);
  84. // {|FUN} 单独使用函数
  85. $rule = preg_replace_callback('/{|(\w+)}/', function ($match) {return $match[1]();}, $rule);
  86. $cacheTime = C('HTML_CACHE_TIME', null, 60);
  87. if (is_array($html)) {
  88. if (!empty($html[2])) {
  89. $rule = $html[2]($rule);
  90. }
  91. // 应用附加函数
  92. $cacheTime = isset($html[1]) ? $html[1] : $cacheTime; // 缓存有效期
  93. } else {
  94. $cacheTime = $cacheTime;
  95. }
  96. // 当前缓存文件
  97. define('HTML_FILE_NAME', HTML_PATH . $rule . C('HTML_FILE_SUFFIX', null, '.html'));
  98. return $cacheTime;
  99. }
  100. }
  101. // 无需缓存
  102. return false;
  103. }
  104. /**
  105. * 检查静态HTML文件是否有效
  106. * 如果无效需要重新更新
  107. * @access public
  108. * @param string $cacheFile 静态文件名
  109. * @param integer $cacheTime 缓存有效期
  110. * @return boolean
  111. */
  112. public static function checkHTMLCache($cacheFile = '', $cacheTime = '')
  113. {
  114. if (!is_file($cacheFile) && 'sae' != APP_MODE) {
  115. return false;
  116. } elseif (filemtime(\Think\Think::instance('Think\View')->parseTemplate()) > Storage::get($cacheFile, 'mtime', 'html')) {
  117. // 模板文件如果更新静态文件需要更新
  118. return false;
  119. } elseif (!is_numeric($cacheTime) && function_exists($cacheTime)) {
  120. return $cacheTime($cacheFile);
  121. } elseif (0 != $cacheTime && NOW_TIME > Storage::get($cacheFile, 'mtime', 'html') + $cacheTime) {
  122. // 文件是否在有效期
  123. return false;
  124. }
  125. //静态文件有效
  126. return true;
  127. }
  128. }