/** * Search for the route that matches the current * request. If not found, returns NULL. * * @return array|null */ public static function getCurrentRoute() { $current = null; $request = Request::current(); $routeStr = $request->getRouteStr(); $method = $request->getMethod(); //Search for the route foreach (self::$routes as $route) { //Check the method if (!in_array($method, $route['methods'])) { continue; } //Check number of slashes if (substr_count($route['route'], '/') != substr_count($routeStr, '/')) { continue; } //Checks the route string if (ParametrizedString::make($route['route'])->matches($routeStr)) { $current = $route; break; } } return $current; }
/** * {@inheritdoc} */ public function run(...$args) { //TODO: turn code into parts (methods) self::$current = $this; //Load application definitions. $this->loadRoutes(); $this->registerRequestParsers(); $this->registerProviders(); //Call controller action. $this->request = Request::current(); $route = Map::getCurrentRoute(); if ($route) { $routeStr = ParametrizedString::make($route['route']); $params = array_replace($routeStr->getValues($this->request->getRouteStr()), $this->request->getQueryParams()); $controller = $this->container->get($route['controller']); //Check controller if (!$controller instanceof Controller) { throw new \Exception("Invalid controller: {$route['controller']}"); } /* * Call controller and process midware list */ $middlewareList = $this->buildMiddlewareQueue([$route['middlewareList'], $controller->getMiddlewareList()]); $application = $this; $fn = function () use($controller, &$params, &$route, $application) { $response = $application->getContainer()->invoke($controller, $route['action'], $params); return ResponseFactory::createResponse($response); }; $response = $this->proccessMiddlewareQueue($middlewareList, $fn); $response->send(); } self::$current = null; }