ReadHtmlCacheBehavior.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 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. defined('THINK_PATH') or exit();
  12. /**
  13. * 系统行为扩展:静态缓存读取
  14. * @category Think
  15. * @package Think
  16. * @subpackage Behavior
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class ReadHtmlCacheBehavior extends Behavior {
  20. protected $options = array(
  21. 'HTML_CACHE_ON' => false,
  22. 'HTML_CACHE_TIME' => 60,
  23. 'HTML_CACHE_RULES' => array(),
  24. 'HTML_FILE_SUFFIX' => '.html',
  25. );
  26. // 行为扩展的执行入口必须是run
  27. public function run(&$params){
  28. // 开启静态缓存
  29. if(C('HTML_CACHE_ON')) {
  30. $cacheTime = $this->requireHtmlCache();
  31. if( false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME,$cacheTime)) { //静态页面有效
  32. // 读取静态页面输出
  33. readfile(HTML_FILE_NAME);
  34. exit();
  35. }
  36. }
  37. }
  38. // 判断是否需要静态缓存
  39. static private function requireHtmlCache() {
  40. // 分析当前的静态规则
  41. $htmls = C('HTML_CACHE_RULES'); // 读取静态规则
  42. if(!empty($htmls)) {
  43. $htmls = array_change_key_case($htmls);
  44. // 静态规则文件定义格式 actionName=>array('静态规则','缓存时间','附加规则')
  45. // 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
  46. // 检测静态规则
  47. $moduleName = strtolower(MODULE_NAME);
  48. $actionName = strtolower(ACTION_NAME);
  49. if(isset($htmls[$moduleName.':'.$actionName])) {
  50. $html = $htmls[$moduleName.':'.$actionName]; // 某个模块的操作的静态规则
  51. }elseif(isset($htmls[$moduleName.':'])){// 某个模块的静态规则
  52. $html = $htmls[$moduleName.':'];
  53. }elseif(isset($htmls[$actionName])){
  54. $html = $htmls[$actionName]; // 所有操作的静态规则
  55. }elseif(isset($htmls['*'])){
  56. $html = $htmls['*']; // 全局静态规则
  57. }elseif(isset($htmls['empty:index']) && !class_exists(MODULE_NAME.'Action')){
  58. $html = $htmls['empty:index']; // 空模块静态规则
  59. }elseif(isset($htmls[$moduleName.':_empty']) && self::isEmptyAction(MODULE_NAME,ACTION_NAME)){
  60. $html = $htmls[$moduleName.':_empty']; // 空操作静态规则
  61. }
  62. if(!empty($html)) {
  63. // 解读静态规则
  64. $rule = $html[0];
  65. // 以$_开头的系统变量
  66. $rule = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);
  67. $rule = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);
  68. // {ID|FUN} GET变量的简写
  69. $rule = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);
  70. $rule = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);
  71. // 特殊系统变量
  72. $rule = str_ireplace(
  73. array('{:app}','{:module}','{:action}','{:group}'),
  74. array(APP_NAME,MODULE_NAME,ACTION_NAME,defined('GROUP_NAME')?GROUP_NAME:''),
  75. $rule);
  76. // {|FUN} 单独使用函数
  77. $rule = preg_replace('/{|(\w+)}/e',"\\1()",$rule);
  78. if(!empty($html[2])) $rule = $html[2]($rule); // 应用附加函数
  79. $cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // 缓存有效期
  80. // 当前缓存文件
  81. define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));
  82. return $cacheTime;
  83. }
  84. }
  85. // 无需缓存
  86. return false;
  87. }
  88. /**
  89. * 检查静态HTML文件是否有效
  90. * 如果无效需要重新更新
  91. * @access public
  92. * @param string $cacheFile 静态文件名
  93. * @param integer $cacheTime 缓存有效期
  94. * @return boolean
  95. */
  96. static public function checkHTMLCache($cacheFile='',$cacheTime='') {
  97. if(!is_file($cacheFile)){
  98. return false;
  99. }elseif (filemtime(C('TEMPLATE_NAME')) > filemtime($cacheFile)) {
  100. // 模板文件如果更新静态文件需要更新
  101. return false;
  102. }elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){
  103. return $cacheTime($cacheFile);
  104. }elseif ($cacheTime != 0 && NOW_TIME > filemtime($cacheFile)+$cacheTime) {
  105. // 文件是否在有效期
  106. return false;
  107. }
  108. //静态文件有效
  109. return true;
  110. }
  111. //检测是否是空操作
  112. static private function isEmptyAction($module,$action) {
  113. $className = $module.'Action';
  114. $class = new $className;
  115. return !method_exists($class,$action);
  116. }
  117. }