-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.php
329 lines (276 loc) · 11.1 KB
/
template.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
<?php
/**
* Template class begins with some adaptable properties and overrides for the exception-generation methods.
* With utility methods to help us break down chunks of the template, separating out the tags from the text
*
* @author Faizan Ayubi
*/
namespace Framework {
use Framework\Base as Base;
use Framework\ArrayMethods as ArrayMethods;
use Framework\StringMethods as StringMethods;
use Framework\Template\Exception as Exception;
class Template extends Base {
/**
* @readwrite
*/
protected $_implementation;
/**
* @readwrite
*/
protected $_header = "if (is_array(\$_data) && sizeof(\$_data)) extract(\$_data); \$_text = array();";
/**
* @readwrite
*/
protected $_footer = "return implode(\$_text);";
/**
* @read
*/
protected $_code;
/**
* @read
*/
protected $_function;
public function _getExceptionForImplementation($method) {
return new Exception\Implementation("{$method} method not implemented");
}
/**
* Used for any of the statements, if they have a specific argument format (such as for, foreach, or macro).
* It returns the bits between the {...} characters in a neat associative array.
*
* @param type $source
* @param type $expression
* @return type
*/
protected function _arguments($source, $expression) {
$args = $this->_array($expression, array(
$expression => array(
"opener" => "{",
"closer" => "}"
)
));
$tags = $args["tags"];
$arguments = array();
$sanitized = StringMethods::sanitize($expression, "()[],.<>*$@");
foreach ($tags as $i => $tag) {
$sanitized = str_replace($tag, "(.*)", $sanitized);
$tags[$i] = str_replace(array("{", "}"), "", $tag);
}
if (preg_match("#{$sanitized}#", $source, $matches)) {
foreach ($tags as $i => $tag) {
$arguments[$tag] = $matches[$i + 1];
}
}
return $arguments;
}
/**
* Generates a $node array, which contains a bit of metadata about the tag.
* @param type $source
* @return boolean
*/
protected function _tag($source) {
$tag = null;
$arguments = array();
$match = $this->_implementation->match($source);
if ($match == null) {
return false;
}
$delimiter = $match["delimiter"];
$type = $match["type"];
$start = strlen($type["opener"]);
$end = strpos($source, $type["closer"]);
$extract = substr($source, $start, $end - $start);
if (isset($type["tags"])) {
$tags = implode("|", array_keys($type["tags"]));
$regex = "#^(/){0,1}({$tags})\s*(.*)$#";
if (!preg_match($regex, $extract, $matches)) {
return false;
}
$tag = $matches[2];
$extract = $matches[3];
$closer = !!$matches[1];
}
if ($tag && $closer) {
return array(
"tag" => $tag,
"delimiter" => $delimiter,
"closer" => true,
"source" => false,
"arguments" => false,
"isolated" => $type["tags"][$tag]["isolated"]
);
}
if (isset($type["arguments"])) {
$arguments = $this->_arguments($extract, $type["arguments"]);
} else if ($tag && isset($type["tags"][$tag]["arguments"])) {
$arguments = $this->_arguments($extract, $type["tags"][$tag]["arguments"]);
}
return array(
"tag" => $tag,
"delimiter" => $delimiter,
"closer" => false,
"source" => $extract,
"arguments" => $arguments,
"isolated" => (!empty($type["tags"]) ? $type["tags"][$tag]["isolated"] : false)
);
}
/**
* Deconstructs a template string into arrays of tags, text, and a combination of the two.
* @param type $source
* @return type
*/
protected function _array($source) {
$parts = array();
$tags = array();
$all = array();
$type = null;
$delimiter = null;
while ($source) {
$match = $this->_implementation->match($source);
$type = $match["type"];
$delimiter = $match["delimiter"];
$opener = strpos($source, $type["opener"]);
$closer = strpos($source, $type["closer"]) + strlen($type["closer"]);
if ($opener !== false) {
$parts[] = substr($source, 0, $opener);
$tags[] = substr($source, $opener, $closer - $opener);
$source = substr($source, $closer);
} else {
$parts[] = $source;
$source = "";
}
}
foreach ($parts as $i => $part) {
$all[] = $part;
if (isset($tags[$i])) {
$all[] = $tags[$i];
}
}
return array(
"text" => ArrayMethods::clean($parts),
"tags" => ArrayMethods::clean($tags),
"all" => ArrayMethods::clean($all)
);
}
/**
* Loops through the array of template segments, generated by the _array() method,
* and organizes them into a hierarchical structure. Plain text nodes are simply assigned as-is to the tree,
* while additional metadata is generated and assigned with the tags.
*
* @param type $array
* @return array
*/
protected function _tree($array) {
$root = array(
"children" => array()
);
$current = & $root;
foreach ($array as $i => $node) {
$result = $this->_tag($node);
if ($result) {
$tag = isset($result["tag"]) ? $result["tag"] : "";
$arguments = isset($result["arguments"]) ? $result["arguments"] : "";
if ($tag) {
if (!$result["closer"]) {
$last = ArrayMethods::last($current["children"]);
if ($result["isolated"] && is_string($last)) {
array_pop($current["children"]);
}
$current["children"][] = array(
"index" => $i,
"parent" => &$current,
"children" => array(),
"raw" => $result["source"],
"tag" => $tag,
"arguments" => $arguments,
"delimiter" => $result["delimiter"],
"number" => sizeof($current["children"])
);
$current = & $current["children"][sizeof($current["children"]) - 1];
} else if (isset($current["tag"]) && $result["tag"] == $current["tag"]) {
$start = $current["index"] + 1;
$length = $i - $start;
$current["source"] = implode(array_slice($array, $start, $length));
$current = & $current["parent"];
}
} else {
$current["children"][] = array(
"index" => $i,
"parent" => &$current,
"children" => array(),
"raw" => $result["source"],
"tag" => $tag,
"arguments" => $arguments,
"delimiter" => $result["delimiter"],
"number" => sizeof($current["children"])
);
}
} else {
$current["children"][] = $node;
}
}
return $root;
}
/**
* It walks the hierarchy (generated by the _tree() method), parses plain text nodes,
* and indirectly invokes the handler for each valid tag.
* The _script() method should generate a syntactically correct function body.
*
* @param type $tree
* @return type
*/
protected function _script($tree) {
$content = array();
if (is_string($tree)) {
$tree = addslashes($tree);
return "\$_text[] = \"{$tree}\";";
}
if (sizeof($tree["children"]) > 0) {
foreach ($tree["children"] as $child) {
$content[] = $this->_script($child);
}
}
if (isset($tree["parent"])) {
return $this->_implementation->handle($tree, implode($content));
}
return implode($content);
}
/**
* It wraps the processed template within the $_header and $_footer variables.
* This is then evaluated with a call to PHP’s create_function method and assigned to the protected $_function property.
*
* @param type $template
* @return \Framework\Template
* @throws Exception\Implementation
*/
public function parse($template) {
if (!is_a($this->_implementation, "Framework\Template\Implementation")) {
throw new Exception\Implementation();
}
$array = $this->_array($template);
$tree = $this->_tree($array["all"]);
$this->_code = $this->header . $this->_script($tree) . $this->footer;
$this->_function = create_function("\$_data", $this->code);
return $this;
}
/**
* Check for the existence of the protected $_function property and throws a Template\Exception\Parser exception
* if it is not present. It then tries to execute the generated function with the $data passed to it.
*
* @param type $data
* @return type
* @throws Exception\Parser
*/
public function process($data = array()) {
if ($this->_function == null) {
throw new Exception\Parser();
}
try {
$function = $this->_function;
return $function($data);
} catch (\Exception $e) {
throw new Exception\Parser($e);
}
}
}
}