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