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; |
|
|
|
|
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
|
|
|
|
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.