|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System PHP Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
/* |
|
19
|
|
|
* --------------------------------------------------------------- |
|
20
|
|
|
* ERROR REPORTING |
|
21
|
|
|
* --------------------------------------------------------------- |
|
22
|
|
|
* |
|
23
|
|
|
* Different environments will require different levels of error reporting. |
|
24
|
|
|
* By default development will show errors but testing and live will hide them. |
|
25
|
|
|
*/ |
|
26
|
|
|
switch (strtoupper(ENVIRONMENT)) { |
|
|
|
|
|
|
27
|
|
|
case 'DEVELOPMENT': |
|
28
|
|
|
error_reporting(-1); |
|
29
|
|
|
ini_set('display_errors', 1); |
|
30
|
|
|
break; |
|
31
|
|
|
case 'TESTING': |
|
32
|
|
|
case 'PRODUCTION': |
|
33
|
|
|
ini_set('display_errors', 0); |
|
34
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
|
35
|
|
|
break; |
|
36
|
|
|
default: |
|
37
|
|
|
header('HTTP/1.1 503 Service Unavailable.', true, 503); |
|
38
|
|
|
echo 'The application environment is not set correctly.'; |
|
39
|
|
|
exit(1); // EXIT_ERROR |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/* |
|
43
|
|
|
*--------------------------------------------------------------- |
|
44
|
|
|
* VENDOR PATH |
|
45
|
|
|
*--------------------------------------------------------------- |
|
46
|
|
|
* |
|
47
|
|
|
* RealPath to vendor folder. |
|
48
|
|
|
* |
|
49
|
|
|
* WITH TRAILING SLASH! |
|
50
|
|
|
*/ |
|
51
|
|
|
if ( ! defined('PATH_VENDOR')) { |
|
52
|
|
|
define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/* |
|
56
|
|
|
*--------------------------------------------------------------- |
|
57
|
|
|
* FRAMEWORK PATH |
|
58
|
|
|
*--------------------------------------------------------------- |
|
59
|
|
|
* |
|
60
|
|
|
* RealPath to framework folder. |
|
61
|
|
|
* |
|
62
|
|
|
* WITH TRAILING SLASH! |
|
63
|
|
|
*/ |
|
64
|
|
|
if ( ! defined('PATH_FRAMEWORK')) { |
|
65
|
|
|
define('PATH_FRAMEWORK', __DIR__ . DIRECTORY_SEPARATOR); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
*--------------------------------------------------------------- |
|
70
|
|
|
* APP PATH |
|
71
|
|
|
*--------------------------------------------------------------- |
|
72
|
|
|
* |
|
73
|
|
|
* RealPath to application folder. |
|
74
|
|
|
* |
|
75
|
|
|
* WITH TRAILING SLASH! |
|
76
|
|
|
*/ |
|
77
|
|
|
if ( ! defined('PATH_APP')) { |
|
78
|
|
|
define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/* |
|
82
|
|
|
*--------------------------------------------------------------- |
|
83
|
|
|
* PUBLIC PATH |
|
84
|
|
|
*--------------------------------------------------------------- |
|
85
|
|
|
* |
|
86
|
|
|
* RealPath to public folder. |
|
87
|
|
|
* |
|
88
|
|
|
* WITH TRAILING SLASH! |
|
89
|
|
|
*/ |
|
90
|
|
|
if ( ! defined('PATH_PUBLIC')) { |
|
91
|
|
|
define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/* |
|
95
|
|
|
*--------------------------------------------------------------- |
|
96
|
|
|
* CACHE PATH |
|
97
|
|
|
*--------------------------------------------------------------- |
|
98
|
|
|
* |
|
99
|
|
|
* RealPath to writable caching folder. |
|
100
|
|
|
* |
|
101
|
|
|
* WITH TRAILING SLASH! |
|
102
|
|
|
*/ |
|
103
|
|
|
if ( ! defined('PATH_CACHE')) { |
|
104
|
|
|
define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/* |
|
108
|
|
|
*--------------------------------------------------------------- |
|
109
|
|
|
* STORAGE PATH |
|
110
|
|
|
*--------------------------------------------------------------- |
|
111
|
|
|
* |
|
112
|
|
|
* RealPath to writable storage folder. |
|
113
|
|
|
* |
|
114
|
|
|
* WITH TRAILING SLASH! |
|
115
|
|
|
*/ |
|
116
|
|
|
if ( ! defined('PATH_STORAGE')) { |
|
117
|
|
|
define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/* |
|
121
|
|
|
*--------------------------------------------------------------- |
|
122
|
|
|
* RESOURCES PATH |
|
123
|
|
|
*--------------------------------------------------------------- |
|
124
|
|
|
* |
|
125
|
|
|
* RealPath to writable resources folder. |
|
126
|
|
|
* |
|
127
|
|
|
* WITH TRAILING SLASH! |
|
128
|
|
|
*/ |
|
129
|
|
|
if ( ! defined('PATH_RESOURCES')) { |
|
130
|
|
|
define('PATH_RESOURCES', PATH_ROOT . DIR_RESOURCES . DIRECTORY_SEPARATOR); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/* |
|
135
|
|
|
*--------------------------------------------------------------- |
|
136
|
|
|
* FRAMEWORK CONSTANTS |
|
137
|
|
|
*--------------------------------------------------------------- |
|
138
|
|
|
*/ |
|
139
|
|
|
require __DIR__ . '/Config/Constants.php'; |
|
140
|
|
|
|
|
141
|
|
|
/* |
|
142
|
|
|
*--------------------------------------------------------------- |
|
143
|
|
|
* FRAMEWORK HELPERS |
|
144
|
|
|
*--------------------------------------------------------------- |
|
145
|
|
|
*/ |
|
146
|
|
|
require __DIR__ . '/Helpers/Reactor.php'; |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Class Reactor |
|
150
|
|
|
* |
|
151
|
|
|
* @package O2System |
|
152
|
|
|
*/ |
|
153
|
|
|
class Reactor extends Kernel |
|
154
|
|
|
{ |
|
155
|
|
|
/** |
|
156
|
|
|
* Reactor Container Models |
|
157
|
|
|
* |
|
158
|
|
|
* @var Reactor\Containers\Models |
|
159
|
|
|
*/ |
|
160
|
|
|
public $models; |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Reactor Container Modules |
|
164
|
|
|
* |
|
165
|
|
|
* @var Reactor\Containers\Modules |
|
|
|
|
|
|
166
|
|
|
*/ |
|
167
|
|
|
public $modules; |
|
168
|
|
|
|
|
169
|
|
|
// ------------------------------------------------------------------------ |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Reactor::__construct |
|
173
|
|
|
* |
|
174
|
|
|
* @return Reactor |
|
175
|
|
|
*/ |
|
176
|
|
|
protected function __construct() |
|
177
|
|
|
{ |
|
178
|
|
|
parent::__construct(); |
|
179
|
|
|
|
|
180
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
181
|
|
|
profiler()->watch('Starting O2System Reactor'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
185
|
|
|
profiler()->watch('Starting Reactor Services'); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$services = [ |
|
189
|
|
|
'Services\Loader' => 'loader', |
|
190
|
|
|
'Services\Config' => 'config' |
|
191
|
|
|
]; |
|
192
|
|
|
|
|
193
|
|
|
foreach ($services as $className => $classOffset) { |
|
194
|
|
|
$this->services->load($className, $classOffset); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Instantiate Models Container |
|
198
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
199
|
|
|
profiler()->watch('Starting Models Container'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->models = new Reactor\Containers\Models(); |
|
203
|
|
|
|
|
204
|
|
|
// Instantiate Cache Service |
|
205
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
206
|
|
|
profiler()->watch('Starting Cache Service'); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$this->services->add(new Reactor\Services\Cache(config('cache', true)), 'cache'); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
// ------------------------------------------------------------------------ |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Reactor::__reconstruct |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function __reconstruct() |
|
218
|
|
|
{ |
|
219
|
|
|
if (is_cli()) { |
|
220
|
|
|
$this->cliHandler(); |
|
221
|
|
|
} else { |
|
222
|
|
|
$this->httpHandler(); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// ------------------------------------------------------------------------ |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Reactor::cliHandler |
|
230
|
|
|
* |
|
231
|
|
|
* @return void |
|
232
|
|
|
*/ |
|
233
|
|
|
private function cliHandler() |
|
234
|
|
|
{ |
|
235
|
|
|
// Instantiate CLI Router Service |
|
236
|
|
|
$this->services->load(Kernel\Cli\Router::class); |
|
237
|
|
|
|
|
238
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
239
|
|
|
profiler()->watch('Parse Router Request'); |
|
240
|
|
|
} |
|
241
|
|
|
router()->parseRequest(); |
|
242
|
|
|
|
|
243
|
|
|
if ($commander = router()->getCommander()) { |
|
244
|
|
|
if ($commander instanceof Kernel\Cli\Router\Datastructures\Commander) { |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
// Autoload Model |
|
247
|
|
|
foreach ($this->modules as $module) { |
|
248
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
|
249
|
|
|
continue; |
|
250
|
|
|
} |
|
251
|
|
|
$module->loadModel(); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// Autoload Model |
|
255
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
|
256
|
|
|
|
|
257
|
|
|
if (class_exists($modelClassName)) { |
|
258
|
|
|
models()->load($modelClassName, 'commander'); |
|
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
262
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
|
263
|
|
|
} |
|
264
|
|
|
$requestCommander = $commander->getInstance(); |
|
265
|
|
|
|
|
266
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
267
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
|
268
|
|
|
} |
|
269
|
|
|
$requestCommander->execute(); |
|
270
|
|
|
|
|
271
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
// ------------------------------------------------------------------------ |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* Reactor::httpHandler |
|
280
|
|
|
* |
|
281
|
|
|
* @return void |
|
282
|
|
|
*/ |
|
283
|
|
|
private function httpHandler() |
|
284
|
|
|
{ |
|
285
|
|
|
// Instantiate Http Router Service |
|
286
|
|
|
$this->services->load(Reactor\Http\Router::class); |
|
|
|
|
|
|
287
|
|
|
|
|
288
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
289
|
|
|
profiler()->watch('Parse Router Request'); |
|
290
|
|
|
} |
|
291
|
|
|
router()->parseRequest(); |
|
292
|
|
|
|
|
293
|
|
|
// Instantiate Http Middleware Service |
|
294
|
|
|
$this->services->load(Reactor\Http\Middleware::class); |
|
295
|
|
|
|
|
296
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
297
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
|
298
|
|
|
} |
|
299
|
|
|
middleware()->run(); |
|
300
|
|
|
|
|
301
|
|
|
if (false !== ($controller = $this->services->get('controller'))) { |
|
302
|
|
|
if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
|
303
|
|
|
// Autoload Model |
|
304
|
|
|
foreach ($this->modules as $module) { |
|
305
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
|
306
|
|
|
continue; |
|
307
|
|
|
} |
|
308
|
|
|
$module->loadModel(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
// Autoload Model |
|
312
|
|
|
$modelClassName = str_replace('Controllers', 'Models', $controller->getName()); |
|
313
|
|
|
|
|
314
|
|
|
if (class_exists($modelClassName)) { |
|
315
|
|
|
$this->models->register($modelClassName, 'controller'); |
|
|
|
|
|
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
// Initialize Controller |
|
319
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
320
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
324
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
|
325
|
|
|
} |
|
326
|
|
|
$requestController = $controller->getInstance(); |
|
327
|
|
|
|
|
328
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
|
329
|
|
|
$requestController->__reconstruct(); |
|
330
|
|
|
} elseif (method_exists($requestController, 'initialize')) { |
|
331
|
|
|
$requestController->initialize(); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
$this->services->add($requestController, 'controller'); |
|
335
|
|
|
|
|
336
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
337
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
|
338
|
|
|
} |
|
339
|
|
|
hooks()->callEvent(Reactor\Services\Hooks::POST_CONTROLLER); |
|
|
|
|
|
|
340
|
|
|
|
|
341
|
|
|
$requestMethod = $controller->getRequestMethod(); |
|
342
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
|
343
|
|
|
|
|
344
|
|
|
// Call the requested controller method |
|
345
|
|
|
if (profiler() !== false) { |
|
|
|
|
|
|
346
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
|
347
|
|
|
} |
|
348
|
|
|
ob_start(); |
|
349
|
|
|
$requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
|
350
|
|
|
|
|
351
|
|
|
if (empty($requestControllerOutput)) { |
|
352
|
|
|
$requestControllerOutput = ob_get_contents(); |
|
353
|
|
|
ob_end_clean(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
|
357
|
|
|
// Send default error 204 - No Content |
|
358
|
|
|
output()->sendError(204); |
|
359
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
|
360
|
|
|
if ($requestControllerOutput === true) { |
|
361
|
|
|
output()->sendError(200); |
|
362
|
|
|
} elseif ($requestControllerOutput === false) { |
|
|
|
|
|
|
363
|
|
|
output()->sendError(204); |
|
364
|
|
|
} |
|
365
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
|
366
|
|
|
$requestController->sendPayload($requestControllerOutput); |
|
367
|
|
|
} elseif (is_numeric($requestControllerOutput)) { |
|
368
|
|
|
output()->sendError($requestControllerOutput); |
|
369
|
|
|
} elseif (is_string($requestControllerOutput)) { |
|
370
|
|
|
if (is_json($requestControllerOutput)) { |
|
371
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
|
|
372
|
|
|
output()->send($requestControllerOutput); |
|
|
|
|
|
|
373
|
|
|
} elseif (is_serialized($requestControllerOutput)) { |
|
374
|
|
|
output()->send($requestControllerOutput); |
|
375
|
|
|
} else { |
|
376
|
|
|
output()->send($requestControllerOutput); |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
// Show Error (404) Page Not Found |
|
383
|
|
|
output()->sendError(404); |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|