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