userClass = $config['userClass']; self::$app = $this; $this->request = new Request(); $this->response = new Response(); $this->router = new Router($this->request, $this->response); $this->view = new View(); $this->db = new Database($config['db']); $this->session = new Session(); $primaryValue = $this->session->get('user'); if ($primaryValue) { $userObj = new $this->userClass(); $primaryKey = $userObj->primaryKey(); $this->user = $userObj->findOne([$primaryKey => $primaryValue]); } else { $this->user = null; } } public function setController($controller) { $this->controller = $controller; } public function getController() { return $this->controller; } public function login(DbModel $user) { $this->user = $user; $primaryKey = $user->primaryKey(); $primaryValue = $user->{$primaryKey}; $this->session->set('user', $primaryValue); return true; } public function logout() { $this->user = null; $this->session->remove('user'); } public static function isLoggedIn() { return !self::$app->user; } public function run() { try{ echo $this->router->resolve(); }catch(ForbiddenException $e){ $this->response->setStatusCode(403); echo $this->view->renderView('_error', [ 'exception' => $e ]); }catch(NotFoundException $e){ $this->response->setStatusCode(404); echo $this->view->renderView('_error', [ 'exception' => $e ]); } } }