/** * Create Router class and call function that * process URL address * * Check using reflection presence of necessary class * of controller and his methods * * Create Response class */ public function run() { $response = null; $router = new Router(include '../app/config/routes.php'); $route = $router->parseUrl(trim(strip_tags($_SERVER['REQUEST_URI']))); try { if (!empty($route)) { $controllerReflection = new \ReflectionClass($route['controller']); $action = $route['action'] . 'Action'; if ($controllerReflection->hasMethod($action)) { // Control user role if ($action != 'indexAction' && $action != 'loginAction' && $action != 'signinAction') { if ($_SESSION['role'] != 'ROLE_USER') { throw new AuthRequredException('You must login'); } } $controller = $controllerReflection->newInstance(); if ($controller instanceof Controller) { $actionReflection = $controllerReflection->getMethod($action); ActiveRecord::getDBCon(); $response = $actionReflection->invokeArgs($controller, $route['params']); if ($response instanceof Response) { $response->send(); // Close database connection call_user_func(Service::get('event')->trigger('db_close')); } else { throw new BadResponseTypeException('Missing type of response'); } } else { throw new BadControllerTypeException('Missing type of controller'); } } } else { throw new HttpNotFoundException('Route not found'); } } catch (HttpNotFoundException $e) { // Render 404 or just show msg $error = $e->getMessage(); include Service::get('config')->get('error_404'); } catch (AuthRequredException $e) { // Reroute to login page Service::get('session')->addFlush('error', $e->getMessage()); $response = new ResponseRedirect("/login"); $response->sendHeaders(); } catch (InvalidArgumentException $e) { echo $e->getMessage(); } catch (BadResponseTypeException $e) { echo $e->getMessage(); } catch (BadPathTypeException $e) { echo $e->getMessage(); } catch (\PDOException $e) { echo $e->getMessage(); } catch (DatabaseException $e) { echo $e->getMessage(); } catch (InvalidTypeException $e) { echo $e->getMessage(); } catch (BadControllerTypeException $e) { echo $e->getMessage(); } catch (\Exception $e) { // Do 500 layout... include Service::get('config')->get('error_500'); } }