ReadHtmlCacheBehavior.class.php 5.5 KB

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