1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\mvc\view; |
4
|
|
|
|
5
|
|
|
use WebComplete\core\utils\alias\AliasService; |
6
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
7
|
|
|
use WebComplete\mvc\assets\AssetManager; |
8
|
|
|
use WebComplete\mvc\controller\AbstractController; |
9
|
|
|
|
10
|
|
|
class View implements ViewInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $layoutPath; |
14
|
|
|
protected $layoutVars = []; |
15
|
|
|
protected $templatePath; |
16
|
|
|
protected $templateVars = []; |
17
|
|
|
protected $controller; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var AliasService|null |
21
|
|
|
*/ |
22
|
|
|
private $aliasService; |
23
|
|
|
/** |
24
|
|
|
* @var ContainerInterface|null |
25
|
|
|
*/ |
26
|
|
|
private $container; |
27
|
|
|
/** |
28
|
|
|
* @var AssetManager |
29
|
|
|
*/ |
30
|
|
|
private $assetManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param AliasService $aliasService |
34
|
|
|
* @param AssetManager $assetManager |
35
|
|
|
*/ |
36
|
|
|
public function __construct(AliasService $aliasService, AssetManager $assetManager) |
37
|
|
|
{ |
38
|
|
|
$this->aliasService = $aliasService; |
39
|
|
|
$this->assetManager = $assetManager; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|null $path |
44
|
|
|
* @param array $vars |
45
|
|
|
* |
46
|
|
|
* @return $this|ViewInterface |
47
|
|
|
* @throws \WebComplete\core\utils\alias\AliasException |
48
|
|
|
*/ |
49
|
|
|
public function layout(string $path = null, array $vars = []): ViewInterface |
50
|
|
|
{ |
51
|
|
|
$this->layoutPath = $path ? $this->aliasService->get($path) : null; |
|
|
|
|
52
|
|
|
$this->layoutVars = $vars; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $path |
58
|
|
|
* @param array $vars |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
* @throws \RuntimeException |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
* @throws \WebComplete\core\utils\alias\AliasException |
64
|
|
|
*/ |
65
|
|
|
public function render($path, array $vars = []): string |
66
|
|
|
{ |
67
|
|
|
$this->templatePath = $this->aliasService->get($path); |
68
|
|
|
$this->templateVars = $vars; |
69
|
|
|
$result = $this->eval($this->templatePath, $this->templateVars); |
70
|
|
|
if ($this->layoutPath) { |
71
|
|
|
$layoutPath = $this->layoutPath; |
72
|
|
|
$this->layoutPath = null; |
73
|
|
|
$this->layoutVars['view'] = $this; |
74
|
|
|
$this->layoutVars['content'] = $result; |
75
|
|
|
$result = $this->eval($layoutPath, $this->layoutVars); |
76
|
|
|
$this->layoutVars = []; |
77
|
|
|
} |
78
|
|
|
return $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $path |
83
|
|
|
* @param $vars |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function eval($path, $vars): string |
88
|
|
|
{ |
89
|
|
|
\extract($vars, \EXTR_SKIP); |
90
|
|
|
\ob_start(); |
91
|
|
|
require $path; |
92
|
|
|
return \ob_get_clean(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param ContainerInterface $container |
97
|
|
|
*/ |
98
|
|
|
public function setContainer(ContainerInterface $container) |
99
|
|
|
{ |
100
|
|
|
$this->container = $container; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ContainerInterface |
105
|
|
|
*/ |
106
|
|
|
public function getContainer(): ContainerInterface |
107
|
|
|
{ |
108
|
|
|
if (!$this->container) { |
109
|
|
|
global $application; |
110
|
|
|
$this->setContainer($application->getContainer()); |
111
|
|
|
} |
112
|
|
|
return $this->container; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param AbstractController $controller |
117
|
|
|
*/ |
118
|
|
|
public function setController(AbstractController $controller) |
119
|
|
|
{ |
120
|
|
|
$this->controller = $controller; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return AssetManager |
125
|
|
|
*/ |
126
|
|
|
public function getAssetManager(): AssetManager |
127
|
|
|
{ |
128
|
|
|
return $this->assetManager; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return AbstractController|null |
133
|
|
|
*/ |
134
|
|
|
public function getController() |
135
|
|
|
{ |
136
|
|
|
return $this->controller; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.