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
|
|
|
$this->config = $this->loadConfig('site'); |
45
|
|
|
try { |
46
|
|
|
if ($this->config['debug']) { |
47
|
|
|
$whoops = new Whoops(); |
48
|
|
|
$whoops->pushHandler(new PrettyPageHandler()); |
|
|
|
|
49
|
|
|
$whoops->register(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->container = [ |
53
|
|
|
'config' => $this->config, |
54
|
|
|
'dbh' => $this->dbConnect($this->loadConfig('db')), |
55
|
|
|
'dir' => $this->getDir(), |
56
|
|
|
'flash' => new Flash(), |
57
|
|
|
'request' => [ |
58
|
|
|
'get' => $_GET, |
59
|
|
|
'post' => $_POST, |
60
|
|
|
'session' => &$_SESSION, |
61
|
|
|
'files' => $_FILES, |
62
|
|
|
], |
63
|
|
|
'rootDir' => $this->getRootDir(), |
64
|
|
|
'version' => self::VERSION, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->container['auth'] = new Auth($this->loadConfig('users'), $this->container['request']); |
68
|
|
|
} catch (Exception $e) { |
69
|
|
|
http_response_code(500); |
70
|
|
|
echo $e->getMessage(); |
71
|
|
|
$this->terminate(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->router = new AltoRouter(); |
75
|
|
|
$this->setUrl($url); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get config |
80
|
|
|
* |
81
|
|
|
* @param string $name Config filename |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function loadConfig($name) |
86
|
|
|
{ |
87
|
|
|
$path = dirname(__DIR__).'/config/'.$name.'.php'; |
88
|
|
|
try { |
89
|
|
|
if (!file_exists($path)) { |
90
|
|
|
http_response_code(500); |
91
|
|
|
throw new Exception('Config file '.$name.'.php does not exist'); |
92
|
|
|
} |
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
echo $e->getMessage(); |
95
|
|
|
$this->terminate(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return include $path; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Run app. |
103
|
|
|
*/ |
104
|
|
|
public function invoke() |
105
|
|
|
{ |
106
|
|
|
$this->loadRoutes(); |
107
|
|
|
|
108
|
|
|
$match = $this->router->match($this->url); |
109
|
|
|
|
110
|
|
|
try { |
111
|
|
|
// call closure or throw 404 status |
112
|
|
|
if ($match && is_callable($match['target'])) { |
113
|
|
|
echo call_user_func_array([ |
114
|
|
|
new $match['target'][0]($this->container), $match['target'][1], |
115
|
|
|
], $match['params']); |
116
|
|
|
} else { |
117
|
|
|
throw new Exception('Page not exist! No route match'); |
118
|
|
|
} |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
if ($this->config['debug']) { |
121
|
|
|
throw new DebugException($e->getMessage()); |
122
|
|
|
} else { |
123
|
|
|
(new Error($this->container))->showAction(404); |
124
|
|
|
} |
125
|
|
|
$this->terminate(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Load routes. |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function loadRoutes() |
135
|
|
|
{ |
136
|
|
|
$routesArray = include __DIR__.'/routes.php'; |
137
|
|
|
|
138
|
|
|
foreach ($routesArray as $key => $route) { |
139
|
|
|
if (2 === count($route)) { |
140
|
|
|
$this->router->map( |
141
|
|
|
$route[0][0], |
142
|
|
|
$route[0][1], |
143
|
|
|
[ |
144
|
|
|
'Egzaminer\Controller\\'.$route[0][2][0], |
145
|
|
|
$route[0][2][1], |
146
|
|
|
], |
147
|
|
|
$key.'/'.$route[0][0] |
148
|
|
|
); |
149
|
|
|
$route = $route[1]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->router->map( |
153
|
|
|
$route[0], |
154
|
|
|
$route[1], |
155
|
|
|
[ |
156
|
|
|
'Egzaminer\Controller\\'.$route[2][0], |
157
|
|
|
$route[2][1], |
158
|
|
|
], |
159
|
|
|
$key.'/'.$route[0] |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function dbConnect(array $config) |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
|
|
$dsn = 'mysql' |
168
|
|
|
.':dbname='.$config['name'] |
169
|
|
|
.';host='.$config['host'] |
170
|
|
|
.';charset=utf8'; |
171
|
|
|
|
172
|
|
|
$user = $config['user']; |
173
|
|
|
$password = $config['pass']; |
174
|
|
|
|
175
|
|
|
$dbh = new PDO($dsn, $user, $password); |
176
|
|
|
|
177
|
|
|
if ($this->config['debug']) { |
178
|
|
|
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return $dbh; |
182
|
|
|
} catch (PDOException $e) { |
183
|
|
|
http_response_code(500); |
184
|
|
|
|
185
|
|
|
if ($this->config['debug']) { |
186
|
|
|
throw new DebugException($e->getMessage()); |
187
|
|
|
} else { |
188
|
|
|
echo 'Error 500'; |
189
|
|
|
} |
190
|
|
|
$this->terminate(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function terminate($code = 1) |
195
|
|
|
{ |
196
|
|
|
exit($code); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set request url. |
201
|
|
|
* |
202
|
|
|
* @param string $request |
203
|
|
|
*/ |
204
|
|
|
public function setUrl($request) |
205
|
|
|
{ |
206
|
|
|
$this->url = substr($request, strlen($this->getDir())); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get app root dir. |
211
|
|
|
* |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getRootDir() |
215
|
|
|
{ |
216
|
|
|
return dirname(__DIR__); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get app dir. |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function getDir() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
if (dirname($_SERVER['SCRIPT_NAME']) == '/') { |
227
|
|
|
return ''; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return dirname($_SERVER['SCRIPT_NAME']); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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: