/ThinkPHP/Lib/Core/Widget.class.php

https://gitlab.com/llwhois/woaimm · PHP · 107 lines · 54 code · 5 blank · 48 comment · 11 complexity · 41b5568c2ed950fd25b6a0a80606c16e MD5 · raw file

  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. /**
  12. * ThinkPHP Widget类 抽象类
  13. * @category Think
  14. * @package Think
  15. * @subpackage Core
  16. * @author liu21st <liu21st@gmail.com>
  17. */
  18. abstract class Widget {
  19. // 使用的模板引擎 每个Widget可以单独配置不受系统影响
  20. protected $template = '';
  21. /**
  22. * 渲染输出 render方法是Widget唯一的接口
  23. * 使用字符串返回 不能有任何输出
  24. * @access public
  25. * @param mixed $data 要渲染的数据
  26. * @return string
  27. */
  28. abstract public function render($data);
  29. /**
  30. * 渲染模板输出 供render方法内部调用
  31. * @access public
  32. * @param string $templateFile 模板文件
  33. * @param mixed $var 模板变量
  34. * @return string
  35. */
  36. protected function renderFile($templateFile='',$var='') {
  37. ob_start();
  38. ob_implicit_flush(0);
  39. if(!file_exists_case($templateFile)){
  40. // 自动定位模板文件
  41. $name = substr(get_class($this),0,-6);
  42. $filename = empty($templateFile)?$name:$templateFile;
  43. $templateFile = BASE_LIB_PATH.'Widget/'.$name.'/'.$filename.C('TMPL_TEMPLATE_SUFFIX');
  44. if(!file_exists_case($templateFile))
  45. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  46. }
  47. $template = strtolower($this->template?$this->template:(C('TMPL_ENGINE_TYPE')?C('TMPL_ENGINE_TYPE'):'php'));
  48. if('php' == $template) {
  49. // 使用PHP模板
  50. if(!empty($var)) extract($var, EXTR_OVERWRITE);
  51. // 直接载入PHP模板
  52. include $templateFile;
  53. }elseif('think'==$template){ // 采用Think模板引擎
  54. if($this->checkCache($templateFile)) { // 缓存有效
  55. // 分解变量并载入模板缓存
  56. extract($var, EXTR_OVERWRITE);
  57. //载入模版缓存文件
  58. include C('CACHE_PATH').md5($templateFile).C('TMPL_CACHFILE_SUFFIX');
  59. }else{
  60. $tpl = Think::instance('ThinkTemplate');
  61. // 编译并加载模板文件
  62. $tpl->fetch($templateFile,$var);
  63. }
  64. }else{
  65. $class = 'Template'.ucwords($template);
  66. if(is_file(CORE_PATH.'Driver/Template/'.$class.'.class.php')) {
  67. // 内置驱动
  68. $path = CORE_PATH;
  69. }else{ // 扩展驱动
  70. $path = EXTEND_PATH;
  71. }
  72. require_cache($path.'Driver/Template/'.$class.'.class.php');
  73. $tpl = new $class;
  74. $tpl->fetch($templateFile,$var);
  75. }
  76. $content = ob_get_clean();
  77. return $content;
  78. }
  79. /**
  80. * 检查缓存文件是否有效
  81. * 如果无效则需要重新编译
  82. * @access public
  83. * @param string $tmplTemplateFile 模板文件名
  84. * @return boolen
  85. */
  86. protected function checkCache($tmplTemplateFile) {
  87. if (!C('TMPL_CACHE_ON')) // 优先对配置设定检测
  88. return false;
  89. $tmplCacheFile = C('CACHE_PATH').md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
  90. if(!is_file($tmplCacheFile)){
  91. return false;
  92. }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
  93. // 模板文件如果有更新则缓存需要更新
  94. return false;
  95. }elseif (C('TMPL_CACHE_TIME') != 0 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
  96. // 缓存是否在有效期
  97. return false;
  98. }
  99. // 缓存有效
  100. return true;
  101. }
  102. }