public function run() { ServiceContainer::get('security')->generateToken(); try { if (!ServiceContainer::get('security')->checkToken()) { die('Invalid token'); } $map = $this->config['routes']; ServiceContainer::set('route', new Router($map)); $match_route = ServiceContainer::get('route'); $route = $match_route->findRoute(); if (!empty($route['security'])) { $user = ServiceContainer::get('session')->get('authenticated'); if (!empty($user->user_role) != 'ROLE_USER') { $msg = 'Access denied, please login to your account!'; $fsg = ServiceContainer::get('session'); $fsg->setFlush('error', $msg); $redirect = new ResponseRedirect(ServiceContainer::get('route')->buildRoute('security_signin')); $redirect->send(); } } if (class_exists($route['controller'])) { $controller = $route['controller']; $action = $route['action']; $vars = NULL; if (!empty($route['vars'])) { $vars = $route['vars']; } $response = $this->startController($controller, $action, $vars); } else { throw new HttpNotFoundException(); } } catch (HttpNotFoundException $e) { $error_layout = $this->config['not_found']; $renderer = new Renderer($error_layout, array('message' => $e->getMessage(), 'code' => $e->getCode())); $response = new Response($renderer->render()); } $flush = ServiceContainer::get('session')->get('flush') ? ServiceContainer::get('session')->get('flush') : array(); ServiceContainer::get('session')->unsetSession('flush'); if ($response instanceof Response) { if ($response->getType() == 'html') { $view = $this->config['main_layout']; $renderer = new Renderer($view, array('content' => $response->getContent(), 'flush' => $flush)); $wrapped = $renderer->render(); $response = new Response($wrapped); $response->send(); } elseif ($response->getType() == 'json') { $response = new ResponseJson(); $response->send(); } } }