1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ================================== |
4
|
|
|
* Responsible PHP API |
5
|
|
|
* ================================== |
6
|
|
|
* |
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
8
|
|
|
* |
9
|
|
|
* @api Responible API |
10
|
|
|
* @package responsible\core\route |
11
|
|
|
* |
12
|
|
|
* @author Vince scarpa <[email protected]> |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
namespace responsible\core\route; |
16
|
|
|
|
17
|
|
|
use responsible\core\exception; |
18
|
|
|
use responsible\core\route; |
19
|
|
|
use responsible\core\server; |
20
|
|
|
use responsible\core\encoder; |
21
|
|
|
|
22
|
|
|
class router extends server |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* [$root Responsible API root path] |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $root; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* [$scope Router scope] |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $scope = 'private'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* [$routes] |
38
|
|
|
* @var object |
39
|
|
|
*/ |
40
|
|
|
private $routes; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* [$requestBody] |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $requestBody = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* [run Try run the request method] |
50
|
|
|
* @return array|object |
51
|
|
|
*/ |
52
|
|
|
public function run() |
53
|
|
|
{ |
54
|
|
|
$controllerSettings = $this->getController(); |
55
|
|
|
$controller = $controllerSettings->model['namespace']; |
56
|
|
|
|
57
|
|
|
if (class_exists($controller)) { |
58
|
|
|
$controller = new $controller; |
59
|
|
|
|
60
|
|
|
$neededMethods = [ |
61
|
|
|
'headerMethods', |
62
|
|
|
'settings', |
63
|
|
|
'run', |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
foreach ($neededMethods as $method) { |
67
|
|
|
if (!method_exists($controller, $method)) { |
68
|
|
|
(new exception\errorException) |
69
|
|
|
->message( |
70
|
|
|
"There's a method missing in '" |
71
|
|
|
. $controllerSettings->model['class'] |
72
|
|
|
. "' {$method}() must be declared." |
73
|
|
|
) |
74
|
|
|
->error('NOT_EXTENDED'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$controller->headerMethods(); |
79
|
|
|
$controller->settings((array) $controllerSettings); |
80
|
|
|
$controller->responsible = new \stdClass; |
81
|
|
|
$controller->responsible = $this->getRoutes(); |
82
|
|
|
$response = $controller->run(); |
83
|
|
|
|
84
|
|
|
return $response; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* [setPostBody Set the post body payload] |
90
|
|
|
* @param array $payload |
91
|
|
|
*/ |
92
|
|
|
public function setPostBody($payload) |
93
|
|
|
{ |
94
|
|
|
$payloadPost = [ |
95
|
|
|
'post' => $payload |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
if( isset($this->requestBody['payload']) ) { |
99
|
|
|
array_merge($this->requestBody['payload'], $payloadPost); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->requestBody = $payloadPost; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* [setRequestBody Set the request body payload] |
108
|
|
|
* @param string $payload |
109
|
|
|
*/ |
110
|
|
|
public function setRequestBody($payload) |
111
|
|
|
{ |
112
|
|
|
$payload = ltrim($payload, 'payload='); |
113
|
|
|
$cipher = new encoder\cipher; |
114
|
|
|
$this->requestBody = $cipher->jsonDecode($cipher->decode($payload)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* [getRequestBody Get the request body defaults to empty array] |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function getRequestBody() |
122
|
|
|
{ |
123
|
|
|
if( isset($this->requestBody['payload']) ) { |
124
|
|
|
return $this->requestBody['payload']; |
125
|
|
|
} |
126
|
|
|
return $this->requestBody; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* [baseRoute] |
131
|
|
|
* @param string $directory |
132
|
|
|
*/ |
133
|
|
|
public function baseApiRoot($directory) |
134
|
|
|
{ |
135
|
|
|
$this->root = $directory; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* [route] |
140
|
|
|
* @return object |
141
|
|
|
*/ |
142
|
|
|
public function route($customRoute = '') |
143
|
|
|
{ |
144
|
|
|
$base = new route\base; |
145
|
|
|
|
146
|
|
|
$base_url = $base->url(); |
147
|
|
|
$base_uri = (!empty($customRoute)) ? $customRoute : $base->uri(); |
148
|
|
|
$base_uri = substr($base_uri, 0, 1) !== '/' ? '/' . $base_uri : $base_uri; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get the routes exit if any errors |
152
|
|
|
*/ |
153
|
|
|
$routes = $this->getTierList($base_uri); |
154
|
|
|
|
155
|
|
|
$routesArray = array( |
156
|
|
|
'base' => array( |
157
|
|
|
'protocol' => $base->protocol(), |
158
|
|
|
'path' => $base->basepath(), |
159
|
|
|
'root' => $this->root, |
160
|
|
|
'url' => $base_url, |
161
|
|
|
), |
162
|
|
|
'url' => array( |
163
|
|
|
'full' => $base_url . $base->basepath() . $base_uri, |
164
|
|
|
'path' => $base_uri, |
165
|
|
|
'query' => $this->queryString(), |
166
|
|
|
), |
167
|
|
|
'route' => array( |
168
|
|
|
'api' => $routes[0], |
169
|
|
|
'tiers' => $routes, |
170
|
|
|
'size' => sizeof($routes), |
171
|
|
|
'scope' => $this->scope, |
172
|
|
|
), |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$this->setRoutes((object)$routesArray); |
176
|
|
|
|
177
|
|
|
return $this->getRoutes(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* [getTierList] |
182
|
|
|
* @return array |
183
|
|
|
*/ |
184
|
|
|
private function getTierList($base_uri) |
185
|
|
|
{ |
186
|
|
|
$routes = explode('/', $base_uri); |
187
|
|
|
$routes = array_values(array_filter($routes)); |
188
|
|
|
|
189
|
|
|
if (empty($routes)) { |
190
|
|
|
(new exception\errorException)->error('NOT_FOUND'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
foreach ($routes as $r => $route) { |
194
|
|
|
$routes[$r] = trim($route); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $routes; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* [systemAccess Is system access allowed] |
202
|
|
|
* @return boolean |
203
|
|
|
*/ |
204
|
|
|
public function systemAccess($account) |
205
|
|
|
{ |
206
|
|
|
if (empty($account) || !isset($account->uid)) { |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ($account->uid > 0 && $this->getScope() == 'system') { |
211
|
|
|
return false; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* [queryString] |
219
|
|
|
* @return string|null |
220
|
|
|
*/ |
221
|
|
|
public function queryString() |
222
|
|
|
{ |
223
|
|
|
if (isset($_GET) && !empty($_GET)) { |
224
|
|
|
return http_build_query($_GET); |
225
|
|
|
} |
226
|
|
|
return; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* [setScope Set the router scope] |
231
|
|
|
* @param string $scope |
232
|
|
|
*/ |
233
|
|
|
public function setScope($scope) |
234
|
|
|
{ |
235
|
|
|
$this->getRoutes()->route['scope'] = $scope; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* [getScope Get the router scope] |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
public function getScope() |
243
|
|
|
{ |
244
|
|
|
return $this->getRoutes()->route['scope']; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* [getApi Name of the API request] |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function getApi() |
252
|
|
|
{ |
253
|
|
|
return $this->getRoutes()->route['api']; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* [getController Controller build settings] |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getController() |
261
|
|
|
{ |
262
|
|
|
if (!isset($this->getRoutes()->endpoint)) { |
263
|
|
|
(new exception\errorException)->error('NOT_FOUND'); |
264
|
|
|
} |
265
|
|
|
return $this->getRoutes()->endpoint; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* [getSize Size of the tier request] |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
public function getSize() |
273
|
|
|
{ |
274
|
|
|
return $this->getRoutes()->route['size']; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* [getPath URi Path request] |
279
|
|
|
* @return string |
280
|
|
|
*/ |
281
|
|
|
public function getPath() |
282
|
|
|
{ |
283
|
|
|
return $this->getRoutes()->url['path']; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* [getIssuer Get the domain issuer] |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function getIssuer($protocol = false) |
291
|
|
|
{ |
292
|
|
|
if (!isset($this->getRoutes()->base)) { |
293
|
|
|
$base = new route\base; |
294
|
|
|
return $base->url(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if (!$protocol) { |
298
|
|
|
return str_replace( |
299
|
|
|
array( |
300
|
|
|
$this->getRoutes()->base['protocol'], |
301
|
|
|
'://', |
302
|
|
|
), |
303
|
|
|
'', |
304
|
|
|
$this->getRoutes()->base['url'] |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $this->getRoutes()->base['url']; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* [setRoutes Set the routers object] |
313
|
|
|
* @param object $routes |
314
|
|
|
*/ |
315
|
|
|
public function setRoutes($routes) |
316
|
|
|
{ |
317
|
|
|
$this->routes = new \stdClass; |
318
|
|
|
$this->routes = $routes; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* [getRoutes Get the routers object] |
323
|
|
|
* @return object |
324
|
|
|
*/ |
325
|
|
|
public function getRoutes() |
326
|
|
|
{ |
327
|
|
|
return $this->routes; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|