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

Engine   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 97.3%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 114
ccs 72
cts 74
cp 0.973
rs 9.1666
c 4
b 0
f 0
wmc 10
lcom 0
cbo 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 77 3
A render() 0 7 1
A __call() 0 8 2
A register() 0 3 1
A addMethods() 0 3 1
A addConfig() 0 3 1
A getContainer() 0 3 1
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