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