WriteHtmlCacheBehavior.class.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  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 WriteHtmlCacheBehavior extends Behavior {
  20. // 行为扩展的执行入口必须是run
  21. public function run(&$content){
  22. if(C('HTML_CACHE_ON') && defined('HTML_FILE_NAME')) {
  23. //静态文件写入
  24. // 如果开启HTML功能 检查并重写HTML文件
  25. // 没有模版的操作不生成静态文件
  26. if(!is_dir(dirname(HTML_FILE_NAME)))
  27. mkdir(dirname(HTML_FILE_NAME),0755,true);
  28. if( false === file_put_contents( HTML_FILE_NAME , $content ))
  29. throw_exception(L('_CACHE_WRITE_ERROR_').':'.HTML_FILE_NAME);
  30. }
  31. }
  32. }