-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
136 lines (111 loc) · 3.53 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/** User: Sabo */
namespace sabosuke\bit_mvc_core;
use sabosuke\bit_mvc_core\Router;
use sabosuke\bit_mvc_core\Request;
use sabosuke\bit_mvc_core\Response;
use sabosuke\bit_mvc_core\Controller;
use sabosuke\bit_mvc_core\db\DbModel;
use sabosuke\bit_mvc_core\db\Database;
use sabosuke\bit_mvc_core\View;
use \sabosuke\bit_mvc_core\theme\ThemeModel;
use \sabosuke\bit_mvc_core\query_builder\QueryBuilder;
/**
* Class Application
*
* @author Essam Abed <abedissam95@gmail.com>
* @package sabosuke\bit_mvc_core
*/
class Application{
public const EVENT_BEFORE_REQUEST = 'beforeRequest';
public const EVENT_AFTER_REQUEST = 'afterRequest';
protected array $eventListeners = [];
public string $userClass;
public string $layout = 'main';
public static string $ROOT_DIRECTORY;
public static Application $app;
public Router $router;
public Request $request;
public Response $response;
public ?Controller $controller = null;
public Session $session;
public Database $db;
public ?UserModel $user;
public View $view;
public QueryBuilder $qb;
public $query;
/**
* Application constructor
*
* @param $ROOT_DIRECTORY
*/
public function __construct($rootPath, array $config){
self::$ROOT_DIRECTORY = $rootPath;
self::$app = $this;
$this->request = new Request();
$this->response = new Response();
$this->session = new Session();
$this->router = new Router($this->request, $this->response);
$this->view = new View();
$this->qb = new QueryBuilder();
$this->db = new Database($config['db']);
if ($config['userClass']?? false){
$this->userClass = $config['userClass'];
$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 InitTheme(){
return new ThemeModel();
}
public function run(){
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);
try{
echo $this->router->resolve();
}catch(\Exception $e){
$this->response->setStatusCode($e->getCode());
echo $this->view->renderView('_exception', [
'exception' => $e
]);
}
}
public function triggerEvent($eventName){
$callbacks = $this->eventListeners[$eventName] ?? [];
foreach($callbacks as $callback){
call_user_func($callback);
}
}
public function on($eventName, $callback){
$this->eventListeners[$eventName][] = $callback;
}
/**
* @param \sabosuke\bit_mvc_core\Controller $controller
*/
public function getController(){
return $this->controller;
}
/**
* @param \sabosuke\bit_mvc_core\Controller $controller
*/
public function setController(Controller $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;
}
}