-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplate.php
423 lines (377 loc) · 10.9 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
declare(strict_types=1);
/*
* This file is part of Mindy Framework.
* (c) 2017 Maxim Falaleev
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mindy\Template;
abstract class Template implements TemplateInterface
{
protected $loader;
protected $helpers = [];
protected $variableProviders = [];
protected $parent;
protected $blocks = [];
protected $macros = [];
protected $imports = [];
protected $stack;
/**
* Template constructor.
*
* @param TemplateEngine $loader
* @param array $helpers
* @param array $variableProviders
*/
public function __construct(TemplateEngine $loader, array $helpers = [], array $variableProviders = [])
{
$this->loader = $loader;
$this->helpers = $helpers;
$this->variableProviders = $variableProviders;
$this->parent = null;
$this->blocks = [];
$this->macros = [];
$this->imports = [];
$this->stack = [];
}
/**
* @param string $template
*
* @return TemplateInterface
*/
public function loadExtends(string $template)
{
try {
return $this->loader->load($template);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'error extending %s (%s) from %s line %d',
var_export($template, true), $e->getMessage(), static::NAME,
$this->getLineTrace($e)
));
}
}
/**
* @param string $template
*
* @return TemplateInterface
*/
public function loadInclude(string $template)
{
try {
return $this->loader->load($template);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'error including %s (%s) from %s line %d',
var_export($template, true), $e->getMessage(), static::NAME,
$this->getLineTrace($e)
));
}
}
/**
* @param string $template
*
* @return string
*/
public function loadImport(string $template)
{
try {
return $this->loader->load($template)->getMacros();
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'error importing %s (%s) from %s line %d',
var_export($template, true), $e->getMessage(), static::NAME,
$this->getLineTrace($e)
));
}
}
/**
* @param $name
* @param $context
* @param $blocks
* @param $macros
* @param $imports
*
* @return string|null
*/
public function displayBlock($name, $context, $blocks, $macros, $imports)
{
$blocks = $blocks + $this->blocks;
$macros = $macros + $this->macros;
$imports = $imports + $this->imports;
if (isset($blocks[$name]) && is_callable($blocks[$name])) {
return call_user_func(
$blocks[$name], $context, $blocks, $macros, $imports
);
} else {
return null;
}
}
/**
* @param $name
* @param $context
* @param $blocks
* @param $macros
* @param $imports
*
* @return mixed
*/
public function displayParent($name, $context, $blocks, $macros, $imports)
{
$parent = $this;
while ($parent = $parent->parent) {
if (isset($parent->blocks[$name]) &&
is_callable($parent->blocks[$name])) {
return call_user_func($parent->blocks[$name], $context, $blocks,
$macros, $imports);
}
}
}
/**
* @param $module
* @param $name
* @param $params
* @param $context
* @param $macros
* @param $imports
* @param $block
*
* @return mixed
*/
public function expandMacro($module, $name, $params, $context, $macros, $imports, $block)
{
$macros = $macros + $this->macros;
$imports = $imports + $this->imports;
if (isset($module) && isset($imports[$module])) {
$macros = $macros + $imports[$module];
}
if (isset($macros[$name]) && is_callable($macros[$name])) {
return call_user_func($macros[$name], $params, $context, $macros, $imports, $block);
}
return null;
}
/**
* @param $context
* @param $name
*
* @return $this
*/
public function pushContext(&$context, $name)
{
if (!array_key_exists($name, $this->stack)) {
$this->stack[$name] = [];
}
array_push($this->stack[$name], isset($context[$name]) ?
$context[$name] : null
);
return $this;
}
/**
* @param $context
* @param $name
*
* @return $this
*/
public function popContext(&$context, $name)
{
if (!empty($this->stack[$name])) {
$context[$name] = array_pop($this->stack[$name]);
}
return $this;
}
/**
* @param \Exception|null $e
* @return null
*/
public function getLineTrace(\Exception $e = null)
{
if (!isset($e)) {
$e = new \Exception();
}
$lines = static::$lines;
$file = get_class($this).'.php';
foreach ($e->getTrace() as $trace) {
if (isset($trace['file']) && basename($trace['file']) == $file) {
$line = $trace['line'];
return isset($lines[$line]) ? $lines[$line] : null;
}
}
return null;
}
/**
* @param $name
* @param array $args
*
* @return mixed
*/
public function helper($name, $args = [])
{
$args = func_get_args();
$name = array_shift($args);
if (isset($this->helpers[$name]) && is_callable($this->helpers[$name])) {
return call_user_func_array($this->helpers[$name], $args);
} elseif (($helper = [Helper::class, $name]) && is_callable($helper)) {
return call_user_func_array($helper, $args);
}
throw new \RuntimeException(sprintf('undefined helper "%s" in %s line %d', $name, static::NAME, $this->getLineTrace()));
}
/**
* @param array $context
* @param array $blocks
* @param array $macros
* @param array $imports
*
* @return mixed
*/
abstract public function display($context = [], $blocks = [], $macros = [], $imports = []);
/**
* {@inheritdoc}
*/
public function render(array $context = [], array $blocks = [], array $macros = [], array $imports = []): string
{
ob_start();
$this->display($this->mergeContext($context), $blocks, $macros);
return ob_get_clean();
}
/**
* @param array $context
*
* @return array
*/
protected function mergeContext($context = [])
{
foreach ($this->variableProviders as $variableProvider) {
$context = array_merge($context, $variableProvider->getData());
}
return array_merge($context, ['__context' => array_keys($context)]);
}
/**
* @param $context
* @param $seq
*
* @return Helper\ContextIterator
*/
public function iterate($context, $seq)
{
return new Helper\ContextIterator($seq, isset($context['loop']) ?
$context['loop'] : null);
}
/**
* @return array
*/
public function getBlocks()
{
return $this->blocks;
}
/**
* @return array
*/
public function getMacros()
{
return $this->macros;
}
/**
* @return array
*/
public function getImports(): array
{
return $this->imports;
}
/**
* @param $obj
* @param $attr
* @param array $args
*
* @return mixed|null
*/
public function getAttr($obj, $attr, $args = [])
{
if (is_array($obj)) {
if (isset($obj[$attr])) {
if ($obj[$attr] instanceof \Closure) {
if (is_array($args)) {
array_unshift($args, $obj);
} else {
$args = [$obj];
}
return call_user_func_array($obj[$attr], $args);
}
return $obj[$attr];
}
return null;
} elseif (is_object($obj)) {
if (is_array($args)) {
$callable = [$obj, $attr];
return is_callable($callable) ?
call_user_func_array($callable, $args) : null;
}
$members = array_keys(get_object_vars($obj));
$methods = get_class_methods(get_class($obj));
if (in_array($attr, $members)) {
return @$obj->$attr;
} elseif (in_array('__get', $methods)) {
return $obj->__get($attr);
}
$callable = [$obj, $attr];
return is_callable($callable) ?
call_user_func($callable) : null;
}
return null;
}
/**
* @param $obj
* @param $attrs
* @param $value
*/
public function setAttr(&$obj, $attrs, $value)
{
if (empty($attrs)) {
$obj = $value;
return;
}
$attr = array_shift($attrs);
if (is_object($obj)) {
$class = get_class($obj);
$members = array_keys(get_object_vars($obj));
if (!in_array($attr, $members)) {
if (empty($attrs) && method_exists($obj, '__set')) {
$obj->__set($attr, $value);
return;
} elseif (property_exists($class, $attr)) {
throw new \RuntimeException(
"inaccessible '$attr' object attribute"
);
}
if ($attr === null || $attr === false || $attr === '') {
if ($attr === null) {
$token = 'null';
}
if ($attr === false) {
$token = 'false';
}
if ($attr === '') {
$token = 'empty string';
}
throw new \RuntimeException(sprintf(
'invalid object attribute (%s) in %s line %d',
$token,
static::NAME,
$this->getLineTrace()
));
}
$obj->{$attr} = null;
}
if (!isset($obj->$attr)) {
$obj->$attr = null;
}
$this->setAttr($obj->$attr, $attrs, $value);
} else {
if (!is_array($obj)) {
$obj = [];
}
$this->setAttr($obj[$attr], $attrs, $value);
}
}
}