Completed
Push — v4.0-dev ( 6660e2...b14a8f )
by
unknown
11s
created

Engine::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 1
dl 0
loc 38
ccs 0
cts 37
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
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($context = []) {
11
        $this->container = new Util\Container();
12
13
        $this->container->add('engine_methods', []);
14
        $this->container->add('config', array_merge([
15
            'render_context_var_name' => 'v',
16
            'ext' => 'phtml',
17
            'base_dir' => null,
18
            'escape_encoding' => null,
19
            'escape_flags' => null,
20
        ], $context));
21
        $this->container->add('include', 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...
22
            return Template\phpInclude();
23
        });
24
        $this->container->add('funcs', function($c) {
25
            return [RenderContext\platesFunc($c->get('config'))];
26
        });
27
        $this->container->add('resolve_name_stack', function($c) {
28
            return [Template\platesResolveName($c->get('config'))];
29
        });
30
        $this->container->add('resolve_data_stack', function() {
31
            return Util\id();
32
        });
33
        $this->container->add('createRenderContext', function($c) {
34
            return RenderContext::factory(Util\stack($c->get('funcs')));
35
        });
36
        $this->container->add('render', function($c) {
37
            $rt = new RenderTemplate\PlatesRenderTemplate(
38
                Util\stack($c->get('resolve_name_stack')),
39
                Util\stack($c->get('resolve_data_stack')),
40
                $c->get('include'),
41
                $c->get('createRenderContext'),
42
                $c->get('config')['render_context_var_name']
43
            );
44
            $rt = new RenderTemplate\LayoutRenderTemplate($rt);
45
            return $rt;
46
        });
47
    }
48
49
    /** @return string */
50
    public function render($template_name, array $data) {
51
        $template = new Template($template_name, $data, [
52
            'config' => $this->getConfig(),
53
            'container' => $this->container,
54
        ]);
55
        return $this->container->get('render')->renderTemplate($template);
56
    }
57
58
    public function __call($method, array $args) {
59
        $methods = $this->get('engine_methods');
60
        if (isset($methods[$method])) {
61
            return $methods[$method]($this, ...$args);
62
        }
63
64
        throw new \BadMethodCallException("No method {$method} found for engine.");
65
    }
66
67
    public function addMethods(array $methods) {
68
        $this->container->merge('engine_methods', $methods);
69
    }
70
    public function addConfig(array $config) {
71
        $this->container->merge('config', $config);
72
    }
73
    public function getConfig() {
74
        $this->container->get('config');
75
    }
76
77
    public function get($id) {
78
        return $this->container->get($id);
79
    }
80
    public function has($id) {
81
        return $this->contianer->has($id);
0 ignored issues
show
Bug introduced by
The property contianer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
    }
83
    public function add($id, $value) {
84
        $this->container->add($id, $value);
85
    }
86
    public function wrap($id, $wrapper) {
87
        $this->container->wrap($id, $wrapper);
88
    }
89
}
90