Completed
Push — v4.0-dev ( b14a8f...3b7e85 )
by
unknown
19:33 queued 05:42
created

Engine::__construct()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 43
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 33
nc 1
nop 1
dl 0
loc 43
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
            'validate_paths' => true,
21
        ], $context));
22
        $this->container->add('include', function($c) {
23
            $inc = Template\phpInclude();
24
            if ($c->get('config')['validate_paths']) {
25
                $inc = Template\validatePathInclude($inc);
26
            }
27
            return $inc;
28
        });
29
        $this->container->add('funcs', function($c) {
30
            return [RenderContext\platesFunc($c->get('config'))];
31
        });
32
        $this->container->add('resolve_name_stack', function($c) {
33
            return [Template\platesResolveName($c->get('config'))];
34
        });
35
        $this->container->add('resolve_data_stack', function() {
36
            return Util\id();
37
        });
38
        $this->container->add('createRenderContext', function($c) {
39
            return RenderContext::factory(Util\stack($c->get('funcs')));
40
        });
41
        $this->container->add('render', function($c) {
42
            $rt = new RenderTemplate\PlatesRenderTemplate(
43
                Util\stack($c->get('resolve_name_stack')),
44
                $c->get('resolve_data_stack'),
45
                $c->get('include'),
46
                $c->get('createRenderContext'),
47
                $c->get('config')['render_context_var_name']
48
            );
49
            $rt = new RenderTemplate\LayoutRenderTemplate($rt);
50
            return $rt;
51
        });
52
    }
53
54
    /** @return string */
55
    public function render($template_name, array $data) {
56
        $template = new Template($template_name, $data, [
57
            'config' => $this->getConfig(),
58
            'container' => $this->container,
59
        ]);
60
        return $this->container->get('render')->renderTemplate($template);
61
    }
62
63
    public function __call($method, array $args) {
64
        $methods = $this->get('engine_methods');
65
        if (isset($methods[$method])) {
66
            return $methods[$method]($this, ...$args);
67
        }
68
69
        throw new \BadMethodCallException("No method {$method} found for engine.");
70
    }
71
72
    public function addMethods(array $methods) {
73
        $this->container->merge('engine_methods', $methods);
74
    }
75
    public function addConfig(array $config) {
76
        $this->container->merge('config', $config);
77
    }
78
    public function getConfig() {
79
        $this->container->get('config');
80
    }
81
82
    public function get($id) {
83
        return $this->container->get($id);
84
    }
85
    public function has($id) {
86
        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...
87
    }
88
    public function add($id, $value) {
89
        $this->container->add($id, $value);
90
    }
91
    public function wrap($id, $wrapper) {
92
        $this->container->wrap($id, $wrapper);
93
    }
94
}
95