-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.php
156 lines (133 loc) · 3.94 KB
/
App.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Okami\Core;
use Exception;
use LogicException;
use Okami\Core\DB\Database;
use Okami\Core\Interfaces\ExecutableInterface;
use Okami\Core\Routing\Router;
use Okami\Core\Traits\WithMiddlewaresTrait;
/**
* Class App
*
* @author Michal Tuček <michaltk1@gmail.com>
* @package Okami\Core
*/
class App
{
use WithMiddlewaresTrait;
public static string $ROOT_DIR;
private bool $debug;
public string $layout = 'main';
public string $userClass;
public Router $router;
public Request $request;
public Response $response;
public Session $session;
public Database $db;
public View $view;
public ?UserModel $user = null;
public static App $app;
public ?Controller $controller = null;
private array $callstack = [];
public function __construct(string $rootPath, array $config)
{
self::$ROOT_DIR = $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->debug = $config['debug'] ?: false;
if($this->debug) {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
}
$this->db = new Database($config['db']);
$this->userClass = $config['userClass'];
$primaryValue = $this->session->get('user');
if ($primaryValue) {
$primaryKey = $this->userClass::primaryKey();
$this->user = $this->userClass::findOne([$primaryKey => $primaryValue]);
}
}
/**
* @throws Exception
*/
public function run()
{
// FIXME: Call App Middleware
try {
$response = $this->router->resolve();
echo $response->body;
} catch (Exception $e) {
if($this->debug) {
throw $e;
} else {
$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)
{
$this->controller = $controller;
}
public function login(UserModel $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');
}
public static function isGuest(): bool
{
return !self::$app->user;
}
/**
* @param ExecutableInterface $executable
*
* @throws LogicException
*/
public function setCallstack(ExecutableInterface $executable)
{
if (empty($this->middlewares)) {
throw new LogicException('Apps Middlewares cannot be empty while using callstack!');
}
$this->callstack = array_merge($this->callstack, $this->middlewares);
array_push($this->callstack, $executable);
}
/**
* @throws LogicException
*/
public function executeCallstack(): Response
{
if (empty($this->callstack) || is_null($next = array_shift($this->callstack))) {
throw new LogicException('Trying to execute an empty callstack!');
}
if (is_string($next)) $next = new $next($this->callstack);
if (!$next instanceof ExecutableInterface) {
throw new LogicException('Callstack contains an object which is not an instance of Executable!');
}
return $next->execute();
}
}