|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates; |
|
4
|
|
|
|
|
5
|
|
|
/** API for the Plates system */ |
|
6
|
|
|
final class Engine |
|
7
|
|
|
{ |
|
8
|
|
|
private $container; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct($context = []) { |
|
11
|
|
|
$this->container = new Util\Container(); |
|
12
|
|
|
|
|
13
|
|
|
$this->container->add('engine_methods', []); |
|
14
|
|
|
$this->container->add('config', array_merge([ |
|
15
|
|
|
'render_context_var_name' => 'v', |
|
16
|
|
|
'ext' => 'phtml', |
|
17
|
|
|
'base_dir' => null, |
|
18
|
|
|
'escape_encoding' => null, |
|
19
|
|
|
'escape_flags' => null, |
|
20
|
|
|
], $context)); |
|
21
|
|
|
$this->container->add('include', function($c) { |
|
|
|
|
|
|
22
|
|
|
return Template\phpInclude(); |
|
23
|
|
|
}); |
|
24
|
|
|
$this->container->add('funcs', function($c) { |
|
25
|
|
|
return [RenderContext\platesFunc($c->get('config'))]; |
|
26
|
|
|
}); |
|
27
|
|
|
$this->container->add('resolve_name_stack', function($c) { |
|
28
|
|
|
return [Template\platesResolveName($c->get('config'))]; |
|
29
|
|
|
}); |
|
30
|
|
|
$this->container->add('resolve_data_stack', function() { |
|
31
|
|
|
return Util\id(); |
|
32
|
|
|
}); |
|
33
|
|
|
$this->container->add('createRenderContext', function($c) { |
|
34
|
|
|
return RenderContext::factory(Util\stack($c->get('funcs'))); |
|
35
|
|
|
}); |
|
36
|
|
|
$this->container->add('render', function($c) { |
|
37
|
|
|
$rt = new RenderTemplate\PlatesRenderTemplate( |
|
38
|
|
|
Util\stack($c->get('resolve_name_stack')), |
|
39
|
|
|
Util\stack($c->get('resolve_data_stack')), |
|
40
|
|
|
$c->get('include'), |
|
41
|
|
|
$c->get('createRenderContext'), |
|
42
|
|
|
$c->get('config')['render_context_var_name'] |
|
43
|
|
|
); |
|
44
|
|
|
$rt = new RenderTemplate\LayoutRenderTemplate($rt); |
|
45
|
|
|
return $rt; |
|
46
|
|
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @return string */ |
|
50
|
|
|
public function render($template_name, array $data) { |
|
51
|
|
|
$template = new Template($template_name, $data, [ |
|
52
|
|
|
'config' => $this->getConfig(), |
|
53
|
|
|
'container' => $this->container, |
|
54
|
|
|
]); |
|
55
|
|
|
return $this->container->get('render')->renderTemplate($template); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function __call($method, array $args) { |
|
59
|
|
|
$methods = $this->get('engine_methods'); |
|
60
|
|
|
if (isset($methods[$method])) { |
|
61
|
|
|
return $methods[$method]($this, ...$args); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
throw new \BadMethodCallException("No method {$method} found for engine."); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function addMethods(array $methods) { |
|
68
|
|
|
$this->container->merge('engine_methods', $methods); |
|
69
|
|
|
} |
|
70
|
|
|
public function addConfig(array $config) { |
|
71
|
|
|
$this->container->merge('config', $config); |
|
72
|
|
|
} |
|
73
|
|
|
public function getConfig() { |
|
74
|
|
|
$this->container->get('config'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function get($id) { |
|
78
|
|
|
return $this->container->get($id); |
|
79
|
|
|
} |
|
80
|
|
|
public function has($id) { |
|
81
|
|
|
return $this->contianer->has($id); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
public function add($id, $value) { |
|
84
|
|
|
$this->container->add($id, $value); |
|
85
|
|
|
} |
|
86
|
|
|
public function wrap($id, $wrapper) { |
|
87
|
|
|
$this->container->wrap($id, $wrapper); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.