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

RenderContext   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A __get() 0 7 2
A __set() 0 3 1
A __call() 0 7 2
A __invoke() 0 7 2
A invokeFuncStack() 0 8 1
A factory() 0 9 1
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