/** * @throws ServiceException * * solve exception depending from enter message (number or string) * if message is numeric get code from Response class and redirect to 500 html page * if string redirect to $redirectAddress */ public function solveException() { $data = array(); if ($this->getMessage() && is_numeric($this->getMessage())) { $data['code'] = $this->getMessage(); $data['message'] = Response::getMessageByCode($this->getMessage()); if ($this->beforeSolve) { $this->beforeSolveException(); } $renderer = new Renderer(); $responce = new Response($renderer::render(Service::get('config')->get500Layout(), $data), 'text/html', 202); $responce->send(); } else { if ($this->getMessage()) { Service::get('session')->addFlush($this->type, $this->getMessage()); if ($this->beforeSolve) { $this->beforeSolveException(); } echo $this->redirectAddress; $redirect = new ResponseRedirect($this->redirectAddress); $redirect->sendHeaders(); } else { throw new ServiceException(500); } } }
/** * 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'); } }