This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parser.php
355 lines (313 loc) · 12 KB
/
Parser.php
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* SimpleTemplate - Parser
*
* @author Michal Vaněk
*/
namespace SimpleTemplate;
class Parser {
/** @var array */
private $templateParams;
/** @var array */
private $templateParamsTmp;
/** @var string */
private $templateContent;
/** @var string */
private $templateContentTmp;
/** @var string */
private $hashTemplate;
/**
* Sets array of variables to parse.
* @param array
*/
public function setParams($params){
$this->templateParams = $params;
}
/**
* Sets template content.
* @param string
*/
public function setContent($content){
$this->templateContent = $content;
}
/**
* Returns template content.
* @return string
*/
public function getOutput(){
return $this->templateContent;
}
/**
* Parse template content.
*/
public function parse(){
$this->hashTemplate = md5(json_encode($this->templateParams).$this->templateContent);
$cacheContent = Cache::loadTemplate($this->hashTemplate);
if(!$cacheContent){
$this->templateContent = $this->markConditions($this->templateContent);
$this->templateContent = $this->markLoops($this->templateContent);
$this->templateContent = $this->parseLoops($this->templateContent,$this->templateParams);
$this->templateContent = $this->parseConditions($this->templateContent,$this->templateParams);
$this->templateContent = $this->parseVariables($this->templateContent,$this->templateParams);
$this->templateContent = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/","\n",$this->templateContent);
Cache::saveTemplate($this->hashTemplate,$this->templateContent);
}
else $this->templateContent = $cacheContent;
}
/**
* Mark [#loops] tags with numbers
* @param string content to parse
* @return string parsed content
*/
private function markLoops($content){
$loopcount = 1;
while(preg_match('|\[#([a-z]+[a-z0-9_\-\[\]]*)\]|i',$content,$matches)){
$replaced = 0;
/** Mark [#loops] tags */
$content = preg_replace_callback('|\[#'.preg_quote($matches[1]).'\](((?!\[#'.preg_quote($matches[1]).'\]).)*)\[\/#'.preg_quote($matches[1]).'\]|isU',function($tag) use ($matches){
global $loopcount;
$loopcount ++;
//print_r($tag);
return "[#".$loopcount."-".$matches[1]."]".$tag[1]."[/#".$loopcount."-".$matches[1]."]";
},$content,-1,$replaced);
if($replaced == 0) throw new Exception("Mismatched if tag.");
}
return $content;
}
/**
* Parse [#loops].
* @param string content to parse
* @param array variables to parse
* @return string parsed content
*/
private function parseLoops($content,$params){
while(preg_match('|\[#(\d+)-([a-z]+[a-z0-9_\-\[\]]*)\]|i',$content,$matches)){
$parsedContent = null;
$loopID = $matches[1];
$loopName = $matches[2];
$this->templateParamsTmp = $params;
$loopObject = $this->getVariableTagContent($matches,true);
/** Check opening and closing tag */
preg_match('|\[#'.$loopID.'-'.preg_quote($loopName).'\](.*)\[/#'.$loopID.'-'.preg_quote($loopName).'\]|is',$content,$matches);
if(count($matches) == 0) throw new Exception("Mismatched loop tag '$loopName'.");
/** Outer and inner loop content */
$outsideLoop = $matches[0];
$insideLoop = $matches[1];
/** Parse loop content */
if(isset($loopObject) && is_array($loopObject) && !empty($loopObject)){
foreach($loopObject AS $loopContent){
$inlineLoopTmp = $this->parseLoops($insideLoop,$loopContent);
$inlineLoopTmp = $this->parseConditions($inlineLoopTmp,(is_array($loopContent) ? array_merge($loopContent,$this->templateParams) : $loopContent));
$inlineLoopTmp = $this->parseVariables($inlineLoopTmp,(is_array($loopContent) ? array_merge($loopContent,$this->templateParams) : $loopContent));
$parsedContent .= $inlineLoopTmp;
}
}
$content = str_replace($outsideLoop,$parsedContent,$content);
}
return $content;
}
/**
* Parse {#variables}.
* @param string content to parse
* @param array variables to parse
* @return string parsed content
*/
private function parseVariables($content,$params){
$this->templateParamsTmp = $params;
return preg_replace_callback('|\{#[a-z0-9_\-\[\]\|:,\.\'\s]+\}|i',array($this,'getVariableTagContent'),$content);
}
/**
* Parse a found variable, detect indexes and filters.
* @param array found variable
* @param bool is variable from [array] tag
* @return string parsed variable
*/
private function getVariableTagContent($contentTag,$isArray = false){
$contentObject = null;
$contentName = preg_replace('|^\\'.($isArray ? "[#\d+-" : "{#").'([a-z0-9_\-]+)(.*)$|i','\\1',$contentTag[0]);
/** Parse array indexes */
preg_match_all('|\[([a-z0-9_\-]+)\]|i',$contentTag[0],$dimensions);
$dimensions = $dimensions[1];
/** Find object */
if(!is_object($this->templateParamsTmp) && !isset($this->templateParamsTmp[$contentName])) $contentObject = null;
else {
if(!empty($dimensions)){
$contentObject = $this->templateParamsTmp[$contentName];
foreach($dimensions AS $idx => $dimension){
$contentObject = $contentObject[$dimension];
}
}
else $contentObject = $this->templateParamsTmp[$contentName];
}
if(!$isArray){
/** Parse filters */
preg_match_all('|\|([a-z0-9_\-:,\.\'\s]+)|i',$contentTag[0],$filters);
$filters = $filters[1];
/** Apply filters */
if(!empty($filters)){
foreach($filters AS $filter){
$contentObject = Filters::applyFilter($contentObject,$filter);
}
}
}
return $contentObject;
}
/**
* Mark opening and closing {if} and {elseif} tags with numbers
* @param string content to parse
* @return string parsed content
*/
private function markConditions($content){
global $ifcount;
$ifcount = 1;
$replaced = 0;
/** Mark {if} conditions */
while(preg_match('|\{if #|i',$content,$matches)){
$content = preg_replace_callback('~\{if #([#a-z0-9_\-\[\]\s=><!]+)\}(((?!\{if #|\/if\}).)*)\{/if\}~isU',function($tag){
global $ifcount;
$ifcount ++;
return "{if-".$ifcount." #".$tag[1]."}".$tag[2]."{/if-".$ifcount."}";
},$content,-1,$replaced);
if($replaced == 0) throw new Exception("Mismatched if tag.");
}
/** Mark {elseif} branchs to {if} conditions */
while(preg_match('|\{elseif\}|i',$content,$matches)){
$content = preg_replace_callback('~\{if-(\d+) #([#a-z0-9_\-\[\]\s=><!]+)\}(((?!elseif\}).)*)\{/if-\1\}(\s*)(\{elseif\})(((?!elseif\}).)*)(\{/elseif\})~isU',function($tag){
$ifcount = $tag[1];
return "{if-".$ifcount." #".$tag[2]."}".$tag[3]."{/if-".$ifcount."}".$tag[5]."{elseif-".$ifcount."}".$tag[7]."{/elseif-".$ifcount."}";
},$content,-1,$replaced);
if($replaced == 0) throw new Exception("Mismatched elseif tag.");
}
return $content;
}
/**
* Parse {if} and {elseif} conditions.
* @param string content to parse
* @param array variables to parse
* @return string parsed content
*/
private function parseConditions($content,$params){
$this->templateParamsTmp = $params;
/** Regular {if} conditions */
while(preg_match('|\{if-(\d+) #([a-z0-9_\-\[\]]+)\}|i',$content,$matches) || preg_match('|\{if-(\d+) #([a-z0-9_\-\[\]]+)(\s?)([=><!]+)(\s?)([#a-z0-9_\-\[\]]+)\}|i',$content,$matches)){
$conditionID = $matches[1];
$conditionName = $matches[2].$matches[3].$matches[4].$matches[5].$matches[6];
$conditionOperator = $matches[4];
$conditionOperand = (preg_match('|#([a-z0-9_\-\[\]]+)|i',$matches[6],$matchesTmp) ? $this->getVariableTagContent(array("{".$matches[6]."}")) : $matches[6]);
if($conditionOperand == "true") $conditionOperand = true;
else if($conditionOperand == "false") $conditionOperand = false;
else if($conditionOperand == "null") $conditionOperand = null;
$contentObject = $this->getVariableTagContent(array("{#".$matches[2]."}"));
/** Check opening and closing {if} tags */
preg_match('|\{if-'.$conditionID.' #'.preg_quote($conditionName).'\}(.*)\{/if-'.$conditionID.'\}|is',$content,$matches);
if(count($matches) == 0) throw new Exception("Mismatched if tag '$conditionName'.");
/** Outer and inner if content */
$outsideCondition = $matches[0];
$insideCondition = $matches[1];
$matches = array();
/** Check opening and closing {elseif} tags */
if(preg_match('|\{elseif-'.$conditionID.'\}|i',$content,$matches)){
preg_match('|\{elseif-'.$conditionID.'\}(.*)\{/elseif-'.$conditionID.'\}|is',$content,$matches);
if(count($matches) == 0) throw new Exception("Mismatched elseif tag '$conditionName'.");
}
/** Outer and inner if content */
$outsideElse = $matches[0];
$insideElse = $matches[1];
/** Delete body if condition is false */
if(empty($conditionOperator)){
if(!isset($contentObject) || empty($contentObject)){
$insideCondition = null;
} else {
$insideElse = null;
}
} else {
switch($conditionOperator){
case '=':
case '==':
if(!($contentObject == $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
case '!=':
if(!($contentObject != $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
case '<':
if(!($contentObject < $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
case '>':
if(!($contentObject > $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
case '<=':
if(!($contentObject <= $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
case '>=':
if(!($contentObject >= $conditionOperand)) $insideCondition = null;
else $insideElse = null;
break;
}
}
$content = str_replace($outsideCondition,$insideCondition,$content);
$content = str_replace($outsideElse,$insideElse,$content);
}
/** Ternary operators */
$conditionType = array(0 => 0,1 => 0);
while($conditionType[0] = preg_match('|\{#([a-z0-9_\-\[\]]+)(\s?)\?(\s?)(.+)(\s?):(\s?)(.+)#\}|iU',$content,$matches)
|| $conditionType[1] = preg_match('|\{#([a-z0-9_\-\[\]]+)(\s?)([=><!]+)(\s?)([#a-z0-9_\-\[\]]+)(\s?)\?(\s?)(.+)(\s?):(\s?)(.+)#\}|iU',$content,$matches)){
$contentObject = $this->getVariableTagContent(array("{#".$matches[1]."}"));
$conditionOperator = null;
if($conditionType[1] == 1){
$conditionOperator = $matches[3];
$conditionOperand = (preg_match('|#([a-z0-9_\-\[\]]+)|i',$matches[5],$matchesTmp) ? $this->getVariableTagContent(array("{".$matches[5]."}")) : $matches[5]);
if($conditionOperand == "true") $conditionOperand = true;
else if($conditionOperand == "false") $conditionOperand = false;
else if($conditionOperand == "null") $conditionOperand = null;
$contentResult[0] = trim($matches[8]);
$contentResult[1] = trim($matches[11]);
} else {
$contentResult[0] = trim($matches[4]);
$contentResult[1] = trim($matches[7]);
}
$conditionType = array(0 => 0,1 => 0);
/** Outer condition content */
$outsideCondition = $matches[0];
$parsedContent = null;
if(empty($conditionOperator)){
if(!isset($contentObject) || empty($contentObject)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
} else {
switch($conditionOperator){
case '=':
case '==':
if(!($contentObject == $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
case '!=':
if(!($contentObject != $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
case '<':
if(!($contentObject < $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
case '>':
if(!($contentObject > $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
case '<=':
if(!($contentObject <= $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
case '>=':
if(!($contentObject >= $conditionOperand)) $parsedContent = $contentResult[1];
else $parsedContent = $contentResult[0];
break;
}
}
$content = str_replace($outsideCondition,$parsedContent,$content);
}
return $content;
}
}