injectableFactory = $injectableFactory; $this->applicationUser = $applicationUser; } public function run() { $slim = SlimAppFactory::create(); $slim->setBasePath(Route::detectBasePath()); $slim->addRoutingMiddleware(); $routeList = $this->getRouteList(); $routeList = $this->filterRouteList($routeList); foreach ($routeList as $item) { $this->addRoute($slim, $item); } $slim->addErrorMiddleware(false, true, true); $slim->run(); } protected function addRoute(SlimApp $slim, array $item) { $method = strtolower($item['method']); $route = $item['route']; $slim->$method( $route, function (Psr7Request $request, Psr7Response $response, array $args) use ($item, $slim) { $requestWrapped = new RequestWrapper($request, $slim->getBasePath()); $responseWrapped = new ResponseWrapper($response); $this->processRequest($item, $requestWrapped, $responseWrapped, $args); return $responseWrapped->getResponse(); } ); } protected function processRequest(array $item, RequestWrapper $requestWrapped, ResponseWrapper $responseWrapped, array $args) { try { $authRequired = !($item['noAuth'] ?? false); $authentication = $this->injectableFactory->create(Authentication::class); $apiAuth = ApiAuth::getBuilder() ->setAuthentication($authentication) ->setAuthRequired($authRequired) ->build(); $apiAuth->process($requestWrapped, $responseWrapped); if (!$apiAuth->isResolved()) { return; } if ($apiAuth->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } $routeProcessor = $this->injectableFactory->create(RouteProcessor::class); $routeProcessor->process($item['route'], $item['params'], $requestWrapped, $responseWrapped, $args); } catch (Exception $exception) { (new ApiErrorOutput($requestWrapped))->process( $responseWrapped, $exception, false, $item, $args ); } } protected function filterRouteList(array $routeList) : array { $routeList = array_filter($routeList, function ($item) { $method = strtolower($item['method'] ?? ''); $route = $item['route'] ?? null; if (!$route) { return false; } if (!in_array($method, $this->allowedMethodList)) { $GLOBALS['log']->warning("Route: Method '{$method}' is not supported. Fix the route '{$route}'."); return false; } return true; }); return $routeList; } protected function getRouteList() : array { return $this->injectableFactory->create(Route::class)->getFullList(); } }