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
|
|
|
'validate_paths' => true, |
21
|
|
|
], $context)); |
22
|
|
|
$this->container->add('include', function($c) { |
23
|
|
|
$inc = Template\phpInclude(); |
24
|
|
|
if ($c->get('config')['validate_paths']) { |
25
|
|
|
$inc = Template\validatePathInclude($inc); |
26
|
|
|
} |
27
|
|
|
return $inc; |
28
|
|
|
}); |
29
|
|
|
$this->container->add('funcs', function($c) { |
30
|
|
|
return [RenderContext\platesFunc($c->get('config'))]; |
31
|
|
|
}); |
32
|
|
|
$this->container->add('resolve_name_stack', function($c) { |
33
|
|
|
return [Template\platesResolveName($c->get('config'))]; |
34
|
|
|
}); |
35
|
|
|
$this->container->add('resolve_data_stack', function() { |
36
|
|
|
return Util\id(); |
37
|
|
|
}); |
38
|
|
|
$this->container->add('createRenderContext', function($c) { |
39
|
|
|
return RenderContext::factory(Util\stack($c->get('funcs'))); |
40
|
|
|
}); |
41
|
|
|
$this->container->add('render', function($c) { |
42
|
|
|
$rt = new RenderTemplate\PlatesRenderTemplate( |
43
|
|
|
Util\stack($c->get('resolve_name_stack')), |
44
|
|
|
$c->get('resolve_data_stack'), |
45
|
|
|
$c->get('include'), |
46
|
|
|
$c->get('createRenderContext'), |
47
|
|
|
$c->get('config')['render_context_var_name'] |
48
|
|
|
); |
49
|
|
|
$rt = new RenderTemplate\LayoutRenderTemplate($rt); |
50
|
|
|
return $rt; |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @return string */ |
55
|
|
|
public function render($template_name, array $data) { |
56
|
|
|
$template = new Template($template_name, $data, [ |
57
|
|
|
'config' => $this->getConfig(), |
58
|
|
|
'container' => $this->container, |
59
|
|
|
]); |
60
|
|
|
return $this->container->get('render')->renderTemplate($template); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function __call($method, array $args) { |
64
|
|
|
$methods = $this->get('engine_methods'); |
65
|
|
|
if (isset($methods[$method])) { |
66
|
|
|
return $methods[$method]($this, ...$args); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \BadMethodCallException("No method {$method} found for engine."); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function addMethods(array $methods) { |
73
|
|
|
$this->container->merge('engine_methods', $methods); |
74
|
|
|
} |
75
|
|
|
public function addConfig(array $config) { |
76
|
|
|
$this->container->merge('config', $config); |
77
|
|
|
} |
78
|
|
|
public function getConfig() { |
79
|
|
|
$this->container->get('config'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function get($id) { |
83
|
|
|
return $this->container->get($id); |
84
|
|
|
} |
85
|
|
|
public function has($id) { |
86
|
|
|
return $this->contianer->has($id); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
public function add($id, $value) { |
89
|
|
|
$this->container->add($id, $value); |
90
|
|
|
} |
91
|
|
|
public function wrap($id, $wrapper) { |
92
|
|
|
$this->container->wrap($id, $wrapper); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: