|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UString; |
|
6
|
|
|
use Ubiquity\views\engine\TemplateEngine; |
|
7
|
|
|
use Ubiquity\utils\http\USession; |
|
8
|
|
|
use Ubiquity\log\Logger; |
|
9
|
|
|
use Ubiquity\controllers\traits\StartupConfigTrait; |
|
10
|
|
|
use Ubiquity\controllers\di\DiManager; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Starts the framework. |
|
14
|
|
|
* This class is part of Ubiquity |
|
15
|
|
|
* |
|
16
|
|
|
* @author jcheron <[email protected]> |
|
17
|
|
|
* @version 1.1.0 |
|
18
|
|
|
* |
|
19
|
|
|
*/ |
|
20
|
|
|
class Startup { |
|
21
|
|
|
use StartupConfigTrait; |
|
22
|
|
|
public static $urlParts; |
|
23
|
|
|
public static $templateEngine; |
|
24
|
|
|
private static $controller; |
|
25
|
|
|
private static $action; |
|
26
|
|
|
private static $actionParams; |
|
27
|
|
|
|
|
28
|
33 |
|
private static function _preRunAction(&$u, $initialize = true, $finalize = true) { |
|
29
|
33 |
|
if (\class_exists ( $u [0] )) { |
|
30
|
33 |
|
self::runAction ( $u, $initialize, $finalize ); |
|
31
|
|
|
} else { |
|
32
|
|
|
\header ( 'HTTP/1.0 404 Not Found', true, 404 ); |
|
33
|
|
|
Logger::warn ( "Startup", "The controller `" . $u [0] . "` doesn't exists! <br/>", "forward" ); |
|
34
|
|
|
} |
|
35
|
33 |
|
} |
|
36
|
|
|
|
|
37
|
33 |
|
private static function parseUrl(&$url) { |
|
38
|
33 |
|
if (! $url) { |
|
39
|
2 |
|
$url = "_default"; |
|
40
|
|
|
} |
|
41
|
33 |
|
if (UString::endswith ( $url, "/" )) |
|
42
|
11 |
|
$url = \substr ( $url, 0, strlen ( $url ) - 1 ); |
|
43
|
33 |
|
self::$urlParts = \explode ( "/", $url ); |
|
44
|
|
|
|
|
45
|
33 |
|
return self::$urlParts; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
32 |
|
private static function startTemplateEngine(&$config) { |
|
49
|
|
|
try { |
|
50
|
32 |
|
if (isset ( $config ['templateEngine'] )) { |
|
51
|
32 |
|
$templateEngine = $config ['templateEngine']; |
|
52
|
32 |
|
$engineOptions = $config ['templateEngineOptions'] ?? array ('cache' => \ROOT . \DS . 'views/cache/' ); |
|
53
|
32 |
|
$engine = new $templateEngine ( $engineOptions ); |
|
54
|
32 |
|
if ($engine instanceof TemplateEngine) { |
|
55
|
32 |
|
self::$templateEngine = $engine; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} catch ( \Exception $e ) { |
|
59
|
|
|
echo $e->getTraceAsString (); |
|
60
|
|
|
} |
|
61
|
32 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Handles the request |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $config The loaded config array |
|
67
|
|
|
*/ |
|
68
|
32 |
|
public static function run(array &$config) { |
|
69
|
32 |
|
self::$config = $config; |
|
70
|
32 |
|
self::startTemplateEngine ( $config ); |
|
71
|
32 |
|
if (isset ( $config ['sessionName'] )) |
|
72
|
32 |
|
USession::start ( $config ['sessionName'] ); |
|
73
|
32 |
|
self::forward ( $_GET ['c'] ); |
|
74
|
32 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Forwards to url |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $url The url to forward to |
|
80
|
|
|
* @param boolean $initialize If true, the **initialize** method of the controller is called |
|
81
|
|
|
* @param boolean $finalize If true, the **finalize** method of the controller is called |
|
82
|
|
|
*/ |
|
83
|
33 |
|
public static function forward($url, $initialize = true, $finalize = true) { |
|
84
|
33 |
|
$u = self::parseUrl ( $url ); |
|
85
|
33 |
|
if (($ru = Router::getRoute ( $url )) !== false) { |
|
86
|
13 |
|
if (\is_array ( $ru )) { |
|
87
|
13 |
|
if (is_callable ( $ru [0] )) { |
|
88
|
|
|
self::runCallable ( $ru ); |
|
89
|
|
|
} else { |
|
90
|
13 |
|
self::_preRunAction ( $ru, $initialize, $finalize ); |
|
91
|
|
|
} |
|
92
|
|
|
} else { |
|
93
|
5 |
|
echo $ru; // Displays route response from cache |
|
94
|
|
|
} |
|
95
|
|
|
} else { |
|
96
|
26 |
|
self::setCtrlNS (); |
|
97
|
26 |
|
$u [0] = self::$ctrlNS . $u [0]; |
|
98
|
26 |
|
self::_preRunAction ( $u, $initialize, $finalize ); |
|
99
|
|
|
} |
|
100
|
33 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns the template engine instance |
|
104
|
|
|
* |
|
105
|
|
|
* @return TemplateEngine |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public static function getTempateEngineInstance() { |
|
108
|
2 |
|
$config = self::$config; |
|
109
|
2 |
|
if (isset ( $config ['templateEngine'] )) { |
|
110
|
2 |
|
$templateEngine = $config ['templateEngine']; |
|
111
|
2 |
|
return new $templateEngine ( [ ] ); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Runs an action on a controller |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $u An array containing controller, action and parameters |
|
119
|
|
|
* @param boolean $initialize If true, the **initialize** method of the controller is called |
|
120
|
|
|
* @param boolean $finalize If true, the **finalize** method of the controller is called |
|
121
|
|
|
*/ |
|
122
|
34 |
|
public static function runAction(array &$u, $initialize = true, $finalize = true) { |
|
123
|
34 |
|
$ctrl = $u [0]; |
|
124
|
34 |
|
self::$controller = $ctrl; |
|
125
|
34 |
|
self::$action = "index"; |
|
126
|
34 |
|
self::$actionParams = [ ]; |
|
127
|
34 |
|
if (\sizeof ( $u ) > 1) |
|
128
|
33 |
|
self::$action = $u [1]; |
|
129
|
34 |
|
if (\sizeof ( $u ) > 2) |
|
130
|
16 |
|
self::$actionParams = array_slice ( $u, 2 ); |
|
131
|
|
|
|
|
132
|
34 |
|
$controller = new $ctrl (); |
|
133
|
34 |
|
if (! $controller instanceof Controller) { |
|
134
|
|
|
print "`{$u[0]}` isn't a controller instance.`<br/>"; |
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
// Dependency injection |
|
138
|
34 |
|
self::injectDependences ( $controller ); |
|
139
|
34 |
|
if (! $controller->isValid ( self::$action )) { |
|
140
|
6 |
|
$controller->onInvalidControl (); |
|
141
|
|
|
} else { |
|
142
|
34 |
|
if ($initialize) |
|
143
|
34 |
|
$controller->initialize (); |
|
144
|
34 |
|
self::callController ( $controller, $u ); |
|
145
|
34 |
|
if ($finalize) |
|
146
|
34 |
|
$controller->finalize (); |
|
147
|
|
|
} |
|
148
|
34 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Runs a callback |
|
152
|
|
|
* |
|
153
|
|
|
* @param array $u An array containing a callback, and some parameters |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function runCallable(array &$u) { |
|
156
|
|
|
self::$actionParams = [ ]; |
|
157
|
|
|
if (\sizeof ( $u ) > 1) { |
|
158
|
|
|
self::$actionParams = array_slice ( $u, 1 ); |
|
159
|
|
|
} |
|
160
|
|
|
if (isset ( self::$config ['di'] )) { |
|
161
|
|
|
$di = self::$config ['di']; |
|
162
|
|
|
if (\is_array ( $di )) { |
|
163
|
|
|
self::$actionParams = array_merge ( self::$actionParams, $di ); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
call_user_func_array ( $u [0], self::$actionParams ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Injects the dependencies from the **di** config key in a controller |
|
171
|
|
|
* |
|
172
|
|
|
* @param Controller $controller The controller |
|
173
|
|
|
*/ |
|
174
|
37 |
|
public static function injectDependences($controller) { |
|
175
|
37 |
|
$di = DiManager::fetch ( $controller ); |
|
176
|
37 |
|
if ($di !== false) { |
|
177
|
16 |
|
$rClass = new \ReflectionClass ( $controller ); |
|
178
|
16 |
|
foreach ( $di as $k => $v ) { |
|
179
|
16 |
|
if ($rClass->hasProperty ( $k )) { |
|
180
|
|
|
$prop = $rClass->getProperty ( $k ); |
|
181
|
|
|
$prop->setAccessible ( true ); |
|
182
|
|
|
$prop->setValue ( $controller, $v ( $controller ) ); |
|
183
|
|
|
} else { |
|
184
|
16 |
|
$controller->$k = $v ( $controller ); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
37 |
|
$di = self::$config ['di'] ?? [ ]; |
|
189
|
37 |
|
if (isset ( $di ['@exec'] )) { |
|
190
|
8 |
|
foreach ( $di ['@exec'] as $k => $v ) { |
|
191
|
8 |
|
$controller->$k = $v ( $controller ); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
37 |
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Runs an action on a controller and returns a string |
|
198
|
|
|
* |
|
199
|
|
|
* @param array $u |
|
200
|
|
|
* @param boolean $initialize If true, the **initialize** method of the controller is called |
|
201
|
|
|
* @param boolean $finalize If true, the **finalize** method of the controller is called |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public static function runAsString(array &$u, $initialize = true, $finalize = true) { |
|
205
|
1 |
|
\ob_start (); |
|
206
|
1 |
|
self::runAction ( $u, $initialize, $finalize ); |
|
207
|
1 |
|
return \ob_get_clean (); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
34 |
|
private static function callController(Controller $controller, array &$u) { |
|
211
|
34 |
|
$urlSize = sizeof ( $u ); |
|
212
|
|
|
switch ($urlSize) { |
|
213
|
34 |
|
case 1 : |
|
214
|
15 |
|
$controller->index (); |
|
215
|
15 |
|
break; |
|
216
|
31 |
|
case 2 : |
|
217
|
22 |
|
$action = $u [1]; |
|
218
|
|
|
// Appel de la méthode (2ème élément du tableau) |
|
219
|
22 |
|
if (\method_exists ( $controller, $action )) { |
|
220
|
22 |
|
$controller->$action (); |
|
221
|
|
|
} else { |
|
222
|
|
|
Logger::warn ( "Startup", "The method `{$action}` doesn't exists on controller `" . $u [0] . "`", "callController" ); |
|
223
|
|
|
} |
|
224
|
22 |
|
break; |
|
225
|
|
|
default : |
|
226
|
|
|
// Appel de la méthode en lui passant en paramètre le reste du tableau |
|
227
|
16 |
|
\call_user_func_array ( array ($controller,$u [1] ), self::$actionParams ); |
|
228
|
16 |
|
break; |
|
229
|
|
|
} |
|
230
|
34 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
public static function errorHandler($message = "", $code = 0, $severity = 1, $filename = null, int $lineno = 0, $previous = NULL) { |
|
233
|
|
|
if (\error_reporting () == 0) { |
|
234
|
|
|
return; |
|
235
|
|
|
} |
|
236
|
|
|
if (\error_reporting () & $severity) { |
|
237
|
|
|
throw new \ErrorException ( $message, 0, $severity, $filename, $lineno, $previous ); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Returns the active controller name |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
8 |
|
public static function getController() { |
|
247
|
8 |
|
return self::$controller; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Returns the class simple name of the active controller |
|
252
|
|
|
* |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
1 |
|
public static function getControllerSimpleName() { |
|
256
|
1 |
|
return (new \ReflectionClass ( self::$controller ))->getShortName (); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Returns the extension for view files |
|
261
|
|
|
* |
|
262
|
|
|
* @return string |
|
263
|
|
|
*/ |
|
264
|
1 |
|
public static function getViewNameFileExtension() { |
|
265
|
1 |
|
return "html"; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Returns tha active action |
|
270
|
|
|
* |
|
271
|
|
|
* @return string |
|
272
|
|
|
*/ |
|
273
|
15 |
|
public static function getAction() { |
|
274
|
15 |
|
return self::$action; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Returns the active parameters |
|
279
|
|
|
* |
|
280
|
|
|
* @return array |
|
281
|
|
|
*/ |
|
282
|
6 |
|
public static function getActionParams() { |
|
283
|
6 |
|
return self::$actionParams; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Returns the framework directory |
|
288
|
|
|
* |
|
289
|
|
|
* @return string |
|
290
|
|
|
*/ |
|
291
|
35 |
|
public static function getFrameworkDir() { |
|
292
|
35 |
|
return \dirname ( __FILE__ ); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Returns the application directory (app directory) |
|
297
|
|
|
* |
|
298
|
|
|
* @return string |
|
299
|
|
|
*/ |
|
300
|
3 |
|
public static function getApplicationDir() { |
|
301
|
3 |
|
return \dirname ( \ROOT ); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Returns the application name |
|
306
|
|
|
* |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
1 |
|
public static function getApplicationName() { |
|
310
|
1 |
|
return basename ( \dirname ( \ROOT ) ); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|