Completed
Pull Request — v4.0-dev (#191)
by
unknown
50:27 queued 35:28
created

DataExtension::register()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 1
nop 1
dl 0
loc 32
rs 8.439
c 0
b 0
f 0
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
    public function register(Plates\Engine $plates) {
11
        $c = $plates->getContainer();
12
        $c->add('data.globals', []);
13
        $c->add('data.template_data', []);
14
        $c->merge('config', ['merge_parent_data' => true]);
15
        $c->wrap('compose', function($compose, $c) {
16
            return Plates\Util\compose(...array_filter([
17
                $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
18
                $c->get('data.globals') ? globalResolveData($c->get('data.globals')) : null,
19
                $c->get('data.template_data') ? perTemplateResolveData($c->get('data.template_data')) : null,
20
                $compose
21
            ]));
22
        });
23
24
        $plates->addMethods([
25
            'addGlobals' => function(Plates\Engine $e, array $data) {
26
                $c = $e->getContainer();
27
                $c->merge('data.globals', $data);
28
            },
29
            'addGlobal' => function(Plates\Engine $e, $name, $value) {
30
                $e->getContainer()->merge('data.globals', [$name => $value]);
31
            },
32
            'assignTemplateData' => function(Plates\Engine $e, $name, $data) {
33
                $template_data = $e->getContainer()->get('data.template_data');
34
                if (!isset($template_data[$name])) {
35
                    $template_data[$name] = [];
36
                }
37
                $template_data[$name] = array_merge($template_data[$name], $data);
38
                $e->getContainer()->add('data.template_data', $template_data);
39
            }
40
        ]);
41
    }
42
}
43