Widget.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  10. * Senthot Widget class Abstract class
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <[email protected]>
  15. */
  16. abstract class Widget {
  17. // Use template engine Each Widget can be individually configured without affecting the system
  18. protected $template = '';
  19. /**
  20. * Render Output render method is unique interface Widget
  21. * Use string to return Can not have any output
  22. * @access public
  23. * @param mixed $data The data to be rendered
  24. * @return string
  25. */
  26. abstract public function render($data);
  27. /**
  28. * Render the template output For the render method is called internally
  29. * @access public
  30. * @param string $templateFile Template File
  31. * @param mixed $var Template Variables
  32. * @return string
  33. */
  34. protected function renderFile($templateFile='',$var='') {
  35. ob_start();
  36. ob_implicit_flush(0);
  37. if(!file_exists_case($templateFile)){
  38. // Automatic positioning template file
  39. $name = substr(get_class($this),0,-6);
  40. $filename = empty($templateFile)?$name:$templateFile;
  41. $templateFile = BASE_LIB_PATH.'Widget/'.$name.'/'.$filename.C('TMPL_TEMPLATE_SUFFIX');
  42. if(!file_exists_case($templateFile))
  43. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  44. }
  45. $template = strtolower($this->template?$this->template:(C('TMPL_ENGINE_TYPE')?C('TMPL_ENGINE_TYPE'):'php'));
  46. if('php' == $template) {
  47. // Using PHP template
  48. if(!empty($var)) extract($var, EXTR_OVERWRITE);
  49. // PHP template loaded directly
  50. include $templateFile;
  51. }elseif('sen'==$template){ // Template engine using Sen
  52. if($this->checkCache($templateFile)) { // Cache is valid
  53. // Decomposition and load the template cache variables
  54. extract($var, EXTR_OVERWRITE);
  55. //Load template cache files
  56. include C('CACHE_PATH').md5($templateFile).C('TMPL_CACHFILE_SUFFIX');
  57. }else{
  58. $tpl = Sen::instance('SenTemplate');
  59. // Compile and load the template file
  60. $tpl->fetch($templateFile,$var);
  61. }
  62. }else{
  63. $class = 'Template'.ucwords($template);
  64. if(is_file(CORE_PATH.'Driver/Template/'.$class.'.class.php')) {
  65. // Internal Drive
  66. $path = CORE_PATH;
  67. }else{ // Expansion Drive
  68. $path = ADDONS_PATH;
  69. }
  70. require_cache($path.'Driver/Template/'.$class.'.class.php');
  71. $tpl = new $class;
  72. $tpl->fetch($templateFile,$var);
  73. }
  74. $content = ob_get_clean();
  75. return $content;
  76. }
  77. /**
  78. * Check the cache file is valid
  79. * If this does not need to be recompiled
  80. * @access public
  81. * @param string $tmplTemplateFile Template file name
  82. * @return boolen
  83. */
  84. protected function checkCache($tmplTemplateFile) {
  85. if (!C('TMPL_CACHE_ON')) // Preferentially detect configuration settings
  86. return false;
  87. $tmplCacheFile = C('CACHE_PATH').md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
  88. if(!is_file($tmplCacheFile)){
  89. return false;
  90. }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
  91. // Template files if you need to update the cache updates
  92. return false;
  93. }elseif (C('TMPL_CACHE_TIME') != 0 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
  94. // Caching is within the validity
  95. return false;
  96. }
  97. // Cache is valid
  98. return true;
  99. }
  100. }