コード例 #1
0
ファイル: Application.php プロジェクト: Artiomtb/MindKBlog
 /**
  * Функция выполняет роутинг, запуск нужного контроллера, отдает респонс
  */
 public function run()
 {
     self::$logger->debug("Running application...");
     $this->request = Request::create();
     $route_answer = $this->router->route($this->request);
     $route = $route_answer["route"];
     //если роут не найден по данному uri
     if (empty($route)) {
         self::$logger->warn("Router was not found");
         $response = new Response("Route not found", ResponseType::NOT_FOUND);
     } else {
         $controller_class = $route["controller"];
         $method_name = $route["action"] . "Action";
         if (class_exists($controller_class) && method_exists($controller_class, $method_name)) {
             $request_params = $route_answer["params"];
             $response = $this->getResponseFromController($controller_class, $method_name, $request_params);
             //                TODO добавить оборачивание респонса в шаблон
             //                if("text/html" === $response->getContentType()) {
             //                    $content = $response->getContent();
             //                    $response = new Response( include (__DIR__.'/../../src/Blog/views/500.html.php'));
             //                }
             //                $response->setContent($response->getContent())
         } else {
             self::$logger->error("Such controller and method does not exists: " . "{$controller_class} -> {$method_name}()");
             $response = new Response("Such controller and method does not exists: " . "{$controller_class} -> {$method_name}()", ResponseType::NOT_FOUND);
         }
     }
     $this->pdo->closeConnection();
     $response->send();
 }