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

Language PHP Lines 154
MD5 Hash 74f1872cf0ed0ad9b9d1814f5b98b5e1
Repository https://github.com/monkeycraps/swoole_framework.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
class ViewToDB extends SwooleObject
{
	public $templates_dir;
	
	public $in_content='';
	public $out_content='';
	public $c_content='';
		
	public $pattern;
	
	public $db;
	
	function __construct($db)
	{
		$this->db = $db;
	}
	
	function parse($tpl_name)
	{
		
		$filename = WEBPATH.$this->templates_dir.$tpl_name;
		$this->c_content = $this->in_content = file_get_contents($filename);
		
		$tags=array();
		$this->pattern = "#".preg_quote('{{','~')."[^}]+".preg_quote('}}','~')."#i";
        preg_match_all($this->pattern,$this->in_content,$tags,PREG_PATTERN_ORDER);		
        $tags=$tags[0];
        
		foreach($tags as $tag)
		{
			$tagname=self::trim($tag);
			$exp=explode(":",$tagname);
			if(count($exp)>2) die("错误的标签:".$tag);
			if(count($exp)==1) continue;
			$this->parse_static($tag);
			call_user_func_array(array($this,'proc_'.$exp[0]),array(trim($exp[1]),$tag));
		}
		$this->end();
		echo $this->out_content;
	}

	function assign($key,$value)
	{
		$this->$key=$value;
	}
	
	function proc_var($tag,$tag_head)
	{
		$this->c_content = str_replace($tag_head,$this->$tag,$this->c_content);

//		$params=explode(".",$varname);
//		$varnum=count($params);
//		if($varnum==1)
//		{
//			return $$varname;
//		}
//		elseif($varnum==2)
//		{
//			$arrayname=$params[0];
//			$array=$$arrayname;
//			return $array[$params[1]];
//		}
	}
	
	public static function parse_param($tagbody)
	{
		$rs = array();
		$list= explode(" ",$tagbody);
		foreach($list as $val)
		{
			$arr=explode("=",$val,2);
			$rs[$arr[0]]=$arr[1];
		}
		return $rs;
	}
	
	function proc_records($tag,$tag_head)
	{
		$param = self::parse_param($tag);
		$select = new SelectDB($this->db);
		$select->put($param);
		$body = $this->get_body('records');
		$list = $select->getall();
		
		$content = '';
		
		foreach($list as $record)
		{
			$content.=$this->parse_record_body($body,$record);
		}
		$this->out_content.=$content;
		$this->clear('records');
	}
	
	function proc_record($tag,$tag_head)
	{
		$param = self::parse_param($tag);
		$record_name = $param['_name'];
		$record_field = $param['_field'];
		if($this->$record_name!=false) $record=$this->$record_name;
		else
		{
			$select = new SelectDB($this->db);
			$select->put($param);
			$record = $select->getone();
			$this->$record_name = $record;
		}
		$this->out_content.=$record[$record_field];
		$this->c_content = str_replace($tag_head,'',$this->c_content);
	}
	
	function parse_record_body($line,$record)
	{
		$vals = array();
		preg_match_all($this->pattern,$line,$vals);
		$vals=$vals[0];
		foreach($vals as $val) $line=str_replace($val,$record[self::trim($val)],$line);
		return $line;
	}
	
	function clear($tagname)
	{
		$end_tag = '{{/'.$tagname.'}}';
		$pos_e = mb_strpos($this->c_content,$end_tag);		
		$this->c_content = substr($this->c_content,$pos_e+strlen($end_tag),-1);
	}
	
	function parse_static($tag)
	{
		$pos = strpos($this->c_content,$tag);
		$this->out_content.= substr($this->c_content,0,$pos);
		$this->c_content = substr($this->c_content,$pos,strlen($this->c_content)-$pos);
	}
	
	function end()
	{
		$this->out_content.=$this->c_content;
	}
	
	function get_body($tagname)
	{
		$end_tag = '{{/'.$tagname.'}}';
		$pos_s = mb_strpos($this->c_content,'}}')+2;
		$pos_e = mb_strpos($this->c_content,$end_tag);
		return mb_substr($this->c_content,$pos_s,$pos_e-$pos_s);
	}
	
	public static function trim($string)
	{
		return str_replace('}}',"",str_replace('{{',"",$string));
	}
}
?>
Back to Top