PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/system/SwooleTemplate.php

https://github.com/monkeycraps/swoole_framework
PHP | 219 lines | 167 code | 32 blank | 20 comment | 4 complexity | 25dd0d567dc674d5811ec1b21f9aa958 MD5 | raw file
  1. <?php
  2. /**
  3. * Swoole模板工具类
  4. * 提供一个简单的模板系统,支持标签、模板数据库映射查询,遍历,显示等功能
  5. * @author Tianfeng.Han
  6. * @package SwooleSystem
  7. * @subpackage template
  8. *
  9. */
  10. class SwooleTemplate extends ArrayObject
  11. {
  12. public $templates_dir;
  13. public $in_content='';
  14. public $out_content='';
  15. public $c_content='';
  16. public $pattern;
  17. public $db;
  18. public $fields;
  19. public $fields_full;
  20. const FILTER_DIR = '/taglib/filter/';
  21. function __construct($db)
  22. {
  23. $this->db = $db;
  24. }
  25. function parse($tpl_name)
  26. {
  27. $filename = WEBPATH.$this->templates_dir.$tpl_name;
  28. $this->c_content = $this->in_content = file_get_contents($filename);
  29. $tags=array();
  30. $this->pattern = "#".preg_quote('{{','~')."[^}]+".preg_quote('}}','~')."#i";
  31. preg_match_all($this->pattern,$this->in_content,$tags,PREG_PATTERN_ORDER);
  32. $tags=$tags[0];
  33. foreach($tags as $tag)
  34. {
  35. $tagname=self::trim($tag);
  36. $exp=explode(":",$tagname);
  37. if(count($exp)>2) die("错误的标签:".$tag);
  38. if(count($exp)==1) continue;
  39. $this->parse_static($tag);
  40. call_user_func_array(array($this,'proc_'.$exp[0]),array(trim($exp[1]),$tag));
  41. }
  42. $this->end();
  43. echo $this->out_content;
  44. }
  45. function assign($key,$value)
  46. {
  47. $this->$key=$value;
  48. }
  49. function proc_var($tag,$tag_head)
  50. {
  51. $this->c_content = str_replace($tag_head,$this->$tag,$this->c_content);
  52. // $params=explode(".",$varname);
  53. // $varnum=count($params);
  54. // if($varnum==1)
  55. // {
  56. // return $$varname;
  57. // }
  58. // elseif($varnum==2)
  59. // {
  60. // $arrayname=$params[0];
  61. // $array=$$arrayname;
  62. // return $array[$params[1]];
  63. // }
  64. }
  65. public static function parse_param($tagbody)
  66. {
  67. $rs = array();
  68. $list= explode(" ",$tagbody);
  69. foreach($list as $val)
  70. {
  71. $arr=explode("=",$val,2);
  72. $rs[$arr[0]]=$arr[1];
  73. }
  74. return $rs;
  75. }
  76. function proc_records($tag,$tag_head)
  77. {
  78. $param = self::parse_param($tag);
  79. $select = new SelectDB($this->db);
  80. $select->put($param);
  81. $body = $this->get_body('records');
  82. $list = $select->getall();
  83. $content = '';
  84. foreach($list as $record)
  85. {
  86. $content.=$this->parse_record_body($body,$record);
  87. }
  88. $this->out_content.=$content;
  89. $this->clear('records');
  90. }
  91. function proc_record($tag,$tag_head)
  92. {
  93. $param = self::parse_param($tag);
  94. $record_name = $param['_name'];
  95. $record_field = $param['_field'];
  96. if($this->$record_name!=false) $record=$this->$record_name;
  97. else
  98. {
  99. $select = new SelectDB($this->db);
  100. $select->put($param);
  101. $record = $select->getone();
  102. $this->$record_name = $record;
  103. }
  104. $this->out_content.=$record[$record_field];
  105. $this->c_content = str_replace($tag_head,'',$this->c_content);
  106. }
  107. function parse_record_body($line,$record)
  108. {
  109. $vals = array();
  110. preg_match_all($this->pattern,$line,$vals);
  111. $vals=$vals[0];
  112. foreach($vals as $val) $line=str_replace($val,$record[self::trim($val)],$line);
  113. return $line;
  114. }
  115. function clear($tagname)
  116. {
  117. $end_tag = '{{/'.$tagname.'}}';
  118. $pos_e = mb_strpos($this->c_content,$end_tag);
  119. $this->c_content = substr($this->c_content,$pos_e+strlen($end_tag),-1);
  120. }
  121. function parse_static($tag)
  122. {
  123. $pos = strpos($this->c_content,$tag);
  124. $this->out_content.= substr($this->c_content,0,$pos);
  125. $this->c_content = substr($this->c_content,$pos,strlen($this->c_content)-$pos);
  126. }
  127. function end()
  128. {
  129. $this->out_content.=$this->c_content;
  130. }
  131. function get_body($tagname)
  132. {
  133. $end_tag = '{{/'.$tagname.'}}';
  134. $pos_s = mb_strpos($this->c_content,'}}')+2;
  135. $pos_e = mb_strpos($this->c_content,$end_tag);
  136. return mb_substr($this->c_content,$pos_s,$pos_e-$pos_s);
  137. }
  138. function trim($string)
  139. {
  140. return str_replace('}}',"",str_replace('{{',"",$string));
  141. }
  142. function get_fields($body)
  143. {
  144. preg_match_all('#{([^\}^|]+)[}|]#i',$body,$vals);
  145. $this->fields = array_unique($vals[1]);
  146. preg_match_all('#{([^\}]+)}#i',$body,$vals);
  147. $this->fields_full = $vals[1];
  148. return $this->fields;
  149. }
  150. function parse_loop($data,$body)
  151. {
  152. $content = '';
  153. if(empty($this->fields_full))
  154. $this->get_fields($body);
  155. foreach($data as $record)
  156. {
  157. $line = $body;
  158. foreach($this->fields_full as $field)
  159. {
  160. $this->filter($line,$field,$record);
  161. }
  162. $content.=$line;
  163. }
  164. return $content;
  165. }
  166. function filter(&$line,$field,&$record)
  167. {
  168. $f = explode('|',$field);
  169. $field_name = $f[0];
  170. unset($f[0]);
  171. $value = $record[$field_name];
  172. foreach($f as $filter)
  173. {
  174. $f_params = explode(':',$filter);
  175. self::_load_filter($f_params[0]);
  176. $func = 'swoole_filter_'.$f_params[0];
  177. $f_params[0] = $field_name;
  178. $value = $func($value,$f_params,$record);
  179. }
  180. $line = str_replace('{'.$field.'}',$value,$line);
  181. }
  182. static function _load_filter($filter_name)
  183. {
  184. require_once(LIBPATH.self::FILTER_DIR.$filter_name.'.filter.php');
  185. }
  186. }
  187. ?>