Completed
Push — v4.0-dev ( b01e70 )
by
unknown
01:53
created

RenderContext   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __get() 0 12 2
A __set() 0 3 1
A __call() 0 13 2
A __invoke() 0 13 2
A createFactory() 0 13 1
1
<?php
2
3
namespace League\Plates;
4
5
use BadMethodCallException;
6
7
final class RenderContext
8
{
9
    private $prop_stack;
10
    private $func_stack;
11
12
    private $render;
13
    private $template;
14
15
    public function __construct(
16
        RenderTemplate $render,
17
        Template $template,
18
        $prop_stack = null,
19
        $func_stack = null
20
    ) {
21
        $this->render = $render;
22
        $this->template = $template;
23
24
        $this->prop_stack = $prop_stack;
25
        $this->func_stack = $func_stack;
26
    }
27
28
    public function __get($name) {
29
        if (!$this->prop_stack) {
30
            throw new BadMethodCallException('Cannot access ' . $name . ' because no prop stack was setup.');
31
        }
32
33
        $prop_stack = $this->prop_stack;
34
        return $prop_stack(new RenderContext\PropArgs(
35
            $this->render,
36
            $this->template,
37
            $name
38
        ));
39
    }
40
41
    public function __set($name) {
42
        throw new BadMethodCallException('Cannot set ' . $name . ' on this render context.');
43
    }
44
45
    public function __call($name, array $args) {
46
        if (!$this->func_stack) {
47
            throw new BadMethodCallException('Cannot call ' . $name . ' because no func stack has been setup.');
48
        }
49
50
        $func_stack = $this->func_stack;
51
        return $func_stack(new RenderContext\FuncArgs(
52
            $this->render,
53
            $this->template,
54
            $name,
55
            $args
56
        ));
57
    }
58
59
    public function __invoke(...$args) {
60
        if (!$this->func_stack) {
61
            throw new BadMethodCallException('Cannot invoke the render context because no func stack has been setup.');
62
        }
63
64
        $func_stack = $this->func_stack;
65
        return $func_stack(new RenderContext\FuncArgs(
66
            $this->render,
67
            $this->template,
68
            null,
69
            $args
70
        ));
71
    }
72
73
    public static function createFactory($prop_stack = null, $func_stack = null) {
74
        return function(RenderTemplate $render, Template $template) use (
75
            $prop_stack,
76
            $func_stack
77
        ) {
78
            return new self(
79
                $render,
80
                $template,
81
                $prop_stack,
82
                $func_stack
83
            );
84
        };
85
    }
86
}
87