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_REACTOR')) { |
65
|
|
|
define('PATH_REACTOR', __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
|
|
|
'Containers\Globals' => 'globals', |
190
|
|
|
'Containers\Environment' => 'environment', |
191
|
|
|
'Services\Loader' => 'loader', |
192
|
|
|
'Services\Config' => 'config' |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
foreach ($services as $className => $classOffset) { |
196
|
|
|
$this->services->load($className, $classOffset); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// Instantiate Models Container |
200
|
|
|
if (profiler() !== false) { |
|
|
|
|
201
|
|
|
profiler()->watch('Starting Models Container'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->models = new Reactor\Containers\Models(); |
205
|
|
|
|
206
|
|
|
// Instantiate Cache Service |
207
|
|
|
if (profiler() !== false) { |
|
|
|
|
208
|
|
|
profiler()->watch('Starting Cache Service'); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->services->add(new Reactor\Services\Cache(config('cache', true)), 'cache'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// ------------------------------------------------------------------------ |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Reactor::__reconstruct |
218
|
|
|
*/ |
219
|
|
|
protected function __reconstruct() |
220
|
|
|
{ |
221
|
|
|
if (is_cli()) { |
222
|
|
|
$this->cliHandler(); |
223
|
|
|
} else { |
224
|
|
|
$this->httpHandler(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// ------------------------------------------------------------------------ |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Reactor::cliHandler |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
private function cliHandler() |
236
|
|
|
{ |
237
|
|
|
// Instantiate CLI Router Service |
238
|
|
|
$this->services->load(Kernel\Cli\Router::class); |
239
|
|
|
|
240
|
|
|
if (profiler() !== false) { |
|
|
|
|
241
|
|
|
profiler()->watch('Parse Router Request'); |
242
|
|
|
} |
243
|
|
|
router()->parseRequest(); |
244
|
|
|
|
245
|
|
|
if ($commander = router()->getCommander()) { |
246
|
|
|
if ($commander instanceof Kernel\Cli\Router\Datastructures\Commander) { |
247
|
|
|
|
248
|
|
|
// Autoload Model |
249
|
|
|
foreach ($this->modules as $module) { |
250
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
251
|
|
|
continue; |
252
|
|
|
} |
253
|
|
|
$module->loadModel(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
// Autoload Model |
257
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
258
|
|
|
|
259
|
|
|
if (class_exists($modelClassName)) { |
260
|
|
|
models()->load($modelClassName, 'commander'); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if (profiler() !== false) { |
|
|
|
|
264
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
265
|
|
|
} |
266
|
|
|
$requestCommander = $commander->getInstance(); |
267
|
|
|
|
268
|
|
|
if (profiler() !== false) { |
|
|
|
|
269
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
270
|
|
|
} |
271
|
|
|
$requestCommander->execute(); |
272
|
|
|
|
273
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
// ------------------------------------------------------------------------ |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Reactor::httpHandler |
282
|
|
|
* |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
private function httpHandler() |
286
|
|
|
{ |
287
|
|
|
// Instantiate Http Router Service |
288
|
|
|
$this->services->load(Reactor\Http\Router::class); |
289
|
|
|
|
290
|
|
|
if (profiler() !== false) { |
|
|
|
|
291
|
|
|
profiler()->watch('Parse Router Request'); |
292
|
|
|
} |
293
|
|
|
router()->parseRequest(); |
294
|
|
|
|
295
|
|
|
// Instantiate Http Middleware Service |
296
|
|
|
$this->services->load(Reactor\Http\Middleware::class); |
297
|
|
|
|
298
|
|
|
if (profiler() !== false) { |
|
|
|
|
299
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
300
|
|
|
} |
301
|
|
|
middleware()->run(); |
302
|
|
|
|
303
|
|
|
if (false !== ($controller = $this->services->get('controller'))) { |
304
|
|
|
if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
305
|
|
|
// Autoload Model |
306
|
|
|
$modelClassName = str_replace('Controllers', 'Models', $controller->getName()); |
307
|
|
|
|
308
|
|
|
if (class_exists($modelClassName)) { |
309
|
|
|
$this->models->register($modelClassName, 'controller'); |
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// Initialize Controller |
313
|
|
|
if (profiler() !== false) { |
|
|
|
|
314
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if (profiler() !== false) { |
|
|
|
|
318
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
319
|
|
|
} |
320
|
|
|
$requestController = $controller->getInstance(); |
321
|
|
|
|
322
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
323
|
|
|
$requestController->__reconstruct(); |
324
|
|
|
} elseif (method_exists($requestController, 'initialize')) { |
325
|
|
|
$requestController->initialize(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$this->services->add($requestController, 'controller'); |
329
|
|
|
|
330
|
|
|
if (profiler() !== false) { |
|
|
|
|
331
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
$requestMethod = $controller->getRequestMethod(); |
335
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
336
|
|
|
|
337
|
|
|
// Call the requested controller method |
338
|
|
|
if (profiler() !== false) { |
|
|
|
|
339
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
340
|
|
|
} |
341
|
|
|
ob_start(); |
342
|
|
|
$requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
343
|
|
|
|
344
|
|
|
if (empty($requestControllerOutput)) { |
345
|
|
|
$requestControllerOutput = ob_get_contents(); |
346
|
|
|
ob_end_clean(); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
350
|
|
|
// Send default error 204 - No Content |
351
|
|
|
output()->sendError(204); |
352
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
353
|
|
|
if ($requestControllerOutput === true) { |
354
|
|
|
output()->sendError(200); |
355
|
|
|
} elseif ($requestControllerOutput === false) { |
|
|
|
|
356
|
|
|
output()->sendError(204); |
357
|
|
|
} |
358
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
359
|
|
|
$requestController->sendPayload($requestControllerOutput); |
360
|
|
|
} elseif (is_numeric($requestControllerOutput)) { |
361
|
|
|
output()->sendError($requestControllerOutput); |
362
|
|
|
} elseif (is_string($requestControllerOutput)) { |
363
|
|
|
if (is_json($requestControllerOutput)) { |
364
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
365
|
|
|
output()->send($requestControllerOutput); |
|
|
|
|
366
|
|
|
} elseif (is_serialized($requestControllerOutput)) { |
367
|
|
|
output()->send($requestControllerOutput); |
368
|
|
|
} else { |
369
|
|
|
output()->send($requestControllerOutput); |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
// Show Error (404) Page Not Found |
376
|
|
|
output()->sendError(404); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|