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