HtmlCache.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * 静态缓存类
  15. * 支持静态缓存规则定义
  16. +------------------------------------------------------------------------------
  17. * @category Think
  18. * @package Think
  19. * @subpackage Util
  20. * @author liu21st <liu21st@gmail.com>
  21. * @version $Id$
  22. +------------------------------------------------------------------------------
  23. */
  24. class HtmlCache extends Think
  25. {
  26. static private $cacheTime = null; // 缓存有效期(支持函数)
  27. static private $requireCache = false; // 是否需要缓存
  28. // 判断是否需要静态缓存
  29. static private function requireHtmlCache() {
  30. // 分析当前的静态规则
  31. $htmls = C('_htmls_'); // 读取静态规则
  32. if(!empty($htmls)) {
  33. // 静态规则文件定义格式 actionName=>array(‘静态规则’,’缓存时间’,’附加规则')
  34. // 'read'=>array('{id},{name}',60,'md5') 必须保证静态规则的唯一性 和 可判断性
  35. // 检测静态规则
  36. if(isset($htmls[MODULE_NAME.':'.ACTION_NAME])) {
  37. $html = $htmls[MODULE_NAME.':'.ACTION_NAME]; // 某个模块的操作的静态规则
  38. }elseif(isset($htmls[MODULE_NAME.':'])){// 某个模块的静态规则
  39. $html = $htmls[MODULE_NAME.':'];
  40. }elseif(isset($htmls[ACTION_NAME])){
  41. $html = $htmls[ACTION_NAME]; // 所有操作的静态规则
  42. }elseif(isset($htmls['*'])){
  43. $html = $htmls['*']; // 全局静态规则
  44. }elseif(isset($htmls['Empty:index']) && !class_exists(MODULE_NAME.'Action')){
  45. $html = $htmls['Empty:index']; // 空模块静态规则
  46. }elseif(isset($htmls[MODULE_NAME.':_empty']) && self::isEmptyAction(MODULE_NAME,ACTION_NAME)){
  47. $html = $htmls[MODULE_NAME.':_empty']; // 空操作静态规则
  48. }
  49. if(!empty($html)) {
  50. self::$requireCache = true; // 需要缓存
  51. // 解读静态规则
  52. $rule = $html[0];
  53. // 以$_开头的系统变量
  54. $rule = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);
  55. $rule = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);
  56. // {ID|FUN} GET变量的简写
  57. $rule = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);
  58. $rule = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);
  59. // 特殊系统变量
  60. $rule = str_ireplace(
  61. array('{:app}','{:module}','{:action}','{:group}'),
  62. array(APP_NAME,MODULE_NAME,ACTION_NAME,GROUP_NAME),
  63. $rule);
  64. // {|FUN} 单独使用函数
  65. $rule = preg_replace('/{|(\w+)}/e',"\\1()",$rule);
  66. if(!empty($html[2])) $rule = $html[2]($rule); // 应用附加函数
  67. self::$cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // 缓存有效期
  68. // 当前缓存文件
  69. define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));
  70. return true;
  71. }
  72. }
  73. // 无需缓存
  74. return false;
  75. }
  76. /**
  77. +----------------------------------------------------------
  78. * 读取静态缓存
  79. +----------------------------------------------------------
  80. * @access static
  81. +----------------------------------------------------------
  82. * @return void
  83. +----------------------------------------------------------
  84. */
  85. static function readHTMLCache()
  86. {
  87. if(self::requireHtmlCache() && self::checkHTMLCache(HTML_FILE_NAME,self::$cacheTime)) { //静态页面有效
  88. if(C('HTML_READ_TYPE')==1) {
  89. // 重定向到静态页面
  90. redirect(str_replace(array(realpath($_SERVER["DOCUMENT_ROOT"]),"\\"),array('',"/"),realpath(HTML_FILE_NAME)));
  91. }else {
  92. // 读取静态页面输出
  93. readfile(HTML_FILE_NAME);
  94. exit();
  95. }
  96. }
  97. return ;
  98. }
  99. /**
  100. +----------------------------------------------------------
  101. * 写入静态缓存
  102. +----------------------------------------------------------
  103. * @access public
  104. +----------------------------------------------------------
  105. * @param string $content 页面内容
  106. +----------------------------------------------------------
  107. * @return void
  108. +----------------------------------------------------------
  109. * @throws ThinkExecption
  110. +----------------------------------------------------------
  111. */
  112. static public function writeHTMLCache($content)
  113. {
  114. if(self::$requireCache) {
  115. //静态文件写入
  116. // 如果开启HTML功能 检查并重写HTML文件
  117. // 没有模版的操作不生成静态文件
  118. if(!is_dir(dirname(HTML_FILE_NAME)))
  119. mk_dir(dirname(HTML_FILE_NAME));
  120. if( false === file_put_contents( HTML_FILE_NAME , $content ))
  121. throw_exception(L('_CACHE_WRITE_ERROR_'));
  122. }
  123. return ;
  124. }
  125. /**
  126. +----------------------------------------------------------
  127. * 检查静态HTML文件是否有效
  128. * 如果无效需要重新更新
  129. +----------------------------------------------------------
  130. * @access public
  131. +----------------------------------------------------------
  132. * @param string $cacheFile 静态文件名
  133. * @param integer $cacheTime 缓存有效期
  134. +----------------------------------------------------------
  135. * @return boolen
  136. +----------------------------------------------------------
  137. */
  138. static public function checkHTMLCache($cacheFile='',$cacheTime='')
  139. {
  140. if(!is_file($cacheFile)){
  141. return false;
  142. }elseif (filemtime(C('TMPL_FILE_NAME')) > filemtime($cacheFile)) {
  143. // 模板文件如果更新静态文件需要更新
  144. return false;
  145. }elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){
  146. return $cacheTime($cacheFile);
  147. }elseif ($cacheTime != -1 && time() > filemtime($cacheFile)+$cacheTime) {
  148. // 文件是否在有效期
  149. return false;
  150. }
  151. //静态文件有效
  152. return true;
  153. }
  154. //检测是否是空操作
  155. static private function isEmptyAction($module,$action){
  156. $className = $module.'Action';
  157. $class=new $className;
  158. return !method_exists($class,$action);
  159. }
  160. }
  161. ?>