ReadHtmlCacheBehavior.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. defined('SEN_PATH') or exit();
  10. /**
  11. * System behavior extension : static cache reads
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Behavior
  15. * @author ms134n <[email protected]>
  16. */
  17. class ReadHtmlCacheBehavior extends Behavior {
  18. protected $options = array(
  19. 'HTML_CACHE_ON' => false,
  20. 'HTML_CACHE_TIME' => 60,
  21. 'HTML_CACHE_RULES' => array(),
  22. 'HTML_FILE_SUFFIX' => '.html',
  23. );
  24. // Behavior extension execution entry must be run
  25. public function run(&$params){
  26. // On static cache
  27. if(C('HTML_CACHE_ON')) {
  28. $cacheTime = $this->requireHtmlCache();
  29. if( false !== $cacheTime && $this->checkHTMLCache(HTML_FILE_NAME,$cacheTime)) { //Effective static pages
  30. // Read static page output
  31. readfile(HTML_FILE_NAME);
  32. exit();
  33. }
  34. }
  35. }
  36. // Determine whether you need static cache
  37. static private function requireHtmlCache() {
  38. // Static analysis of the current rules
  39. $htmls = C('HTML_CACHE_RULES'); // Read static rule
  40. if(!empty($htmls)) {
  41. $htmls = array_change_key_case($htmls);
  42. // Static rules file defines the format actionName=>array('Static rule','Cache Time','Additional rules')
  43. // 'read'=>array('{id},{name}',60,'md5') Must ensure the uniqueness of static rules and can be judged
  44. // Detect static rule
  45. $moduleName = strtolower(MODULE_NAME);
  46. $actionName = strtolower(ACTION_NAME);
  47. if(isset($htmls[$moduleName.':'.$actionName])) {
  48. $html = $htmls[$moduleName.':'.$actionName]; // Operation of a module static rule
  49. }elseif(isset($htmls[$moduleName.':'])){// A module static rules
  50. $html = $htmls[$moduleName.':'];
  51. }elseif(isset($htmls[$actionName])){
  52. $html = $htmls[$actionName]; // All static rules
  53. }elseif(isset($htmls['*'])){
  54. $html = $htmls['*']; // Global static rules
  55. }elseif(isset($htmls['empty:index']) && !class_exists(MODULE_NAME.'Action')){
  56. $html = $htmls['empty:index']; // Empty module static rules
  57. }elseif(isset($htmls[$moduleName.':_empty']) && $this->isEmptyAction(MODULE_NAME,ACTION_NAME)){
  58. $html = $htmls[$moduleName.':_empty']; // Space operation static rules
  59. }
  60. if(!empty($html)) {
  61. // Understanding static rules
  62. $rule = $html[0];
  63. // By $_ the beginning of system variables
  64. $rule = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);
  65. $rule = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);
  66. // {ID|FUN} GET variable DateFormat
  67. $rule = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);
  68. $rule = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);
  69. // Special system variables
  70. $rule = str_ireplace(
  71. array('{:app}','{:module}','{:action}','{:group}'),
  72. array(APP_NAME,MODULE_NAME,ACTION_NAME,defined('GROUP_NAME')?GROUP_NAME:''),
  73. $rule);
  74. // {|FUN} Alone function
  75. $rule = preg_replace('/{|(\w+)}/e',"\\1()",$rule);
  76. if(!empty($html[2])) $rule = $html[2]($rule); // Apply additional functions
  77. $cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // Cache validity period
  78. // The current cache file
  79. define('HTML_FILE_NAME',HTML_PATH . $rule.C('HTML_FILE_SUFFIX'));
  80. return $cacheTime;
  81. }
  82. }
  83. // No cache
  84. return false;
  85. }
  86. /**
  87. * Check effectiveness of static HTML files
  88. * If you need to update
  89. * @access public
  90. * @param string $cacheFile A static file name
  91. * @param integer $cacheTime Cache validity period
  92. * @return boolen
  93. */
  94. static public function checkHTMLCache($cacheFile='',$cacheTime='') {
  95. if(!is_file($cacheFile)){
  96. return false;
  97. }elseif (filemtime(C('TEMPLATE_NAME')) > filemtime($cacheFile)) {
  98. // Static files are template files if the update needs to be updated
  99. return false;
  100. }elseif(!is_numeric($cacheTime) && function_exists($cacheTime)){
  101. return $cacheTime($cacheFile);
  102. }elseif ($cacheTime != 0 && NOW_TIME > filemtime($cacheFile)+$cacheTime) {
  103. // File is valid
  104. return false;
  105. }
  106. //Static files
  107. return true;
  108. }
  109. //Test whether the action is empty
  110. static private function isEmptyAction($module,$action) {
  111. $className = $module.'Action';
  112. $class = new $className;
  113. return !method_exists($class,$action);
  114. }
  115. }