ParseTemplateBehavior.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * Behavior of the system expansion: template parsing
  12. * @category Sen
  13. * @package Sen
  14. * @subpackage Behavior
  15. * @author ms134n <[email protected]>
  16. */
  17. class ParseTemplateBehavior extends Behavior {
  18. // Behavioral parameters defined ( the default value ) Covered in the project configuration
  19. protected $options = array(
  20. // Layout Settings
  21. 'TMPL_ENGINE_TYPE' => 'Sen', // The default template engine The following settings are only valid with Sen template engine
  22. 'TMPL_CACHFILE_SUFFIX' => '.php', // The default template cache suffix
  23. 'TMPL_DENY_FUNC_LIST' => 'echo,exit', // Disable function template engine
  24. 'TMPL_DENY_PHP' => false, // Whether to disable the default template engine native PHP code
  25. 'TMPL_L_DELIM' => '{', // Generic tag template engine start tag
  26. 'TMPL_R_DELIM' => '}', // Generic tag template engine end tag
  27. 'TMPL_VAR_IDENTIFY' => 'array', // Template variables identified . Blank automatically determine, parameters 'obj' indicates that the object
  28. 'TMPL_STRIP_SPACE' => true, // Removal of HTML template files spaces and line
  29. 'TMPL_CACHE_ON' => true, // Open the compiled template caching, set to false then every will be recompiled
  30. 'TMPL_CACHE_PREFIX' => '', // Template cache prefix designation can be changed dynamically
  31. 'TMPL_CACHE_TIME' => 0, // Template cache validity 0 Permanent, (In figures, the unit: Second)
  32. 'TMPL_LAYOUT_ITEM' => '{__CONTENT__}', // Replace the contents of the layout template logo
  33. 'LAYOUT_ON' => false, // Whether to enable layout
  34. 'LAYOUT_NAME' => 'layout', // Name of the current layout Default layout
  35. // Sen template engine tag library related settings
  36. 'TAGLIB_BEGIN' => '<', // Start tag tag tag library
  37. 'TAGLIB_END' => '>', // End tag tag tag library
  38. 'TAGLIB_LOAD' => true, // Whether to use the built-in tag library tag library other than the default auto-detection
  39. 'TAGLIB_BUILD_IN' => 'cx', // Built-in tag library name(Use of the tag does not have to specify the name of the tag library), separated by commas Note resolution order
  40. 'TAGLIB_PRE_LOAD' => '', // Require additional tag libraries loaded(Must specify the name of the tag library), multiple comma-separated
  41. );
  42. // Behavior extension execution entry must be run
  43. public function run(&$_data){
  44. $engine = strtolower(C('TMPL_ENGINE_TYPE'));
  45. $_content = empty($_data['content'])?$_data['file']:$_data['content'];
  46. $_data['prefix'] = !empty($_data['prefix'])?$_data['prefix']:C('TMPL_CACHE_PREFIX');
  47. if('sen'==$engine){ // Template engine using Sen
  48. if((!empty($_data['content']) && $this->checkContentCache($_data['content'],$_data['prefix']))
  49. || $this->checkCache($_data['file'],$_data['prefix'])) { // Cache is valid
  50. // Decomposition and load the template cache variables
  51. extract($_data['var'], EXTR_OVERWRITE);
  52. //Load template cache files
  53. include C('CACHE_PATH').$_data['prefix'].md5($_content).C('TMPL_CACHFILE_SUFFIX');
  54. }else{
  55. $tpl = Sen::instance('SenTemplate');
  56. // Compile and load the template file
  57. $tpl->fetch($_content,$_data['var'],$_data['prefix']);
  58. }
  59. }else{
  60. // Called third-party template engine to parse and output
  61. $class = 'Template'.ucwords($engine);
  62. if(class_exists($class)) {
  63. $tpl = new $class;
  64. $tpl->fetch($_content,$_data['var']);
  65. }else { // Class does not define
  66. throw_exception(L('_NOT_SUPPERT_').': ' . $class);
  67. }
  68. }
  69. }
  70. /**
  71. * Check the cache file is valid
  72. * If this does not need to be recompiled
  73. * @access public
  74. * @param string $tmplTemplateFile Template file name
  75. * @return boolen
  76. */
  77. protected function checkCache($tmplTemplateFile,$prefix='') {
  78. if (!C('TMPL_CACHE_ON')) // Preferentially detect configuration settings
  79. return false;
  80. $tmplCacheFile = C('CACHE_PATH').$prefix.md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
  81. if(!is_file($tmplCacheFile)){
  82. return false;
  83. }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
  84. // Template files if you need to update the cache updates
  85. return false;
  86. }elseif (C('TMPL_CACHE_TIME') != 0 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
  87. // Caching is within the validity
  88. return false;
  89. }
  90. // Open layout templates
  91. if(C('LAYOUT_ON')) {
  92. $layoutFile = THEME_PATH.C('LAYOUT_NAME').C('TMPL_TEMPLATE_SUFFIX');
  93. if(filemtime($layoutFile) > filemtime($tmplCacheFile)) {
  94. return false;
  95. }
  96. }
  97. // Cache is valid
  98. return true;
  99. }
  100. /**
  101. * Check the contents of the cache is valid
  102. * If this does not need to be recompiled
  103. * @access public
  104. * @param string $tmplContent Template content
  105. * @return boolen
  106. */
  107. protected function checkContentCache($tmplContent,$prefix='') {
  108. if(is_file(C('CACHE_PATH').$prefix.md5($tmplContent).C('TMPL_CACHFILE_SUFFIX'))){
  109. return true;
  110. }else{
  111. return false;
  112. }
  113. }
  114. }