-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
91 lines (80 loc) · 2.21 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace assaad\core;
use assaad\core\db\Database;
use assaad\core\db\DbModel;
class Application
{
public string $userClass;
public static string $ROOTDIR;
public Router $route;
public Response $response;
public Request $request;
public Database $db;
public Session $session;
public ?DbModel $user;
public View $view;
public static $assaad;
public ?Controller $controller = null;
public string $layout = 'main';
public function __construct($rootPath,$config)
{
$this->userClass = $config['userClass'];
self::$ROOTDIR = $rootPath;
self::$assaad = $this;
$this->request = new Request();
$this->response = new Response();
$this->view = new View();
$this->session = new Session();
$this->route = new Router($this->request,$this->response);
$this->db = new Database($config['db']);
$primaryValue = $this->session->get('user');
if($primaryValue){
$primaryKey = $this->userClass::primaryKey();
$this->user = $this->userClass::findOne([$primaryKey => $primaryValue]);
}else{
$this->user = null;
}
}
public static function isGuest() : bool
{
return !self::$assaad->user;
}
public function run()
{
try {
echo $this->route->resolve();
}catch (\Exception $e){
$this->response->setStatusCode($e->getCode());
echo $this->view->renderView('_error',[
'exception'=>$e
]);
}
}
/**
* @return Controller
*/
public function getController(): Controller
{
return $this->controller;
}
/**
* @param Controller $controller
*/
public function setController(Controller $controller): void
{
$this->controller = $controller;
}
public function login(DbModel $user) : bool
{
$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');
}
}