Example #1
0
 /**
  * {@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;
 }