-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathTemplate.php
More file actions
executable file
·396 lines (367 loc) · 11.6 KB
/
Template.php
File metadata and controls
executable file
·396 lines (367 loc) · 11.6 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* Handlebars base template
* contain some utility method to get context and helpers
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Mardix <https://github.com/mardix>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @copyright 2013 (c) Mardix
* @license MIT
* @link http://voodoophp.org/docs/handlebars
*/
namespace Handlebars;
use InvalidArgumentException;
use RuntimeException;
class Template
{
/**
* @var Handlebars
*/
protected $handlebars;
protected $tree = [];
protected $source = '';
/**
* @var array Run stack
*/
private $stack = [];
/**
* Handlebars template constructor
*
* @param Handlebars $engine handlebar engine
* @param array $tree Parsed tree
* @param string $source Handlebars source
*/
public function __construct(Handlebars $engine, $tree, $source)
{
$this->handlebars = $engine;
$this->tree = $tree;
$this->source = $source;
array_push($this->stack, [0, $this->getTree(), false]);
}
/**
* Get current tree
*
* @return array
*/
public function getTree()
{
return $this->tree;
}
/**
* Get current source
*
* @return string
*/
public function getSource()
{
return $this->source;
}
/**
* Get current engine associated with this object
*
* @return Handlebars
*/
public function getEngine()
{
return $this->handlebars;
}
/**
* set stop token for render and discard method
*
* @param string|false $token token to set as stop token or false to remove
*
* @return void
*/
public function setStopToken($token)
{
$topStack = array_pop($this->stack);
$topStack[2] = $token;
array_push($this->stack, $topStack);
}
/**
* get current stop token
*
* @return string|false
*/
public function getStopToken()
{
return end($this->stack)[2];
}
/**
* Render top tree
*
* @param mixed $context current context
*
* @throws \RuntimeException
* @return string
*/
public function render($context)
{
if (!$context instanceof Context) {
$context = new Context($context, [
'enableDataVariables' => $this->handlebars->isDataVariablesEnabled(),
]);
}
$topTree = end($this->stack); // never pop a value from stack
list($index, $tree, $stop) = $topTree;
$buffer = '';
while (array_key_exists($index, $tree)) {
$current = $tree[$index];
$index++;
//if the section is exactly like waitFor
if (is_string($stop)
&& $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED
&& $current[Tokenizer::NAME] === $stop
) {
break;
}
switch ($current[Tokenizer::TYPE]) {
case Tokenizer::T_SECTION :
$newStack = isset($current[Tokenizer::NODES])
? $current[Tokenizer::NODES] : [];
array_push($this->stack, [0, $newStack, false]);
$buffer .= $this->section($context, $current);
array_pop($this->stack);
break;
case Tokenizer::T_INVERTED :
$newStack = isset($current[Tokenizer::NODES]) ?
$current[Tokenizer::NODES] : [];
array_push($this->stack, [0, $newStack, false]);
$buffer .= $this->inverted($context, $current);
array_pop($this->stack);
break;
case Tokenizer::T_COMMENT :
$buffer .= '';
break;
case Tokenizer::T_PARTIAL:
case Tokenizer::T_PARTIAL_2:
$buffer .= $this->partial($context, $current);
break;
case Tokenizer::T_UNESCAPED:
case Tokenizer::T_UNESCAPED_2:
$buffer .= $this->variables($context, $current, false);
break;
case Tokenizer::T_ESCAPED:
$buffer .= $this->variables($context, $current, true);
break;
case Tokenizer::T_TEXT:
$buffer .= $current[Tokenizer::VALUE];
break;
default:
throw new RuntimeException(
'Invalid node type : ' . json_encode($current)
);
}
}
if ($stop) {
//Ok break here, the helper should be aware of this.
$newStack = array_pop($this->stack);
$newStack[0] = $index;
$newStack[2] = false; //No stop token from now on
array_push($this->stack, $newStack);
}
return $buffer;
}
/**
* Discard top tree
*
* @return string
*/
public function discard()
{
$topTree = end($this->stack); //This method never pop a value from stack
list($index, $tree, $stop) = $topTree;
while (array_key_exists($index, $tree)) {
$current = $tree[$index];
$index++;
//if the section is exactly like waitFor
if (is_string($stop)
&& $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED
&& $current[Tokenizer::NAME] === $stop
) {
break;
}
}
if ($stop) {
//Ok break here, the helper should be aware of this.
$newStack = array_pop($this->stack);
$newStack[0] = $index;
$newStack[2] = false;
array_push($this->stack, $newStack);
}
return '';
}
/**
* Process section nodes
*
* @param Context $context current context
* @param array $current section node data
*
* @throws \RuntimeException
* @return string the result
*/
private function section(Context $context, $current)
{
$helpers = $this->handlebars->getHelpers();
$sectionName = $current[Tokenizer::NAME];
if ($helpers->has($sectionName)) {
if (isset($current[Tokenizer::END])) {
$source = substr(
$this->getSource(),
$current[Tokenizer::INDEX],
$current[Tokenizer::END] - $current[Tokenizer::INDEX]
);
} else {
$source = '';
}
$params = [
$this, //First argument is this template
$context, //Second is current context
$current[Tokenizer::ARGS], //Arguments
$source
];
$return = call_user_func_array($helpers->$sectionName, $params);
if ($return instanceof String) {
return $this->handlebars->loadString($return)->render($context);
} else {
return $return;
}
} elseif (trim($current[Tokenizer::ARGS]) == '') {
// fallback to mustache style each/with/for just if there is
// no argument at all.
try {
$sectionVar = $context->get($sectionName, true);
} catch (InvalidArgumentException $e) {
throw new RuntimeException(
$sectionName . ' is not registered as a helper'
);
}
$buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof \Traversable) {
foreach ($sectionVar as $index => $d) {
$context->pushIndex($index);
$context->push($d);
$buffer .= $this->render($context);
$context->pop();
$context->popIndex();
}
} elseif (is_object($sectionVar)) {
//Act like with
$context->push($sectionVar);
$buffer = $this->render($context);
$context->pop();
} elseif ($sectionVar) {
$buffer = $this->render($context);
}
return $buffer;
} else {
throw new RuntimeException(
$sectionName . ' is not registered as a helper'
);
}
}
/**
* Process inverted section
*
* @param Context $context current context
* @param array $current section node data
*
* @return string the result
*/
private function inverted(Context $context, $current)
{
$sectionName = $current[Tokenizer::NAME];
$data = $context->get($sectionName);
if (!$data) {
return $this->render($context);
} else {
//No need to discard here, since it has no else
return '';
}
}
/**
* Process partial section
*
* @param Context $context current context
* @param array $current section node data
*
* @return string the result
*/
private function partial(Context $context, $current)
{
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
if ($current[Tokenizer::ARGS]) {
$args = explode(' ', $current[Tokenizer::ARGS]);
$newContext = [];
foreach ($args as $arg) {
$kv = explode('=', $arg, 2);
if (count($kv) == 1) {
if (count($args) > 1) {
throw new RuntimeException(
$current[Tokenizer::NAME] . ' call has invalid syntax'
);
}
$value = trim($kv[0]);
$newContext = $context->get($value);
} else {
$key = trim($kv[0]);
$value = trim($kv[1]);
$newContext[$key] = $context->get($value);
}
}
$context = $newContext;
}
return $partial->render($context);
}
/**
* Process partial section
*
* @param Context $context current context
* @param array $current section node data
* @param boolean $escaped escape result or not
*
* @return string the result
*/
private function variables(Context $context, $current, $escaped)
{
$name = $current[Tokenizer::NAME];
$value = $context->get($name);
// If @data variables are enabled, use the more complex algorithm for handling the the variables otherwise
// use the previous version.
if ($this->handlebars->isDataVariablesEnabled()) {
if (substr(trim($name), 0, 1) == '@') {
$variable = $context->getDataVariable($name);
if (is_bool($variable)) {
return $variable ? 'true' : 'false';
}
return $variable;
}
} else {
// If @data variables are not enabled, then revert back to legacy behavior
if ($name == '@index') {
return $context->lastIndex();
}
if ($name == '@key') {
return $context->lastKey();
}
}
if ($escaped) {
$args = $this->handlebars->getEscapeArgs();
array_unshift($args, $value);
$value = call_user_func_array(
$this->handlebars->getEscape(),
array_values($args)
);
}
return $value;
}
public function __clone()
{
return $this;
}
}