Completed
Push — auto-escape ( 46bb2f...7a06cf )
by
unknown
38:05 queued 23:10
created

Engine::__construct()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 77
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 53
nc 1
nop 1
dl 0
loc 77
rs 8.9342
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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', [
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
        ]);
24
        $this->container->addComposed('compose', function() { return []; });
25
        $this->container->add('fileExists', function($c) {
0 ignored issues
show
Unused Code introduced by
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...
26
            return 'file_exists';
27
        });
28
        $this->container->add('escape', function($c) {
29
            $config = $c->get('config');
30
            return isset($config['escape_flags'], $config['escape_encoding'])
31
                ? Util\escape($config['escape_flags'], $config['escape_encoding'])
32
                : Util\escape();
33
        });
34
        $this->container->add('renderTemplate', function($c) {
35
            $rt = new RenderTemplate\FileSystemRenderTemplate([
36
                [
37
                    Template\matchExtensions($c->get('config')['php_extensions']),
38
                    new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind'))
39
                ],
40
                [
41
                    Template\matchExtensions($c->get('config')['image_extensions']),
42
                    RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate())
43
                ],
44
                [
45
                    Template\matchStub(true),
46
                    new RenderTemplate\StaticFileRenderTemplate(),
47
                ]
48
            ]);
49
            if ($c->get('config')['validate_paths']) {
50
                $rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
51
            }
52
            $rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
53
                return $create($rt);
54
            }, $rt);
55
            $rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose'));
56
            return $rt;
57
        });
58
        $this->container->add('renderTemplate.bind', function() {
59
            return Util\id();
60
        });
61
        $this->container->add('renderTemplate.factories', function() {
62
            return [];
63
        });
64
65
        $this->addMethods([
66
            'pushComposers' => function(Engine $e, $def_composer) {
67
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
68
                    return array_merge($composed, $def_composer($c));
69
                });
70
            },
71
            'unshiftComposers' => function(Engine $e, $def_composer) {
72
                $e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
73
                    return array_merge($def_composer($c), $composed);
74
                });
75
            },
76
        ]);
77
78
        $this->register(new Extension\Data\DataExtension());
79
        $this->register(new Extension\Path\PathExtension());
80
        $this->register(new Extension\RenderContext\RenderContextExtension());
81
        $this->register(new Extension\LayoutSections\LayoutSectionsExtension());
82
        $this->register(new Extension\Folders\FoldersExtension());
83
        $this->register(new Extension\AutoEscape\AutoEscapeExtension());
84
85
        $this->addConfig($config);
86
    }
87
88
    /** @return string */
89
    public function render($template_name, array $data = [], array $attributes = []) {
90
        return $this->container->get('renderTemplate')->renderTemplate(new Template(
91
            $template_name,
92
            $data,
93
            $attributes
94
        ));
95
    }
96
97
    public function __call($method, array $args) {
98
        $methods = $this->container->get('engine_methods');
99
        if (isset($methods[$method])) {
100
            return $methods[$method]($this, ...$args);
101
        }
102
103
        throw new \BadMethodCallException("No method {$method} found for engine.");
104
    }
105
106
    public function register(Extension $extension) {
107
        $extension->register($this);
108
    }
109
    public function addMethods(array $methods) {
110
        $this->container->merge('engine_methods', $methods);
111
    }
112
    public function addConfig(array $config) {
113
        $this->container->merge('config', $config);
114
    }
115
116
    public function getContainer() {
117
        return $this->container;
118
    }
119
}
120