Completed
Push — 183-default-layout-ext ( a97107 )
by
unknown
10:37
created

Engine::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 59
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 41
nc 1
nop 1
dl 0
loc 59
rs 9.597
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->add('compose', 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...
25
            return Util\id();
26
        });
27
        $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...
28
            return 'file_exists';
29
        });
30
        $this->container->add('renderTemplate', function($c) {
31
            $rt = new RenderTemplate\FileSystemRenderTemplate([
32
                [
33
                    Template\matchExtensions($c->get('config')['php_extensions']),
34
                    new RenderTemplate\PhpRenderTemplate($c->get('renderTemplate.bind'))
35
                ],
36
                [
37
                    Template\matchExtensions($c->get('config')['image_extensions']),
38
                    RenderTemplate\MapContentRenderTemplate::base64Encode(new RenderTemplate\StaticFileRenderTemplate())
39
                ],
40
                [
41
                    Template\matchStub(true),
42
                    new RenderTemplate\StaticFileRenderTemplate(),
43
                ]
44
            ]);
45
            if ($c->get('config')['validate_paths']) {
46
                $rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
47
            }
48
            // $rt = new RenderTemplate\LayoutRenderTemplate($rt);
49
            $rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
50
                return $create($rt);
51
            }, $rt);
52
            $rt = new RenderTemplate\ComposeRenderTemplate($rt, $c->get('compose'));
53
            return $rt;
54
        });
55
        $this->container->add('renderTemplate.bind', function() {
56
            return Util\id();
57
        });
58
        $this->container->add('renderTemplate.factories', function() {
59
            return [];
60
        });
61
        $this->register(new Extension\Data\DataExtension());
62
        $this->register(new Extension\Path\PathExtension());
63
        $this->register(new Extension\RenderContext\RenderContextExtension());
64
        $this->register(new Extension\LayoutSections\LayoutSectionsExtension());
65
        $this->register(new Extension\Folders\FoldersExtension());
66
67
        $this->addConfig($config);
68
    }
69
70
    /** @return string */
71
    public function render($template_name, array $data = [], array $attributes = []) {
72
        return $this->container->get('renderTemplate')->renderTemplate(new Template(
73
            $template_name,
74
            $data,
75
            $attributes
76
        ));
77
    }
78
79
    public function __call($method, array $args) {
80
        $methods = $this->container->get('engine_methods');
81
        if (isset($methods[$method])) {
82
            return $methods[$method]($this, ...$args);
83
        }
84
85
        throw new \BadMethodCallException("No method {$method} found for engine.");
86
    }
87
88
    public function register(Extension $extension) {
89
        $extension->register($this);
90
    }
91
    public function addMethods(array $methods) {
92
        $this->container->merge('engine_methods', $methods);
93
    }
94
    public function addConfig(array $config) {
95
        $this->container->merge('config', $config);
96
    }
97
98
    public function getContainer() {
99
        return $this->container;
100
    }
101
}
102