1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\Mvc; |
4
|
|
|
|
5
|
|
|
use Bone\Db\Adapter\MySQL; |
6
|
|
|
use Bone\Mvc\View\PlatesEngine; |
7
|
|
|
use Bone\Mvc\View\ViewEngine; |
8
|
|
|
use Bone\Server\Environment; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use stdClass; |
11
|
|
|
|
12
|
|
|
abstract class AbstractController |
13
|
|
|
{ |
14
|
|
|
/** @var Environment $serverEnvironment */ |
15
|
|
|
protected $serverEnvironment; |
16
|
|
|
|
17
|
|
|
/** @var ServerRequestInterface */ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** @var ViewEngine $plates */ |
21
|
|
|
protected $viewEngine; |
22
|
|
|
|
23
|
|
|
/** @var MySQL */ |
24
|
|
|
protected $db; |
25
|
|
|
|
26
|
|
|
/** @var string $controller */ |
27
|
|
|
protected $controller; |
28
|
|
|
|
29
|
|
|
/** @var string $action */ |
30
|
|
|
protected $action; |
31
|
|
|
|
32
|
|
|
/** @var stdClass $view */ |
33
|
|
|
public $view; |
34
|
|
|
|
35
|
|
|
/** @var string $body */ |
36
|
|
|
private $body; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $layoutEnabled; |
40
|
|
|
|
41
|
|
|
/** @var bool */ |
42
|
|
|
private $viewEnabled; |
43
|
|
|
|
44
|
|
|
/** @var array $headers */ |
45
|
|
|
private $headers; |
46
|
|
|
|
47
|
|
|
/** @var int $statusCode */ |
48
|
|
|
private $statusCode = 200; |
49
|
|
|
|
50
|
|
|
/** @var array $params */ |
51
|
|
|
public $params; |
52
|
|
|
|
53
|
|
|
/** @var array $post */ |
54
|
|
|
protected $post = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Controller constructor. |
58
|
|
|
* @param ServerRequestInterface $request |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
33 |
|
public function __construct(ServerRequestInterface $request) |
62
|
33 |
|
{ |
63
|
33 |
|
$this->request = $request; |
64
|
33 |
|
$this->headers = []; |
65
|
33 |
|
$this->params = $this->request->getQueryParams(); |
66
|
|
|
|
67
|
33 |
|
if ($this->request->getMethod() == 'POST') { |
68
|
21 |
|
$body = $this->request->getParsedBody(); |
69
|
21 |
|
$this->post = $body ?: []; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
33 |
|
$this->initViewEngine(); |
73
|
33 |
|
$this->view = new stdClass(); |
74
|
33 |
|
$this->layoutEnabled = true; |
75
|
33 |
|
$this->viewEnabled = true; |
76
|
33 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function getParams() |
79
|
1 |
|
{ |
80
|
1 |
|
return array_merge($this->params, $this->post); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $param |
85
|
|
|
* @param null $default |
86
|
|
|
* @return mixed|null|string |
87
|
|
|
*/ |
88
|
1 |
|
public function getParam($param, $default = null) |
89
|
1 |
|
{ |
90
|
1 |
|
$set = isset($this->params[$param]); |
91
|
1 |
|
if ($set && is_string($this->params[$param])) { |
92
|
1 |
|
return urldecode($this->params[$param]); |
93
|
1 |
|
} elseif ($set) { |
94
|
1 |
|
return $this->params[$param]; |
95
|
|
|
} |
96
|
1 |
|
return $default; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $key |
101
|
|
|
* @param $val |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
22 |
|
public function setParam($key, $val) |
105
|
22 |
|
{ |
106
|
22 |
|
$this->params[$key] = $val; |
107
|
22 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
1 |
|
protected function setDB() |
114
|
1 |
|
{ |
115
|
1 |
|
$config = Registry::ahoy()->get('db'); |
116
|
1 |
|
$this->db = new MySQL($config); |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
33 |
|
protected function initViewEngine() |
123
|
33 |
|
{ |
124
|
33 |
|
$viewPath = file_exists(APPLICATION_PATH.'/src/App/View/') ? APPLICATION_PATH.'/src/App/View/' : '.' ; |
125
|
33 |
|
$engine = new PlatesEngine($viewPath); |
126
|
33 |
|
$this->viewEngine = $engine; |
127
|
33 |
|
} |
128
|
|
|
|
129
|
1 |
|
public function setViewEngine(ViewEngine $engine) |
130
|
1 |
|
{ |
131
|
1 |
|
$this->viewEngine = $engine; |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return PDO |
136
|
|
|
*/ |
137
|
1 |
|
public function getDbAdapter() |
138
|
1 |
|
{ |
139
|
1 |
|
if(!$this->db) |
140
|
|
|
{ |
141
|
1 |
|
$this->setDB(); |
142
|
|
|
} |
143
|
1 |
|
return $this->db->getConnection(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* For loadin' th' cannon, so t' speak |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
6 |
|
public function getHeaders() |
152
|
6 |
|
{ |
153
|
6 |
|
return $this->headers; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
4 |
|
public function hasLayoutEnabled() |
160
|
4 |
|
{ |
161
|
4 |
|
return ($this->layoutEnabled === true); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Enables the layout |
166
|
|
|
*/ |
167
|
1 |
|
public function enableLayout() |
168
|
1 |
|
{ |
169
|
1 |
|
$this->layoutEnabled = true; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Disables the layout |
174
|
|
|
*/ |
175
|
5 |
|
public function disableLayout() |
176
|
5 |
|
{ |
177
|
5 |
|
$this->layoutEnabled = false; |
178
|
5 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
4 |
|
public function hasViewEnabled() |
184
|
4 |
|
{ |
185
|
4 |
|
return ($this->viewEnabled === true); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Enables the view |
190
|
|
|
*/ |
191
|
1 |
|
public function enableView() |
192
|
1 |
|
{ |
193
|
1 |
|
$this->viewEnabled = true; |
194
|
1 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Disables the view |
198
|
|
|
*/ |
199
|
5 |
|
public function disableView() |
200
|
5 |
|
{ |
201
|
5 |
|
$this->viewEnabled = false; |
202
|
5 |
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
6 |
|
public function getBody() |
208
|
6 |
|
{ |
209
|
6 |
|
return $this->body; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Only used if Layout & View disabled |
214
|
|
|
* |
215
|
|
|
* @param $body |
216
|
|
|
*/ |
217
|
5 |
|
public function setBody($body) |
218
|
5 |
|
{ |
219
|
5 |
|
$this->body = $body; |
220
|
5 |
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param string $key |
224
|
|
|
* @param string $value |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
3 |
|
public function setHeader($key, $value) |
228
|
3 |
|
{ |
229
|
3 |
|
$this->headers[$key] = $value; |
230
|
3 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $headers |
235
|
|
|
*/ |
236
|
2 |
|
public function setHeaders(array $headers) |
237
|
2 |
|
{ |
238
|
2 |
|
$this->headers = $headers; |
239
|
2 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param int $statusCode |
243
|
|
|
*/ |
244
|
2 |
|
public function setStatusCode($statusCode) |
245
|
2 |
|
{ |
246
|
2 |
|
$this->statusCode = $statusCode; |
247
|
2 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return int |
251
|
|
|
*/ |
252
|
4 |
|
public function getStatusCode() |
253
|
4 |
|
{ |
254
|
4 |
|
return $this->statusCode; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param $key |
260
|
|
|
* @return string|null |
261
|
|
|
*/ |
262
|
3 |
|
public function getHeader($key) |
263
|
3 |
|
{ |
264
|
3 |
|
return $this->headers[$key] ? $this->headers[$key] : null; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return ServerRequestInterface |
269
|
|
|
*/ |
270
|
1 |
|
public function getRequest() |
271
|
1 |
|
{ |
272
|
1 |
|
return $this->request; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param ServerRequestInterface $request |
277
|
|
|
* @return Controller |
278
|
|
|
*/ |
279
|
1 |
|
public function setRequest(ServerRequestInterface $request) |
280
|
1 |
|
{ |
281
|
1 |
|
$this->request = $request; |
282
|
1 |
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param array $data |
287
|
|
|
* @param int $statusCode |
288
|
|
|
*/ |
289
|
1 |
|
public function sendJsonResponse(array $data, $statusCode = 200) |
290
|
1 |
|
{ |
291
|
1 |
|
$this->disableLayout(); |
292
|
1 |
|
$this->disableView(); |
293
|
1 |
|
$this->setHeader('Cache-Control', 'no-cache, must-revalidate'); |
294
|
1 |
|
$this->setHeader('Expires','Mon, 26 Jul 1997 05:00:00 GMT'); |
295
|
1 |
|
$this->setHeader('Content-Type','application/json'); |
296
|
1 |
|
$json = json_encode($data); |
297
|
1 |
|
$this->setBody($json); |
298
|
1 |
|
$this->setStatusCode($statusCode); |
299
|
1 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param null $key |
303
|
|
|
* @param string $default |
304
|
|
|
* @return array|string|null |
305
|
|
|
*/ |
306
|
1 |
|
public function getPost($key = null, $default = null) |
307
|
1 |
|
{ |
308
|
1 |
|
if ($key) { |
309
|
1 |
|
return array_key_exists($key, $this->post) ? $this->post[$key] : $default; |
310
|
|
|
} |
311
|
|
|
|
312
|
1 |
|
return $this->post; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return Environment |
317
|
|
|
*/ |
318
|
|
|
public function getServerEnvironment(): Environment |
319
|
|
|
{ |
320
|
|
|
return $this->serverEnvironment; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param Environment $serverEnvironment |
325
|
|
|
*/ |
326
|
3 |
|
public function setServerEnvironment(Environment $serverEnvironment) |
327
|
3 |
|
{ |
328
|
3 |
|
$this->serverEnvironment = $serverEnvironment; |
329
|
3 |
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return ViewEngine |
333
|
|
|
*/ |
334
|
15 |
|
public function getViewEngine() |
335
|
15 |
|
{ |
336
|
15 |
|
return $this->viewEngine; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.