These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace League\Plates\Extension\Data; |
||
4 | |||
5 | use League\Plates; |
||
6 | |||
7 | /** The DataExtension adds the ability to hydrate data into a template before it gets rendered. */ |
||
8 | final class DataExtension implements Plates\Extension |
||
9 | { |
||
10 | 16 | public function register(Plates\Engine $plates) { |
|
11 | 16 | $c = $plates->getContainer(); |
|
12 | 16 | $c->add('data.globals', []); |
|
13 | 16 | $c->add('data.template_data', []); |
|
14 | 16 | ||
15 | $plates->defineConfig(['merge_parent_data' => true]); |
||
16 | 16 | $plates->pushComposers(function($c) { |
|
17 | 16 | return array_filter([ |
|
18 | 16 | 'data.addGlobals' => $c->get('data.globals') ? addGlobalsCompose($c->get('data.globals')) : null, |
|
19 | 16 | 'data.mergeParentData' => $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null, |
|
20 | 16 | 'data.perTemplateData' => $c->get('data.template_data') ? perTemplateDataCompose($c->get('data.template_data')) : null, |
|
21 | ]); |
||
22 | 16 | }); |
|
23 | |||
24 | 16 | $plates->addMethods([ |
|
25 | 16 | 'addGlobals' => function(Plates\Engine $e, array $data) { |
|
26 | 4 | $c = $e->getContainer(); |
|
27 | 4 | $c->merge('data.globals', $data); |
|
28 | 16 | }, |
|
29 | 16 | 'addGlobal' => function(Plates\Engine $e, $name, $value) { |
|
30 | $e->getContainer()->merge('data.globals', [$name => $value]); |
||
31 | 16 | }, |
|
32 | 16 | 'addData' => function(Plates\Engine $e, $data, $name = null) { |
|
33 | 4 | if (!$name) { |
|
34 | 4 | return $e->addGlobals($data); |
|
0 ignored issues
–
show
|
|||
35 | 4 | } |
|
36 | |||
37 | 4 | $template_data = $e->getContainer()->get('data.template_data'); |
|
38 | 4 | if (!isset($template_data[$name])) { |
|
39 | 16 | $template_data[$name] = []; |
|
40 | } |
||
41 | 16 | $template_data[$name] = array_merge($template_data[$name], $data); |
|
42 | $e->getContainer()->add('data.template_data', $template_data); |
||
43 | } |
||
44 | ]); |
||
45 | } |
||
46 | } |
||
47 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: