Completed
Push — master ( b3978b...8d3a7c )
by
unknown
26:09 queued 12:30
created

PlatesExtension   A

Coupling/Cohesion

Components 0
Dependencies 8

Complexity

Total Complexity 2

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
A register() 0 69 2
1
<?php
2
3
namespace League\Plates;
4
5
final class PlatesExtension implements Extension
6
{
7
    public function register(Engine $plates) {
8
        $c = $plates->getContainer();
9
10
        $c->add('config', [
11
            'render_context_var_name' => 'v',
12
            'ext' => 'phtml',
13
            'base_dir' => null,
14
            'escape_encoding' => null,
15
            'escape_flags' => null,
16
            'validate_paths' => true,
17
            'php_extensions' => ['php', 'phtml'],
18
            'image_extensions' => ['png', 'jpg'],
19
        ]);
20
        $c->addComposed('compose', function() { return []; });
21
        $c->add('fileExists', function($c) {
0 ignored issues
show
Unused Code introduced by RJ Garcia
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
            return 'file_exists';
23
        });
24
        $c->add('renderTemplate', function($c) {
25
            $rt = new RenderTemplate\FileSystemRenderTemplate([
26
                [
27
                    Template\matchExtensions($c->get('config')['php_extensions']),
28
                    new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind'))
29
                ],
30
                [
31
                    Template\matchExtensions($c->get('config')['image_extensions']),
32
                    RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate())
33
                ],
34
                [
35
                    Template\matchStub(true),
36
                    new RenderTemplate\StaticFileRenderTemplate(),
37
                ]
38
            ]);
39
            if ($c->get('config')['validate_paths']) {
40
                $rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
41
            }
42
            $rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
43
                return $create($rt);
44
            }, $rt);
45
            $rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose'));
46
            return $rt;
47
        });
48
        $c->add('renderTemplate.bind', function() {
49
            return Util\id();
50
        });
51
        $c->add('renderTemplate.factories', function() {
52
            return [];
53
        });
54
55
        $plates->addMethods([
56
            'pushComposers' => function(Engine $e, $def_composer) {
57
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
58
                    return array_merge($composed, $def_composer($c));
59
                });
60
            },
61
            'unshiftComposers' => function(Engine $e, $def_composer) {
62
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
63
                    return array_merge($def_composer($c), $composed);
64
                });
65
            },
66
            'addConfig' => function(Engine $e, array $config) {
67
                $e->getContainer()->merge('config', $config);
68
            },
69
            /** merges in config values, but will defer to values already set in the config */
70
            'defineConfig' => function(Engine $e, array $config_def) {
71
                $config = $e->getContainer()->get('config');
72
                $e->getContainer()->add('config', array_merge($config_def, $config));
73
            },
74
        ]);
75
    }
76
}
77