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
|
|
|
* Returns the current module instance |
70
|
|
|
* |
71
|
|
|
* @return AbstractModule |
72
|
|
|
*/ |
73
|
|
|
public function getModule() |
74
|
|
|
{ |
75
|
|
|
return $this->module; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the current method |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getMethod() |
84
|
|
|
{ |
85
|
|
|
return $this->method; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the mode of visualization |
90
|
|
|
* |
91
|
|
|
* @return boolean |
92
|
|
|
*/ |
93
|
|
|
public function getTerminal() |
94
|
|
|
{ |
95
|
|
|
return $this->terminal; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns the mode of viewing |
100
|
|
|
* |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
public function getShowView() |
104
|
|
|
{ |
105
|
|
|
return $this->showView; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the current layout |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getLayout() |
114
|
|
|
{ |
115
|
|
|
return $this->layout; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the terminal mode |
120
|
|
|
* |
121
|
|
|
* @param boolean $terminal |
122
|
|
|
* |
123
|
|
|
* @return null |
124
|
|
|
*/ |
125
|
|
|
public function setTerminal($terminal = true) |
126
|
|
|
{ |
127
|
|
|
$this->terminal = $terminal; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sets the showView parameter |
132
|
|
|
* |
133
|
|
|
* @param boolean $show |
134
|
|
|
* |
135
|
|
|
* @return null |
136
|
|
|
*/ |
137
|
|
|
public function setShowView($show = true) |
138
|
|
|
{ |
139
|
|
|
$this->showView = $show; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets layout name |
144
|
|
|
* |
145
|
|
|
* @param string $layout |
146
|
|
|
* |
147
|
|
|
* @return null |
148
|
|
|
*/ |
149
|
|
|
public function setLayout($layout) |
150
|
|
|
{ |
151
|
|
|
$this->layout = $layout; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Sets the method attribute |
156
|
|
|
* |
157
|
|
|
* @param string $method |
158
|
|
|
* |
159
|
|
|
* @return null |
160
|
|
|
*/ |
161
|
|
|
public function setMethod($method) |
162
|
|
|
{ |
163
|
|
|
$this->method = $method; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Creates the module instance |
168
|
|
|
* |
169
|
|
|
* @param string $module |
170
|
|
|
* @param Router $router |
171
|
|
|
* |
172
|
|
|
* @return null |
173
|
|
|
*/ |
174
|
|
|
public function createModuleInstance($module, Router $router) |
175
|
|
|
{ |
176
|
|
|
if (!is_null($module)) |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
/* |
179
|
|
|
* Module class instantiation |
180
|
|
|
* |
181
|
|
|
* Each module must have a class called Module in her namesapce. This class |
182
|
|
|
* is initilized here and can change the behavior of a controller using |
183
|
|
|
* stopExecution(), setMethod() or other methods. |
184
|
|
|
*/ |
185
|
|
|
|
186
|
|
|
$fqn_module = "\\" . $module . "\\Module"; |
187
|
|
|
|
188
|
|
|
if (!class_exists($fqn_module)) |
189
|
|
|
throw new Exception\ModuleNotFoundException("The module class '$fqn_module' does not exists!"); |
190
|
|
|
|
191
|
|
|
$this->module = new $fqn_module($module, $this, $router); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Executes the controller |
197
|
|
|
* |
198
|
|
|
* @return null |
199
|
|
|
*/ |
200
|
|
|
public function execute() |
201
|
|
|
{ |
202
|
|
|
$method = $this->method; |
203
|
|
|
|
204
|
|
|
if (is_null($method)) |
|
|
|
|
205
|
|
|
# This error is thrown because of 'setMethod' method has not been executed |
206
|
|
|
throw new \LogicException("No method has been setted to execute!"); |
207
|
|
|
|
208
|
|
|
if ($this->allowExecution) |
209
|
|
|
{ |
210
|
|
|
if (method_exists($this, $method)) |
211
|
|
|
{ |
212
|
|
|
$class = __CLASS__; |
213
|
|
|
|
214
|
|
|
$reflection = new \ReflectionMethod($this, $method); |
215
|
|
|
|
216
|
|
|
if (!$reflection->isPublic()) |
217
|
|
|
throw new Exception\PrivateMethodExecutionException("The method '$method' is not public in the control class '$class'"); |
218
|
|
|
|
219
|
|
|
# Get the returned value of the method to send to the view |
220
|
|
|
$this->params = $this->$method(); |
221
|
|
|
|
222
|
|
|
# The only way to manage views is through an AbstractModule |
223
|
|
|
if (!is_null($this->module)) |
224
|
|
|
{ |
225
|
|
|
$params = $this->getParams(); |
226
|
|
|
|
227
|
|
|
$layout_params = (count($params) && array_key_exists('::Layout', $params)) ? $params["::Layout"] : []; |
228
|
|
|
|
229
|
|
|
$layoutManager = new Layout($layout_params); |
230
|
|
|
$layoutManager->fromController($this); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
else |
234
|
|
|
{ |
235
|
|
|
$class = __CLASS__; |
236
|
|
|
throw new Exception\MethodNotFoundException("The method '$method' doesn't exists in the control class '$class'"); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Stops the execution of the specified method |
243
|
|
|
* |
244
|
|
|
* The only way to stop method execution before execute() |
245
|
|
|
* |
246
|
|
|
* @return null |
247
|
|
|
*/ |
248
|
|
|
public function stopExecution() |
249
|
|
|
{ |
250
|
|
|
$this->allowExecution = false; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Checks if allowExecution is true |
255
|
|
|
* |
256
|
|
|
* @return null |
257
|
|
|
*/ |
258
|
|
|
public function executionIsAllowed() |
259
|
|
|
{ |
260
|
|
|
return $this->allowExecution; |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Returns the class name |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public static function getClassName() |
269
|
|
|
{ |
270
|
|
|
return __CLASS__; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Returns $_POST contents |
275
|
|
|
* |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
|
|
public function getPost() |
279
|
|
|
{ |
280
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) |
281
|
|
|
$_POST = json_decode(file_get_contents('php://input'), true); |
282
|
|
|
|
283
|
|
|
return (array) $_POST; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Returns json contents |
288
|
|
|
* |
289
|
|
|
* @throws LogicException |
290
|
|
|
* |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
|
public function getJson() |
294
|
|
|
{ |
295
|
|
|
if ($_SERVER['REQUEST_METHOD'] != 'JSON') |
296
|
|
|
throw new \LogicException("Request method is not JSON"); |
297
|
|
|
|
298
|
|
|
$input = file_get_contents('php://input'); |
299
|
|
|
$array = explode("&", $input); |
300
|
|
|
|
301
|
|
|
$result = []; |
302
|
|
|
|
303
|
|
|
foreach ($array as $value) |
304
|
|
|
{ |
305
|
|
|
$io = explode("=", $value); |
306
|
|
|
$result[$io[0]] = $io[1]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $result; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Checks if the current request is XmlHttpRequest (AJAX) |
314
|
|
|
* |
315
|
|
|
* @return boolean |
316
|
|
|
*/ |
317
|
|
|
public function isXmlHttpRequest() |
318
|
|
|
{ |
319
|
|
|
# non standard (HTTP_X_REQUESTED_WITH is not a part of PHP) |
320
|
|
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) |
321
|
|
|
return true; |
322
|
|
|
return false; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Checks if the current request is POST |
327
|
|
|
* |
328
|
|
|
* @return boolean |
329
|
|
|
*/ |
330
|
|
|
public function isPost() |
331
|
|
|
{ |
332
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") |
333
|
|
|
return true; |
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Checks if the current request is GET |
339
|
|
|
* |
340
|
|
|
* @return boolean |
341
|
|
|
*/ |
342
|
|
|
public function isGet() |
343
|
|
|
{ |
344
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "GET") |
345
|
|
|
return true; |
346
|
|
|
return false; |
347
|
|
|
} |
348
|
|
|
} |