PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/class/swoole/html/ViewToDB.class.php

https://github.com/monkeycraps/swoole_framework
PHP | 154 lines | 120 code | 22 blank | 12 comment | 3 complexity | 74f1872cf0ed0ad9b9d1814f5b98b5e1 MD5 | raw file
  1. <?php
  2. class ViewToDB extends SwooleObject
  3. {
  4. public $templates_dir;
  5. public $in_content='';
  6. public $out_content='';
  7. public $c_content='';
  8. public $pattern;
  9. public $db;
  10. function __construct($db)
  11. {
  12. $this->db = $db;
  13. }
  14. function parse($tpl_name)
  15. {
  16. $filename = WEBPATH.$this->templates_dir.$tpl_name;
  17. $this->c_content = $this->in_content = file_get_contents($filename);
  18. $tags=array();
  19. $this->pattern = "#".preg_quote('{{','~')."[^}]+".preg_quote('}}','~')."#i";
  20. preg_match_all($this->pattern,$this->in_content,$tags,PREG_PATTERN_ORDER);
  21. $tags=$tags[0];
  22. foreach($tags as $tag)
  23. {
  24. $tagname=self::trim($tag);
  25. $exp=explode(":",$tagname);
  26. if(count($exp)>2) die("错误的标签:".$tag);
  27. if(count($exp)==1) continue;
  28. $this->parse_static($tag);
  29. call_user_func_array(array($this,'proc_'.$exp[0]),array(trim($exp[1]),$tag));
  30. }
  31. $this->end();
  32. echo $this->out_content;
  33. }
  34. function assign($key,$value)
  35. {
  36. $this->$key=$value;
  37. }
  38. function proc_var($tag,$tag_head)
  39. {
  40. $this->c_content = str_replace($tag_head,$this->$tag,$this->c_content);
  41. // $params=explode(".",$varname);
  42. // $varnum=count($params);
  43. // if($varnum==1)
  44. // {
  45. // return $$varname;
  46. // }
  47. // elseif($varnum==2)
  48. // {
  49. // $arrayname=$params[0];
  50. // $array=$$arrayname;
  51. // return $array[$params[1]];
  52. // }
  53. }
  54. public static function parse_param($tagbody)
  55. {
  56. $rs = array();
  57. $list= explode(" ",$tagbody);
  58. foreach($list as $val)
  59. {
  60. $arr=explode("=",$val,2);
  61. $rs[$arr[0]]=$arr[1];
  62. }
  63. return $rs;
  64. }
  65. function proc_records($tag,$tag_head)
  66. {
  67. $param = self::parse_param($tag);
  68. $select = new SelectDB($this->db);
  69. $select->put($param);
  70. $body = $this->get_body('records');
  71. $list = $select->getall();
  72. $content = '';
  73. foreach($list as $record)
  74. {
  75. $content.=$this->parse_record_body($body,$record);
  76. }
  77. $this->out_content.=$content;
  78. $this->clear('records');
  79. }
  80. function proc_record($tag,$tag_head)
  81. {
  82. $param = self::parse_param($tag);
  83. $record_name = $param['_name'];
  84. $record_field = $param['_field'];
  85. if($this->$record_name!=false) $record=$this->$record_name;
  86. else
  87. {
  88. $select = new SelectDB($this->db);
  89. $select->put($param);
  90. $record = $select->getone();
  91. $this->$record_name = $record;
  92. }
  93. $this->out_content.=$record[$record_field];
  94. $this->c_content = str_replace($tag_head,'',$this->c_content);
  95. }
  96. function parse_record_body($line,$record)
  97. {
  98. $vals = array();
  99. preg_match_all($this->pattern,$line,$vals);
  100. $vals=$vals[0];
  101. foreach($vals as $val) $line=str_replace($val,$record[self::trim($val)],$line);
  102. return $line;
  103. }
  104. function clear($tagname)
  105. {
  106. $end_tag = '{{/'.$tagname.'}}';
  107. $pos_e = mb_strpos($this->c_content,$end_tag);
  108. $this->c_content = substr($this->c_content,$pos_e+strlen($end_tag),-1);
  109. }
  110. function parse_static($tag)
  111. {
  112. $pos = strpos($this->c_content,$tag);
  113. $this->out_content.= substr($this->c_content,0,$pos);
  114. $this->c_content = substr($this->c_content,$pos,strlen($this->c_content)-$pos);
  115. }
  116. function end()
  117. {
  118. $this->out_content.=$this->c_content;
  119. }
  120. function get_body($tagname)
  121. {
  122. $end_tag = '{{/'.$tagname.'}}';
  123. $pos_s = mb_strpos($this->c_content,'}}')+2;
  124. $pos_e = mb_strpos($this->c_content,$end_tag);
  125. return mb_substr($this->c_content,$pos_s,$pos_e-$pos_s);
  126. }
  127. public static function trim($string)
  128. {
  129. return str_replace('}}',"",str_replace('{{',"",$string));
  130. }
  131. }
  132. ?>