PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/2010/end_system/library/endskin.php

http://endcms.googlecode.com/
PHP | 143 lines | 123 code | 15 blank | 5 comment | 18 complexity | a4840ca747cc9d575d33d4e4274e2940 MD5 | raw file
  1. <?php
  2. class EndSkin
  3. {
  4. public $vars = array();
  5. public $template_dir = './';
  6. public $cache_dir = './';
  7. public $default_template = '';
  8. public $compile_hook = '';
  9. function EndSkin($template_dir = NULL,$cache_dir = NULL)
  10. {
  11. if ($template_dir) $this->template_dir = $template_dir;
  12. if ($cache_dir) $this->cache_dir = $cache_dir;
  13. if (substr($this->template_dir,-1) != '/') $this->template_dir.='/';
  14. if (substr($this->cache_dir,-1) != '/') $this->cache_dir.='/';
  15. if (!is_dir($this->template_dir)) mkdir($this->template_dir);
  16. if (!is_dir($this->cache_dir)) mkdir($this->cache_dir);
  17. }
  18. function assign($key,$data = NULL)
  19. {
  20. if (is_array($key))
  21. {
  22. foreach($key as $_k=>$_v) $this->vars[$_k] = $_v;
  23. }
  24. else
  25. {
  26. $this->vars[$key] = $data;
  27. }
  28. }
  29. function display($__endskin__template = '')
  30. {
  31. if (!$__endskin__template) $__endskin__template = $this->default_template;
  32. extract($this->vars);
  33. include($this->_compile($__endskin__template));
  34. }
  35. function result($template = '')
  36. {
  37. if (!$template) $template = $this->default_template;
  38. ob_start();
  39. $this->display($template);
  40. $s = ob_get_contents();
  41. ob_end_clean();
  42. return $s;
  43. }
  44. private function _compile($template)
  45. {
  46. $filename = $this->template_dir.$template;
  47. $cache_filename = $this->cache_dir.md5($template);
  48. //if (file_exists($cache_filename) && filemtime($cache_filename) > filemtime($filename)) return $cache_filename;
  49. $page = $this->_get_template($template);
  50. //匹配变量
  51. if (preg_match_all('/\{(\$[a-zA-Z\_][a-zA-Z0-9\_\.\-\>\[\]\'\"]*)\}/',$page,$ms))
  52. {
  53. foreach($ms[1] as $cnt=>$tag)
  54. {
  55. $tag = $this->_replace_var_name($tag);
  56. $code = '<'.'?php echo '.$tag.'; ?'.'>';
  57. $page = str_replace($ms[0][$cnt],$code,$page);
  58. }
  59. }
  60. //匹配foreach
  61. if (preg_match_all('/\{(foreach\s*\([^}]+\))\}/',$page,$ms))
  62. {
  63. foreach($ms[1] as $cnt=>$tag)
  64. {
  65. $tag = $this->_replace_var_name($tag);
  66. $code = '<'.'?php '.$tag.': ?>';
  67. $page = str_replace($ms[0][$cnt],$code,$page);
  68. }
  69. }
  70. //匹配if,elseif
  71. if (preg_match_all('/\{((else)?if\s*\([^}]+\))\}/',$page,$ms))
  72. {
  73. foreach($ms[1] as $cnt=>$tag)
  74. {
  75. $tag = $this->_replace_var_name($tag);
  76. $code = '<'.'?php '.$tag.':?>';
  77. $page = str_replace($ms[0][$cnt],$code,$page);
  78. }
  79. }
  80. //匹配函数调用
  81. if (preg_match_all('/\{([a-zA-Z0-9\_]+\([^\}]*\))\}/',$page,$ms))
  82. {
  83. foreach($ms[1] as $cnt=>$tag)
  84. {
  85. $tag = $this->_replace_var_name($tag);
  86. if (!preg_match('/\;\s*$/',$tag)) $tag.=';';
  87. $code = '<'.'?php echo '.$tag.'?>';
  88. $page = str_replace($ms[0][$cnt],$code,$page);
  89. }
  90. }
  91. $page = preg_replace('/\{\/foreach\}/i','<?php endforeach; ?>',$page);
  92. $page = preg_replace('/\{else\}/i','<?php else: ?>',$page);
  93. $page = preg_replace('/\{\/if\}/i','<?php endif; ?>',$page);
  94. if ($this->compile_hook && function_exists($this->compile_hook))
  95. {
  96. $_hook = $this->compile_hook;
  97. $page = $_hook($page);
  98. }
  99. file_put_contents($cache_filename,$page);
  100. return $cache_filename;
  101. }
  102. private function _replace_var_name($s)
  103. {
  104. preg_match_all('/\$([a-zA-Z\_][a-zA-Z\_0-9]*)\.([\.a-zA-Z\_0-9]+)/',$s,$ms);
  105. foreach($ms[1] as $i=>$v)
  106. {
  107. $parts = explode('.',$ms[2][$i]);
  108. $code = '$'.$ms[1][$i];
  109. foreach($parts as $_p)
  110. {
  111. $code.= '[\''.$_p.'\']';
  112. }
  113. $s = str_replace($ms[0][$i],$code,$s);
  114. }
  115. return $s;
  116. }
  117. private function _get_template($template)
  118. {
  119. $filename = $this->template_dir.$template;
  120. $s = file_get_contents($filename);
  121. preg_match_all('/<\!\-\-\s+INCLUDE\s+(.*?)\s*\-\-\>/i', $s, $ms);
  122. foreach($ms[1] as $_key=>$_temp)
  123. {
  124. $s = str_replace($ms[0][$_key],$this->_get_template(trim($_temp)),$s);
  125. }
  126. return $s;
  127. }
  128. }