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($config = []) { |
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
|
|
|
'php_extensions' => ['php', 'phtml'], |
22
|
|
|
'image_extensions' => ['png', 'jpg'], |
23
|
|
|
], $config)); |
24
|
|
|
$this->container->add('compose', function($c) { |
|
|
|
|
25
|
|
|
return Util\id(); |
26
|
|
|
}); |
27
|
|
|
$this->container->add('fileExists', function($c) { |
|
|
|
|
28
|
|
|
return 'file_exists'; |
29
|
|
|
}); |
30
|
|
|
$this->container->add('renderTemplate', function($c) { |
31
|
|
|
$rt = new RenderTemplate\FileSystemRenderTemplate([ |
32
|
|
|
[ |
33
|
|
|
Template\matchExtensions($c->get('config')['php_extensions']), |
34
|
|
|
new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind')) |
35
|
|
|
], |
36
|
|
|
[ |
37
|
|
|
Template\matchExtensions($c->get('config')['image_extensions']), |
38
|
|
|
RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate()) |
39
|
|
|
], |
40
|
|
|
[ |
41
|
|
|
Template\matchStub(true), |
42
|
|
|
new RenderTemplate\StaticFileRenderTemplate(), |
43
|
|
|
] |
44
|
|
|
]); |
45
|
|
|
if ($c->get('config')['validate_paths']) { |
46
|
|
|
$rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists')); |
47
|
|
|
} |
48
|
|
|
// $rt = new RenderTemplate\LayoutRenderTemplate($rt); |
49
|
|
|
$rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) { |
50
|
|
|
return $create($rt); |
51
|
|
|
}, $rt); |
52
|
|
|
$rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose')); |
53
|
|
|
return $rt; |
54
|
|
|
}); |
55
|
|
|
$this->container->add('renderTemplate.bind', function() { |
56
|
|
|
return Util\id(); |
57
|
|
|
}); |
58
|
|
|
$this->container->add('renderTemplate.factories', function() { |
59
|
|
|
return []; |
60
|
|
|
}); |
61
|
|
|
$this->register(new Extension\Data\DataExtension()); |
62
|
|
|
$this->register(new Extension\Path\PathExtension()); |
63
|
|
|
$this->register(new Extension\RenderContext\RenderContextExtension()); |
64
|
|
|
$this->register(new Extension\LayoutSections\LayoutSectionsExtension()); |
65
|
|
|
$this->register(new Extension\Folders\FoldersExtension()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @return string */ |
69
|
|
|
public function render($template_name, array $data = [], array $attributes = []) { |
70
|
|
|
return $this->container->get('renderTemplate')->renderTemplate(new Template( |
71
|
|
|
$template_name, |
72
|
|
|
$data, |
73
|
|
|
$attributes |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function __call($method, array $args) { |
78
|
|
|
$methods = $this->container->get('engine_methods'); |
79
|
|
|
if (isset($methods[$method])) { |
80
|
|
|
return $methods[$method]($this, ...$args); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \BadMethodCallException("No method {$method} found for engine."); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function register(Extension $extension) { |
87
|
|
|
$extension->register($this); |
88
|
|
|
} |
89
|
|
|
public function addMethods(array $methods) { |
90
|
|
|
$this->container->merge('engine_methods', $methods); |
91
|
|
|
} |
92
|
|
|
public function addConfig(array $config) { |
93
|
|
|
$this->container->merge('config', $config); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getContainer() { |
97
|
|
|
return $this->container; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.