Completed
Push — master ( 3a4c8a...558346 )
by Maxim
02:41
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\mvc\view;
4
5
use WebComplete\core\utils\alias\AliasService;
6
use WebComplete\mvc\assets\AssetManager;
7
use WebComplete\mvc\controller\AbstractController;
8
9
class View implements ViewInterface
10
{
11
12
    protected $layoutPath;
13
    protected $layoutVars = [];
14
    protected $templatePath;
15
    protected $templateVars = [];
16
    protected $controller;
17
18
    /**
19
     * @var AliasService|null
20
     */
21
    private $aliasService;
22
    /**
23
     * @var AssetManager
24
     */
25
    private $assetManager;
26
27
    /**
28
     * @param AliasService $aliasService
29
     * @param AssetManager $assetManager
30
     */
31
    public function __construct(AliasService $aliasService, AssetManager $assetManager)
32
    {
33
        $this->aliasService = $aliasService;
34
        $this->assetManager = $assetManager;
35
    }
36
37
    /**
38
     * @param string|null $path
39
     * @param array $vars
40
     *
41
     * @return $this|ViewInterface
42
     * @throws \WebComplete\core\utils\alias\AliasException
43
     */
44
    public function layout(string $path = null, array $vars = []): ViewInterface
45
    {
46
        $this->layoutPath = $path ? $this->aliasService->get($path) : null;
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $this->layoutPath = $path ? $this->aliasService->/** @scrutinizer ignore-call */ get($path) : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        $this->layoutVars = $vars;
48
        return $this;
49
    }
50
51
    /**
52
     * @param $path
53
     * @param array $vars
54
     *
55
     * @return string
56
     * @throws \RuntimeException
57
     * @throws \InvalidArgumentException
58
     * @throws \WebComplete\core\utils\alias\AliasException
59
     */
60
    public function render($path, array $vars = []): string
61
    {
62
        $this->templatePath = $this->aliasService->get($path);
63
        $this->templateVars = $vars;
64
        $result = $this->eval($this->templatePath, $this->templateVars);
65
        if ($this->layoutPath) {
66
            $this->layoutVars['view'] = $this;
67
            $this->layoutVars['content'] = $result;
68
            $result = $this->eval($this->layoutPath, $this->layoutVars);
69
            $this->layoutPath = null;
70
            $this->layoutVars = [];
71
        }
72
        return $result;
73
    }
74
75
    /**
76
     * @param $path
77
     * @param $vars
78
     *
79
     * @return string
80
     */
81
    protected function eval($path, $vars): string
82
    {
83
        \extract($vars, \EXTR_SKIP);
84
        \ob_start();
85
        require $path;
86
        return \ob_get_clean();
87
    }
88
89
    /**
90
     * @param AbstractController $controller
91
     */
92
    public function setController(AbstractController $controller)
93
    {
94
        $this->controller = $controller;
95
    }
96
97
    /**
98
     * @return AssetManager
99
     */
100
    public function getAssetManager(): AssetManager
101
    {
102
        return $this->assetManager;
103
    }
104
105
    /**
106
     * @return AbstractController|null
107
     */
108
    public function getController()
109
    {
110
        return $this->controller;
111
    }
112
}
113