Completed
Push — 186-data ( 572d68...d14d7f )
by
unknown
10:34
created

RenderContext::invokeFuncStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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