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