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