Completed
Push — 186-data ( d4ee1e )
by
unknown
08:25 queued 10s
created

RenderContext::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\RenderContext;
4
5
use BadMethodCallException;
6
7
final class RenderContext
8
{
9
    public $render;
10
    public $template;
11
12
    private $func_stack;
13
14
    public function __construct(
15
        RenderTemplate $render,
16
        Template $template,
17
        $func_stack = null
18
    ) {
19
        $this->render = $render;
20
        $this->template = $template;
21
        $this->func_stack = $func_stack ?: Util\stack([RenderContext\platesFunc()]);
22
    }
23
24
    public function __get($name) {
25
        if (!$this->func_stack) {
26
            throw new BadMethodCallException('Cannot access ' . $name . ' because no func stack has been setup.');
27
        }
28
29
        $func_stack = $this->func_stack;
30
        return $func_stack(new RenderContext\FuncArgs(
31
            $this->render,
32
            $this->template,
33
            $name
34
        ));
35
    }
36
37
    public function __set($name, $value) {
38
        throw new BadMethodCallException('Cannot set ' . $name . ' on this render context.');
39
    }
40
41
    public function __call($name, array $args) {
42
        if (!$this->func_stack) {
43
            throw new BadMethodCallException('Cannot call ' . $name . ' because no func stack has been setup.');
44
        }
45
46
        $func_stack = $this->func_stack;
47
        return $func_stack(new RenderContext\FuncArgs(
48
            $this->render,
49
            $this->template,
50
            $name,
51
            $args
52
        ));
53
    }
54
55
    public function __invoke(...$args) {
56
        if (!$this->func_stack) {
57
            throw new BadMethodCallException('Cannot invoke the render context because no func stack has been setup.');
58
        }
59
60
        $func_stack = $this->func_stack;
61
        return $func_stack(new RenderContext\FuncArgs(
62
            $this->render,
63
            $this->template,
64
            '__invoke',
65
            $args
66
        ));
67
    }
68
69
    public static function factory($func_stack = null) {
70
        return function(RenderTemplate $render, Template $template) use (
71
            $func_stack
72
        ) {
73
            return new self(
74
                $render,
75
                $template,
76
                $func_stack
77
            );
78
        };
79
    }
80
}
81