1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Plates; |
4
|
|
|
|
5
|
|
|
final class PlatesExtension implements Extension |
6
|
|
|
{ |
7
|
24 |
|
public function register(Engine $plates) { |
8
|
24 |
|
$c = $plates->getContainer(); |
9
|
|
|
|
10
|
24 |
|
$c->add('config', [ |
11
|
24 |
|
'validate_paths' => true, |
12
|
|
|
'php_extensions' => ['php', 'phtml'], |
13
|
|
|
'image_extensions' => ['png', 'jpg'], |
14
|
|
|
]); |
15
|
|
|
$c->addComposed('compose', function() { return []; }); |
16
|
24 |
|
$c->add('fileExists', function($c) { |
|
|
|
|
17
|
24 |
|
return 'file_exists'; |
18
|
24 |
|
}); |
19
|
24 |
|
$c->add('renderTemplate', function($c) { |
20
|
24 |
|
$rt = new RenderTemplate\FileSystemRenderTemplate([ |
21
|
|
|
[ |
22
|
24 |
|
Template\matchExtensions($c->get('config')['php_extensions']), |
23
|
24 |
|
new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind')) |
24
|
|
|
], |
25
|
|
|
[ |
26
|
24 |
|
Template\matchExtensions($c->get('config')['image_extensions']), |
27
|
24 |
|
RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate()) |
28
|
|
|
], |
29
|
|
|
[ |
30
|
24 |
|
Template\matchStub(true), |
31
|
24 |
|
new RenderTemplate\StaticFileRenderTemplate(), |
32
|
|
|
] |
33
|
|
|
]); |
34
|
24 |
|
if ($c->get('config')['validate_paths']) { |
35
|
24 |
|
$rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists')); |
36
|
|
|
} |
37
|
24 |
|
$rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) { |
38
|
24 |
|
return $create($rt); |
39
|
24 |
|
}, $rt); |
40
|
24 |
|
$rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose')); |
41
|
24 |
|
return $rt; |
42
|
24 |
|
}); |
43
|
24 |
|
$c->add('renderTemplate.bind', function() { |
44
|
24 |
|
return Util\id(); |
45
|
24 |
|
}); |
46
|
24 |
|
$c->add('renderTemplate.factories', function() { |
47
|
24 |
|
return []; |
48
|
24 |
|
}); |
49
|
|
|
|
50
|
24 |
|
$plates->addMethods([ |
51
|
|
|
'pushComposers' => function(Engine $e, $def_composer) { |
52
|
24 |
|
$e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) { |
53
|
24 |
|
return array_merge($composed, $def_composer($c)); |
54
|
24 |
|
}); |
55
|
24 |
|
}, |
56
|
|
|
'unshiftComposers' => function(Engine $e, $def_composer) { |
57
|
|
|
$e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) { |
58
|
|
|
return array_merge($def_composer($c), $composed); |
59
|
|
|
}); |
60
|
24 |
|
}, |
61
|
24 |
|
'addConfig' => function(Engine $e, array $config) { |
62
|
24 |
|
$e->getContainer()->merge('config', $config); |
63
|
24 |
|
}, |
64
|
|
|
/** merges in config values, but will defer to values already set in the config */ |
65
|
24 |
|
'defineConfig' => function(Engine $e, array $config_def) { |
66
|
24 |
|
$config = $e->getContainer()->get('config'); |
67
|
24 |
|
$e->getContainer()->add('config', array_merge($config_def, $config)); |
68
|
24 |
|
}, |
69
|
|
|
]); |
70
|
24 |
|
} |
71
|
|
|
} |
72
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.