facut pana la video 7
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
use app\core\exceptions\ForbiddenException;
|
||||
use app\core\exceptions\NotFoundException;
|
||||
|
||||
class Application
|
||||
{
|
||||
public static string $ROOT_DIR;
|
||||
public string $userClass;
|
||||
public string $layout = 'main';
|
||||
|
||||
public Router $router;
|
||||
public Request $request;
|
||||
public Response $response;
|
||||
public static Application $app;
|
||||
public ?Controller $controller = null;
|
||||
public Database $db;
|
||||
public Session $session;
|
||||
public ?DbModel $user;
|
||||
|
||||
public function __construct($rootPath, array $config)
|
||||
{
|
||||
self::$ROOT_DIR = $rootPath;
|
||||
$this->userClass = $config['userClass'];
|
||||
self::$app = $this;
|
||||
|
||||
$this->request = new Request();
|
||||
$this->response = new Response();
|
||||
$this->router = new Router($this->request, $this->response);
|
||||
|
||||
$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->router->renderView('_error', [
|
||||
'exception' => $e
|
||||
]);
|
||||
}catch(NotFoundException $e){
|
||||
$this->response->setStatusCode(404);
|
||||
echo $this->router->renderView('_error', [
|
||||
'exception' => $e
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user