1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DronePHP (http://www.dronephp.com) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
7
|
|
|
* @license http://www.dronephp.com/license |
8
|
|
|
* @author Darío Rivera <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Drone\Mvc; |
12
|
|
|
|
13
|
|
|
use Drone\Mvc\Exception; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* AbstractionController class |
17
|
|
|
* |
18
|
|
|
* This class manages the interaction between models and views |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractController |
21
|
|
|
{ |
22
|
|
|
use \Drone\Util\ParamTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Current module instance |
26
|
|
|
* |
27
|
|
|
* @var AbstractModule |
28
|
|
|
*/ |
29
|
|
|
private $module; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Current method |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $method = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Layout name |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $layout = "default"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Terminal mode |
47
|
|
|
* |
48
|
|
|
* @var boolean |
49
|
|
|
*/ |
50
|
|
|
private $terminal = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Indicates if the controller must show the views |
54
|
|
|
* |
55
|
|
|
* @var boolean |
56
|
|
|
*/ |
57
|
|
|
private $showView = true; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Defines method execution |
61
|
|
|
* |
62
|
|
|
* The only way to stop method execution is executing stopExecution() before execute(). |
63
|
|
|
* |
64
|
|
|
* @var boolean |
65
|
|
|
*/ |
66
|
|
|
private $allowExecution = true; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Base path |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $basePath; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the current module instance |
77
|
|
|
* |
78
|
|
|
* @return AbstractModule |
79
|
|
|
*/ |
80
|
|
|
public function getModule() |
81
|
|
|
{ |
82
|
|
|
return $this->module; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the current method |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getMethod() |
91
|
|
|
{ |
92
|
|
|
return $this->method; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the mode of visualization |
97
|
|
|
* |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
|
|
public function getTerminal() |
101
|
|
|
{ |
102
|
|
|
return $this->terminal; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the mode of viewing |
107
|
|
|
* |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
|
|
public function getShowView() |
111
|
|
|
{ |
112
|
|
|
return $this->showView; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the current layout |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getLayout() |
121
|
|
|
{ |
122
|
|
|
return $this->layout; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the base path |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getBasePath() |
131
|
|
|
{ |
132
|
|
|
return $this->basePath; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Sets the terminal mode |
137
|
|
|
* |
138
|
|
|
* @param boolean $terminal |
139
|
|
|
* |
140
|
|
|
* @return null |
141
|
|
|
*/ |
142
|
|
|
public function setTerminal($terminal = true) |
143
|
|
|
{ |
144
|
|
|
$this->terminal = $terminal; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Sets the showView parameter |
149
|
|
|
* |
150
|
|
|
* @param boolean $show |
151
|
|
|
* |
152
|
|
|
* @return null |
153
|
|
|
*/ |
154
|
|
|
public function setShowView($show = true) |
155
|
|
|
{ |
156
|
|
|
$this->showView = $show; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets layout name |
161
|
|
|
* |
162
|
|
|
* @param string $layout |
163
|
|
|
* |
164
|
|
|
* @return null |
165
|
|
|
*/ |
166
|
|
|
public function setLayout($layout) |
167
|
|
|
{ |
168
|
|
|
$this->layout = $layout; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Sets the method attribute |
173
|
|
|
* |
174
|
|
|
* @param string $method |
175
|
|
|
* |
176
|
|
|
* @return null |
177
|
|
|
*/ |
178
|
|
|
public function setMethod($method) |
179
|
|
|
{ |
180
|
|
|
$this->method = $method; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Constructor |
185
|
|
|
* |
186
|
|
|
* @param string $basePath |
187
|
|
|
* |
188
|
|
|
* @throws Exception\PageNotFoundException |
189
|
|
|
*/ |
190
|
|
|
public function __construct($basePath = null) |
191
|
|
|
{ |
192
|
|
|
$this->basePath = $basePath; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Creates the module instance |
197
|
|
|
* |
198
|
|
|
* @param string $module |
199
|
|
|
* |
200
|
|
|
* @return null |
201
|
|
|
*/ |
202
|
|
|
public function createModuleInstance($module) |
203
|
|
|
{ |
204
|
|
|
if (!is_null($module)) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
/* |
207
|
|
|
* Module class instantiation |
208
|
|
|
* |
209
|
|
|
* Each module must have a class called Module in her namesapce. This class |
210
|
|
|
* is initilized here and can change the behavior of a controller using |
211
|
|
|
* stopExecution(), setMethod() or other methods. |
212
|
|
|
*/ |
213
|
|
|
|
214
|
|
|
$fqn_module = "\\" . $module . "\\Module"; |
215
|
|
|
|
216
|
|
|
if (!class_exists($fqn_module)) |
217
|
|
|
throw new Exception\ModuleNotFoundException("The module class '$fqn_module' does not exists!"); |
218
|
|
|
|
219
|
|
|
$this->module = new $fqn_module($module, $this); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Executes the controller |
225
|
|
|
* |
226
|
|
|
* @return null |
227
|
|
|
*/ |
228
|
|
|
public function execute() |
229
|
|
|
{ |
230
|
|
|
$method = $this->method; |
231
|
|
|
|
232
|
|
|
if (is_null($method)) |
|
|
|
|
233
|
|
|
# This error is thrown because of 'setMethod' method has not been executed |
234
|
|
|
throw new \LogicException("No method has been setted to execute!"); |
235
|
|
|
|
236
|
|
|
if ($this->allowExecution) |
237
|
|
|
{ |
238
|
|
|
if (method_exists($this, $method)) |
239
|
|
|
{ |
240
|
|
|
$class = __CLASS__; |
241
|
|
|
|
242
|
|
|
$reflection = new \ReflectionMethod($this, $method); |
243
|
|
|
|
244
|
|
|
if (!$reflection->isPublic()) |
245
|
|
|
throw new Exception\PrivateMethodExecutionException("The method '$method' is not public in the control class '$class'"); |
246
|
|
|
|
247
|
|
|
# Get the returned value of the method to send to the view |
248
|
|
|
$this->params = $this->$method(); |
249
|
|
|
|
250
|
|
|
# The only way to manage views is through an AbstractModule |
251
|
|
|
if (!is_null($this->module)) |
252
|
|
|
{ |
253
|
|
|
$params = $this->getParams(); |
254
|
|
|
|
255
|
|
|
$layout_params = (count($params) && array_key_exists('::Layout', $params)) ? $params["::Layout"] : []; |
256
|
|
|
|
257
|
|
|
$layoutManager = new Layout($layout_params); |
258
|
|
|
$layoutManager->fromController($this); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
else |
262
|
|
|
{ |
263
|
|
|
$class = __CLASS__; |
264
|
|
|
throw new Exception\MethodNotFoundException("The method '$method' doesn't exists in the control class '$class'"); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Stops the execution of the specified method |
271
|
|
|
* |
272
|
|
|
* The only way to stop method execution before execute() |
273
|
|
|
* |
274
|
|
|
* @return null |
275
|
|
|
*/ |
276
|
|
|
public function stopExecution() |
277
|
|
|
{ |
278
|
|
|
$this->allowExecution = false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Checks if allowExecution is true |
283
|
|
|
* |
284
|
|
|
* @return null |
285
|
|
|
*/ |
286
|
|
|
public function executionIsAllowed() |
287
|
|
|
{ |
288
|
|
|
return $this->allowExecution; |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Returns the class name |
293
|
|
|
* |
294
|
|
|
* @return string |
295
|
|
|
*/ |
296
|
|
|
public static function getClassName() |
297
|
|
|
{ |
298
|
|
|
return __CLASS__; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Returns $_POST contents |
303
|
|
|
* |
304
|
|
|
* @return array |
305
|
|
|
*/ |
306
|
|
|
public function getPost() |
307
|
|
|
{ |
308
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) |
309
|
|
|
$_POST = json_decode(file_get_contents('php://input'), true); |
310
|
|
|
|
311
|
|
|
return (array) $_POST; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Returns json contents |
316
|
|
|
* |
317
|
|
|
* @throws LogicException |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public function getJson() |
322
|
|
|
{ |
323
|
|
|
if ($_SERVER['REQUEST_METHOD'] != 'JSON') |
324
|
|
|
throw new \LogicException("Request method is not JSON"); |
325
|
|
|
|
326
|
|
|
$input = file_get_contents('php://input'); |
327
|
|
|
$array = explode("&", $input); |
328
|
|
|
|
329
|
|
|
$result = []; |
330
|
|
|
|
331
|
|
|
foreach ($array as $value) |
332
|
|
|
{ |
333
|
|
|
$io = explode("=", $value); |
334
|
|
|
$result[$io[0]] = $io[1]; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $result; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Checks if the current request is XmlHttpRequest (AJAX) |
342
|
|
|
* |
343
|
|
|
* @return boolean |
344
|
|
|
*/ |
345
|
|
|
public function isXmlHttpRequest() |
346
|
|
|
{ |
347
|
|
|
# non standard (HTTP_X_REQUESTED_WITH is not a part of PHP) |
348
|
|
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) |
349
|
|
|
return true; |
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Checks if the current request is POST |
355
|
|
|
* |
356
|
|
|
* @return boolean |
357
|
|
|
*/ |
358
|
|
|
public function isPost() |
359
|
|
|
{ |
360
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") |
361
|
|
|
return true; |
362
|
|
|
return false; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Checks if the current request is GET |
367
|
|
|
* |
368
|
|
|
* @return boolean |
369
|
|
|
*/ |
370
|
|
|
public function isGet() |
371
|
|
|
{ |
372
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "GET") |
373
|
|
|
return true; |
374
|
|
|
return false; |
375
|
|
|
} |
376
|
|
|
} |