ParseTemplateBehavior.class.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Behavior;
  12. use Think\Storage;
  13. use Think\Think;
  14. /**
  15. * 系统行为扩展:模板解析
  16. */
  17. class ParseTemplateBehavior {
  18. // 行为扩展的执行入口必须是run
  19. public function run(&$_data){
  20. $engine = strtolower(C('TMPL_ENGINE_TYPE'));
  21. $_content = empty($_data['content'])?$_data['file']:$_data['content'];
  22. $_data['prefix'] = !empty($_data['prefix'])?$_data['prefix']:C('TMPL_CACHE_PREFIX');
  23. if('think'==$engine){ // 采用Think模板引擎
  24. if((!empty($_data['content']) && $this->checkContentCache($_data['content'],$_data['prefix']))
  25. || $this->checkCache($_data['file'],$_data['prefix'])) { // 缓存有效
  26. //载入模版缓存文件
  27. Storage::load(C('CACHE_PATH').$_data['prefix'].md5($_content).C('TMPL_CACHFILE_SUFFIX'),$_data['var']);
  28. }else{
  29. $tpl = Think::instance('Think\\Template');
  30. // 编译并加载模板文件
  31. $tpl->fetch($_content,$_data['var'],$_data['prefix']);
  32. }
  33. }else{
  34. // 调用第三方模板引擎解析和输出
  35. if(strpos($engine,'\\')){
  36. $class = $engine;
  37. }else{
  38. $class = 'Think\\Template\\Driver\\'.ucwords($engine);
  39. }
  40. if(class_exists($class)) {
  41. $tpl = new $class;
  42. $tpl->fetch($_content,$_data['var']);
  43. }else { // 类没有定义
  44. E(L('_NOT_SUPPORT_').': ' . $class);
  45. }
  46. }
  47. }
  48. /**
  49. * 检查缓存文件是否有效
  50. * 如果无效则需要重新编译
  51. * @access public
  52. * @param string $tmplTemplateFile 模板文件名
  53. * @return boolean
  54. */
  55. protected function checkCache($tmplTemplateFile,$prefix='') {
  56. if (!C('TMPL_CACHE_ON')) // 优先对配置设定检测
  57. return false;
  58. $tmplCacheFile = C('CACHE_PATH').$prefix.md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
  59. if(!Storage::has($tmplCacheFile)){
  60. return false;
  61. }elseif (filemtime($tmplTemplateFile) > Storage::get($tmplCacheFile,'mtime')) {
  62. // 模板文件如果有更新则缓存需要更新
  63. return false;
  64. }elseif (C('TMPL_CACHE_TIME') != 0 && time() > Storage::get($tmplCacheFile,'mtime')+C('TMPL_CACHE_TIME')) {
  65. // 缓存是否在有效期
  66. return false;
  67. }
  68. // 开启布局模板
  69. if(C('LAYOUT_ON')) {
  70. $layoutFile = THEME_PATH.C('LAYOUT_NAME').C('TMPL_TEMPLATE_SUFFIX');
  71. if(filemtime($layoutFile) > Storage::get($tmplCacheFile,'mtime')) {
  72. return false;
  73. }
  74. }
  75. // 缓存有效
  76. return true;
  77. }
  78. /**
  79. * 检查缓存内容是否有效
  80. * 如果无效则需要重新编译
  81. * @access public
  82. * @param string $tmplContent 模板内容
  83. * @return boolean
  84. */
  85. protected function checkContentCache($tmplContent,$prefix='') {
  86. if(Storage::has(C('CACHE_PATH').$prefix.md5($tmplContent).C('TMPL_CACHFILE_SUFFIX'))){
  87. return true;
  88. }else{
  89. return false;
  90. }
  91. }
  92. }