Completed
Pull Request — v4.0-dev (#203)
by
unknown
01:35
created

Engine::__construct()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 77
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 3

Importance

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