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