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
|
|
|
/** |
23
|
|
|
* Current module instance |
24
|
|
|
* |
25
|
|
|
* @var AbstractModule |
26
|
|
|
*/ |
27
|
|
|
private $module; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Current method |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $method = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the current module instance |
38
|
|
|
* |
39
|
|
|
* @return AbstractModule |
40
|
|
|
*/ |
41
|
|
|
public function getModule() |
42
|
|
|
{ |
43
|
|
|
return $this->module; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns the current method |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getMethod() |
52
|
|
|
{ |
53
|
|
|
return $this->method; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets module instance |
58
|
|
|
* |
59
|
|
|
* @param AbstractModule $module |
60
|
|
|
* |
61
|
|
|
* @return null |
62
|
|
|
*/ |
63
|
|
|
public function setModule(AbstractModule $module) |
64
|
|
|
{ |
65
|
|
|
$this->module = $module; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Sets the method attribute |
70
|
|
|
* |
71
|
|
|
* @param string $method |
72
|
|
|
* |
73
|
|
|
* @return null |
74
|
|
|
*/ |
75
|
|
|
public function setMethod($method) |
76
|
|
|
{ |
77
|
|
|
$this->method = $method; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Executes the controller |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function execute() |
86
|
|
|
{ |
87
|
|
|
$method = $this->method; |
88
|
|
|
|
89
|
|
|
if (is_null($method)) |
|
|
|
|
90
|
|
|
# This error is thrown because of 'setMethod' method has not been executed |
91
|
|
|
throw new \LogicException("No method has been setted to execute!"); |
92
|
|
|
|
93
|
|
|
if (!is_null($this->module) && !$this->module->executionIsAllowed()) |
|
|
|
|
94
|
|
|
throw new Exception\MethodExecutionNotAllowedException("Method execution is not allowed"); |
95
|
|
|
else |
96
|
|
|
{ |
97
|
|
|
if (method_exists($this, $method)) |
98
|
|
|
{ |
99
|
|
|
$class = __CLASS__; |
100
|
|
|
|
101
|
|
|
$reflection = new \ReflectionMethod($this, $method); |
102
|
|
|
|
103
|
|
|
if (!$reflection->isPublic()) |
104
|
|
|
throw new Exception\PrivateMethodExecutionException("The method '$method' is not public in the control class '$class'"); |
105
|
|
|
|
106
|
|
|
return $this->$method(); |
107
|
|
|
} |
108
|
|
|
else |
109
|
|
|
{ |
110
|
|
|
$class = __CLASS__; |
111
|
|
|
throw new Exception\MethodNotFoundException("The method '$method' doesn't exists in the control class '$class'"); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the class name |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public static function getClassName() |
122
|
|
|
{ |
123
|
|
|
return __CLASS__; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns $_POST contents |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getPost() |
132
|
|
|
{ |
133
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST)) |
134
|
|
|
$_POST = json_decode(file_get_contents('php://input'), true); |
135
|
|
|
|
136
|
|
|
return (array) $_POST; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns json contents |
141
|
|
|
* |
142
|
|
|
* @throws LogicException |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function getJson() |
147
|
|
|
{ |
148
|
|
|
if ($_SERVER['REQUEST_METHOD'] != 'JSON') |
149
|
|
|
throw new \LogicException("Request method is not JSON"); |
150
|
|
|
|
151
|
|
|
$input = file_get_contents('php://input'); |
152
|
|
|
$array = explode("&", $input); |
153
|
|
|
|
154
|
|
|
$result = []; |
155
|
|
|
|
156
|
|
|
foreach ($array as $value) |
157
|
|
|
{ |
158
|
|
|
$io = explode("=", $value); |
159
|
|
|
$result[$io[0]] = $io[1]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Checks if the current request is XmlHttpRequest (AJAX) |
167
|
|
|
* |
168
|
|
|
* @return boolean |
169
|
|
|
*/ |
170
|
|
|
public function isXmlHttpRequest() |
171
|
|
|
{ |
172
|
|
|
# non standard (HTTP_X_REQUESTED_WITH is not a part of PHP) |
173
|
|
|
if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) |
174
|
|
|
return true; |
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Checks if the current request is POST |
180
|
|
|
* |
181
|
|
|
* @return boolean |
182
|
|
|
*/ |
183
|
|
|
public function isPost() |
184
|
|
|
{ |
185
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") |
186
|
|
|
return true; |
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Checks if the current request is GET |
192
|
|
|
* |
193
|
|
|
* @return boolean |
194
|
|
|
*/ |
195
|
|
|
public function isGet() |
196
|
|
|
{ |
197
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "GET") |
198
|
|
|
return true; |
199
|
|
|
return false; |
200
|
|
|
} |
201
|
|
|
} |