-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
92 lines (71 loc) · 2.13 KB
/
Copy pathApp.php
File metadata and controls
92 lines (71 loc) · 2.13 KB
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
92
<?php
namespace lexuanphat\phpmvc;
use lexuanphat\phpmvc\db\Database;
class App
{
public static string $ROOT_DIR;
public static App $app;
public string $layout = 'main';
public string $userClass;
public Router $router;
public Response $response;
public Request $request;
public Database $db;
public ?Controller $controller = null;
public Session $session;
public ?UserModel $user;
public View $view;
public function __construct($rootPath, array $config)
{
self::$ROOT_DIR = $rootPath;
self::$app = $this;
$this->request = new Request();
$this->response = new Response();
$this->router = new Router($this->request, $this->response);
$this->session = new Session();
$this->view = new View();
$this->db = new Database($config['db']);
$this->userClass = $config['userClass'];
$userClass = new $this->userClass;
$primaryKey = $userClass->primaryKey();
$primaryValue = $this->session->get("user");
if($primaryValue){
$this->user = $userClass::findOne([$primaryKey => $primaryValue]);
}else{
$this->user = null;
}
}
public function run()
{
try {
echo $this->router->resolve();
} catch (\Exception $e) {
$this->response->setStatusCode($e->getCode());
echo $this->view->renderView("_error", [
'exception' => $e,
]);
}
}
public function getController()
{
return $this->controller;
}
public function setController($controller)
{
$this->controller = $controller;
}
public function login(UserModel $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 isGuest() {
return !self::$app->user;
}
}