|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tebe\Pvc\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Tebe\Pvc\Exception\SystemException; |
|
9
|
|
|
use Tebe\Pvc\View\View; |
|
10
|
|
|
|
|
11
|
|
|
abstract class BaseController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var View |
|
15
|
|
|
*/ |
|
16
|
|
|
private $view; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ServerRequestInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $request; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $controllerName; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $actionName; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Controller constructor. |
|
35
|
|
|
* @param View $view |
|
36
|
|
|
* @param ServerRequestInterface $request |
|
37
|
|
|
* @param string $pathInfo |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(View $view, ServerRequestInterface $request, string $pathInfo) |
|
40
|
|
|
{ |
|
41
|
|
|
// TODO think of why not getting $pathInfo from $request? |
|
42
|
|
|
[$controllerName, $actionName] = explode('/', $pathInfo); |
|
43
|
|
|
$this->setView($view); |
|
44
|
|
|
$this->setRequest($request); |
|
45
|
|
|
$this->setControllerName($controllerName); |
|
46
|
|
|
$this->setActionName($actionName); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return View |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getView(): View |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->view; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param View $view |
|
59
|
|
|
*/ |
|
60
|
|
|
private function setView(View $view) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->view = $view; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return ServerRequestInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getRequest(): ServerRequestInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->request; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param ServerRequestInterface $request |
|
75
|
|
|
*/ |
|
76
|
|
|
private function setRequest(ServerRequestInterface $request) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->request = $request; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $viewName |
|
83
|
|
|
* @param array $params |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @throws SystemException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function render(string $viewName, array $params = []): string |
|
88
|
|
|
{ |
|
89
|
|
|
$viewRoute = $this->resolveViewPath($viewName); |
|
90
|
|
|
$content = $this->renderPartial($viewRoute, $params); |
|
91
|
|
|
$html = $this->renderPartial('layouts/default', ['content' => $content]); |
|
92
|
|
|
return $html; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $viewName |
|
97
|
|
|
* @param array $params |
|
98
|
|
|
* @return string |
|
99
|
|
|
* @throws SystemException |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function renderPartial(string $viewName, array $params = []): string |
|
102
|
|
|
{ |
|
103
|
|
|
$viewRoute = $this->resolveViewPath($viewName); |
|
104
|
|
|
return $this->view->render($viewRoute, $params); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $viewName |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
private function resolveViewPath(string $viewName): string |
|
112
|
|
|
{ |
|
113
|
|
|
$viewRoute = $viewName; |
|
114
|
|
|
if (strpos($viewName, '/') === false) { |
|
115
|
|
|
$viewRoute = sprintf('%s/%s', $this->controllerName, $viewName); |
|
116
|
|
|
} |
|
117
|
|
|
return $viewRoute; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getControllerName(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->controllerName; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $controllerName |
|
130
|
|
|
*/ |
|
131
|
|
|
private function setControllerName(string $controllerName) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->controllerName = $controllerName; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getActionName(): string |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->actionName; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $actionName |
|
146
|
|
|
*/ |
|
147
|
|
|
private function setActionName(string $actionName) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->actionName = $actionName; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getActionMethod(): string |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->actionName . 'Action'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getRoute(): string |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->controllerName . '/' . $this->actionName; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
public function httpMethods() |
|
172
|
|
|
{ |
|
173
|
|
|
return []; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getAllowedHttpMethods() |
|
180
|
|
|
{ |
|
181
|
|
|
$actionName = $this->getActionName(); |
|
182
|
|
|
$httpMethods = $this->httpMethods(); |
|
183
|
|
|
if (empty($httpMethods[$actionName])) { |
|
184
|
|
|
return ['GET']; |
|
185
|
|
|
} |
|
186
|
|
|
if (!empty($httpMethods[$actionName])) { |
|
187
|
|
|
return array_map('strtoupper', $httpMethods[$actionName]); |
|
188
|
|
|
} |
|
189
|
|
|
return []; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|