1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lepton\Core; |
4
|
|
|
|
5
|
|
|
use Lepton\Core\Handler\AbstractHandler; |
6
|
|
|
use Lepton\Http\Request; |
7
|
|
|
use Lepton\Http\Response\{FileResponse, HttpResponse}; |
8
|
|
|
use Whoops; |
9
|
|
|
|
10
|
|
|
class Application |
11
|
|
|
{ |
12
|
|
|
protected static $_config; |
13
|
|
|
protected static $_db; |
14
|
|
|
protected static $_dbconfig; |
15
|
|
|
protected static $_auth; |
16
|
|
|
protected static $_email; |
17
|
|
|
|
18
|
|
|
public static $request; |
19
|
|
|
public static $routes; |
20
|
|
|
|
21
|
|
|
public static string $controller; |
22
|
|
|
public static $documentRoot; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Entry point for application logic |
26
|
|
|
* Here it is where the whole magic happens |
27
|
|
|
*/ |
28
|
|
|
public static function run() |
29
|
|
|
{ |
30
|
|
|
if(! static::$_config) { |
31
|
|
|
throw new \Exception("No config loaded!"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
session_start(); |
35
|
|
|
|
36
|
|
|
if(static::$_dbconfig->use_db) { |
37
|
|
|
static::$_db = new Database( |
38
|
|
|
static::$_dbconfig->host, |
39
|
|
|
static::$_dbconfig->user, |
40
|
|
|
static::$_dbconfig->password, |
41
|
|
|
static::$_dbconfig->dbname |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
static::$request = new Request(); |
46
|
|
|
$handler = static::getHandler(); |
47
|
|
|
$response = $handler->getResponse(); |
48
|
|
|
$response->send(); |
49
|
|
|
|
50
|
|
|
if(static::$_dbconfig->use_db) { |
51
|
|
|
static::$_db->close(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
exit(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the appropriate handler for the pending request |
60
|
|
|
* |
61
|
|
|
* @return AbstractHandler $handler |
62
|
|
|
*/ |
63
|
|
|
public static function getHandler(): AbstractHandler |
64
|
|
|
{ |
65
|
|
|
$regex = '/\.(?:'.implode('|', static::$_config->static_files_extensions).')$/'; |
66
|
|
|
if (preg_match($regex, static::$request->url)) { |
67
|
|
|
return new Handler\StaticHandler(static::$request); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$handler = new Handler\BaseHandler(static::$request); |
71
|
|
|
$handler->addMiddlewares(static::$_config->middlewares); |
72
|
|
|
return $handler; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Loads the configuration for the application |
78
|
|
|
* @param \stdClass $config |
79
|
|
|
*/ |
80
|
|
|
|
81
|
|
|
public static function loadConfig(\stdClass $config) |
82
|
|
|
{ |
83
|
|
|
if(property_exists($config, "app")) { |
84
|
|
|
static::$_config = $config->app; |
85
|
|
|
} else { |
86
|
|
|
throw new \Exception("No app configuration!"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if(property_exists($config, "routes")) { |
90
|
|
|
static::$routes = $config->routes; |
91
|
|
|
} else { |
92
|
|
|
throw new \Exception("No routes configuration!"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
if(property_exists($config, "database")) { |
97
|
|
|
static::$_dbconfig = $config->database; |
98
|
|
|
} else { |
99
|
|
|
throw new \Exception("No database configuration!"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
if(property_exists($config, "auth")) { |
104
|
|
|
static::$_auth = $config->auth; |
105
|
|
|
} else { |
106
|
|
|
throw new \Exception("No authentication configuration!"); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if(property_exists($config, "email")) { |
110
|
|
|
static::$_email = $config->email; |
111
|
|
|
} else { |
112
|
|
|
throw new \Exception("No email configuration!"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
static::$documentRoot = $_SERVER["DOCUMENT_ROOT"]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Loads the error handler (Whoops) |
122
|
|
|
* For Whoops documentation |
123
|
|
|
* @see https://filp.github.io/whoops/ |
124
|
|
|
*/ |
125
|
|
|
public static function loadErrorHandler() |
126
|
|
|
{ |
127
|
|
|
$whoops = new \Whoops\Run(); |
128
|
|
|
|
129
|
|
|
$handler = new \Whoops\Handler\PrettyPageHandler(); |
130
|
|
|
$handler->setPageTitle("Whooops! There was an unexpected error!"); |
131
|
|
|
$handler->addResourcePath(__DIR__."/css"); |
132
|
|
|
$handler->addCustomCss("whoops.css"); |
133
|
|
|
|
134
|
|
|
$whoops->pushHandler($handler); |
135
|
|
|
$whoops->register(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/*==================================== GETTERS ======================================= */ |
139
|
|
|
|
140
|
|
|
public static function getDb() |
141
|
|
|
{ |
142
|
|
|
return static::$_db; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public static function getDir() |
146
|
|
|
{ |
147
|
|
|
return static::$_config->base_url; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public static function getAuthConfig() |
151
|
|
|
{ |
152
|
|
|
return static::$_auth; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public static function getDbConfig() |
156
|
|
|
{ |
157
|
|
|
return static::$_dbconfig; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public static function getEmailConfig() |
161
|
|
|
{ |
162
|
|
|
return static::$_email; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public static function getAppConfig() |
166
|
|
|
{ |
167
|
|
|
return static::$_config; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.