1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egzaminer; |
4
|
|
|
|
5
|
|
|
use AltoRouter; |
6
|
|
|
use Exception; |
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
use Tamtamchik\SimpleFlash\Flash; |
10
|
|
|
use Whoops\Handler\PrettyPageHandler; |
11
|
|
|
use Whoops\Run as Whoops; |
12
|
|
|
|
13
|
|
|
class App |
14
|
|
|
{ |
15
|
|
|
const VERSION = '0.12.0'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $url; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AltoRouter |
24
|
|
|
*/ |
25
|
|
|
private $router; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $container; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* @param string $url |
41
|
|
|
*/ |
42
|
|
|
public function __construct($url) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$configPath = $this->getRootDir().'/config/site.php'; |
46
|
|
|
if (!file_exists($configPath)) { |
47
|
|
|
http_response_code(500); |
48
|
|
|
throw new Exception('Config file site.php does not exist'); |
49
|
|
|
} |
50
|
|
|
$this->config = include $configPath; |
51
|
|
|
|
52
|
|
|
if ($this->config['debug']) { |
53
|
|
|
$whoops = new Whoops(); |
54
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
55
|
|
|
$whoops->register(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->container = [ |
59
|
|
|
'config' => $this->config, |
60
|
|
|
'dbh' => $this->dbConnect(include $this->getRootDir().'/config/db.php'), |
61
|
|
|
'dir' => $this->getDir(), |
62
|
|
|
'flash' => new Flash(), |
63
|
|
|
'request' => [ |
64
|
|
|
'get' => $_GET, |
65
|
|
|
'post' => $_POST, |
66
|
|
|
'session' => &$_SESSION, |
67
|
|
|
'cookie' => $_COOKIE, |
68
|
|
|
'files' => $_FILES, |
69
|
|
|
'server' => $_SERVER, |
70
|
|
|
], |
71
|
|
|
'rootDir' => $this->getRootDir(), |
72
|
|
|
'version' => self::VERSION, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
$configPath = $this->getRootDir().'/config/users.php'; |
76
|
|
|
if (!file_exists($configPath)) { |
77
|
|
|
http_response_code(500); |
78
|
|
|
throw new Exception('Config file users.php does not exist'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->container['auth'] = new Auth(include $configPath, $this->container['request']); |
82
|
|
|
} catch (Exception $e) { |
83
|
|
|
http_response_code(500); |
84
|
|
|
echo $e->getMessage(); |
85
|
|
|
$this->terminate(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->router = new AltoRouter(); |
89
|
|
|
$this->setUrl($url); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Run app. |
94
|
|
|
*/ |
95
|
|
|
public function invoke() |
96
|
|
|
{ |
97
|
|
|
$this->loadRoutes(); |
98
|
|
|
|
99
|
|
|
$match = $this->router->match($this->url); |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
// call closure or throw 404 status |
103
|
|
|
if ($match && is_callable($match['target'])) { |
104
|
|
|
call_user_func_array([ |
105
|
|
|
new $match['target'][0]($this->container), $match['target'][1], |
106
|
|
|
], $match['params']); |
107
|
|
|
} else { |
108
|
|
|
throw new Exception('Page not exist! No route match'); |
109
|
|
|
} |
110
|
|
|
} catch (Exception $e) { |
111
|
|
|
if ($this->config['debug']) { |
112
|
|
|
throw new DebugException($e->getMessage()); |
113
|
|
|
} else { |
114
|
|
|
(new Error($this->container))->showAction(404); |
115
|
|
|
} |
116
|
|
|
$this->terminate(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Load routes. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
|
|
public function loadRoutes() |
126
|
|
|
{ |
127
|
|
|
$routesArray = include __DIR__.'/routes.php'; |
128
|
|
|
|
129
|
|
|
foreach ($routesArray as $key => $route) { |
130
|
|
|
if (2 === count($route)) { |
131
|
|
|
$this->router->map( |
132
|
|
|
$route[0][0], |
133
|
|
|
$route[0][1], |
134
|
|
|
[ |
135
|
|
|
'Egzaminer\Controller\\'.$route[0][2][0], |
136
|
|
|
$route[0][2][1] |
137
|
|
|
], |
138
|
|
|
$key.'/'.$route[0][0] |
139
|
|
|
); |
140
|
|
|
$route = $route[1]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->router->map( |
144
|
|
|
$route[0], |
145
|
|
|
$route[1], |
146
|
|
|
[ |
147
|
|
|
'Egzaminer\Controller\\'.$route[2][0], |
148
|
|
|
$route[2][1] |
149
|
|
|
], |
150
|
|
|
$key.'/'.$route[0] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function dbConnect(array $config) |
156
|
|
|
{ |
157
|
|
|
try { |
158
|
|
|
$dsn = 'mysql' |
159
|
|
|
.':dbname='.$config['name'] |
160
|
|
|
.';host='.$config['host'] |
161
|
|
|
.';charset=utf8'; |
162
|
|
|
|
163
|
|
|
$user = $config['user']; |
164
|
|
|
$password = $config['pass']; |
165
|
|
|
|
166
|
|
|
$dbh = new PDO($dsn, $user, $password); |
167
|
|
|
|
168
|
|
|
if ($this->config['debug']) { |
169
|
|
|
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $dbh; |
173
|
|
|
} catch (PDOException $e) { |
174
|
|
|
http_response_code(500); |
175
|
|
|
|
176
|
|
|
if ($this->config['debug']) { |
177
|
|
|
throw new DebugException($e->getMessage()); |
178
|
|
|
} else { |
179
|
|
|
echo 'Error 500'; |
180
|
|
|
} |
181
|
|
|
$this->terminate(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function terminate($code = 1) |
186
|
|
|
{ |
187
|
|
|
exit($code); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set request url. |
192
|
|
|
* |
193
|
|
|
* @param string $request |
194
|
|
|
*/ |
195
|
|
|
public function setUrl($request) |
196
|
|
|
{ |
197
|
|
|
$this->url = substr($request, strlen($this->getDir())); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get app root dir. |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getRootDir() |
206
|
|
|
{ |
207
|
|
|
return dirname(__DIR__); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get app dir. |
212
|
|
|
* |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function getDir() |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
if (dirname($_SERVER['SCRIPT_NAME']) == '/') { |
218
|
|
|
return ''; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return dirname($_SERVER['SCRIPT_NAME']); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: