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/Framework.php'; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Class Framework |
150
|
|
|
* |
151
|
|
|
* @package O2System |
152
|
|
|
*/ |
153
|
|
|
class Framework extends Kernel |
154
|
|
|
{ |
155
|
|
|
/** |
156
|
|
|
* Framework Container Models |
157
|
|
|
* |
158
|
|
|
* @var Framework\Containers\Models |
159
|
|
|
*/ |
160
|
|
|
public $models; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Framework Container Modules |
164
|
|
|
* |
165
|
|
|
* @var Framework\Containers\Modules |
166
|
|
|
*/ |
167
|
|
|
public $modules; |
168
|
|
|
|
169
|
|
|
// ------------------------------------------------------------------------ |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Framework::__construct |
173
|
|
|
* |
174
|
|
|
* @return Framework |
175
|
|
|
*/ |
176
|
|
|
protected function __construct() |
177
|
|
|
{ |
178
|
|
|
parent::__construct(); |
179
|
|
|
|
180
|
|
|
if (profiler() !== false) { |
|
|
|
|
181
|
|
|
profiler()->watch('Starting O2System Framework'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Add App Views Folder |
185
|
|
|
output()->addFilePath(PATH_APP); |
186
|
|
|
|
187
|
|
|
$this->services->load(Framework\Services\Hooks::class); |
|
|
|
|
188
|
|
|
|
189
|
|
|
if (profiler() !== false) { |
|
|
|
|
190
|
|
|
profiler()->watch('Starting Framework Services'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
foreach (['Globals', 'Environment', 'Loader', 'Config'] as $serviceClassName) { |
194
|
|
|
if (class_exists('App\Kernel\Services\\' . $serviceClassName)) { |
195
|
|
|
$this->services->load('App\Kernel\Services\\' . $serviceClassName); |
196
|
|
|
} elseif (class_exists('O2System\Framework\Services\\' . $serviceClassName)) { |
197
|
|
|
$this->services->load('O2System\Framework\Services\\' . $serviceClassName); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Instantiate Models Container |
202
|
|
|
if (profiler() !== false) { |
|
|
|
|
203
|
|
|
profiler()->watch('Starting Models Container'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->models = new Framework\Containers\Models(); |
207
|
|
|
|
208
|
|
|
// Instantiate Cache Service |
209
|
|
|
if (profiler() !== false) { |
|
|
|
|
210
|
|
|
profiler()->watch('Starting Cache Service'); |
211
|
|
|
} |
212
|
|
|
$this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache'); |
|
|
|
|
213
|
|
|
|
214
|
|
|
// Instantiate Modules Container |
215
|
|
|
if (profiler() !== false) { |
|
|
|
|
216
|
|
|
profiler()->watch('Starting Modules Container'); |
217
|
|
|
} |
218
|
|
|
$this->modules = new Framework\Containers\Modules(); |
219
|
|
|
|
220
|
|
|
if (profiler() !== false) { |
|
|
|
|
221
|
|
|
profiler()->watch('Starting O2System Framework Hooks Pre System'); |
222
|
|
|
} |
223
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// ------------------------------------------------------------------------ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Framework::__reconstruct |
230
|
|
|
*/ |
231
|
|
|
protected function __reconstruct() |
232
|
|
|
{ |
233
|
|
|
// Modules default app |
234
|
|
|
if (null !== ($defaultApp = config('app'))) { |
|
|
|
|
235
|
|
|
if (false !== ($defaultModule = modules()->getApp($defaultApp))) { |
|
|
|
|
236
|
|
|
// Register Domain App Module Namespace |
237
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
238
|
|
|
|
239
|
|
|
// Push Domain App Module |
240
|
|
|
modules()->push($defaultModule); |
241
|
|
|
} elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) { |
|
|
|
|
242
|
|
|
// Register Path Module Namespace |
243
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
244
|
|
|
|
245
|
|
|
// Push Path Module |
246
|
|
|
modules()->push($defaultModule); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (profiler() !== false) { |
|
|
|
|
251
|
|
|
profiler()->watch('Calling Hooks Service: Post System'); |
252
|
|
|
} |
253
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM); |
254
|
|
|
|
255
|
|
|
if (is_cli()) { |
256
|
|
|
$this->cliHandler(); |
257
|
|
|
} else { |
258
|
|
|
$this->httpHandler(); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// ------------------------------------------------------------------------ |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Framework::cliHandler |
266
|
|
|
* |
267
|
|
|
* @return void |
268
|
|
|
*/ |
269
|
|
|
private function cliHandler() |
270
|
|
|
{ |
271
|
|
|
// Instantiate CLI Router Service |
272
|
|
|
$this->services->load(Framework\Cli\Router::class); |
|
|
|
|
273
|
|
|
|
274
|
|
|
if (profiler() !== false) { |
|
|
|
|
275
|
|
|
profiler()->watch('Parse Router Request'); |
276
|
|
|
} |
277
|
|
|
router()->parseRequest(); |
278
|
|
|
|
279
|
|
|
if ($commander = router()->getCommander()) { |
|
|
|
|
280
|
|
|
if ($commander instanceof Kernel\Cli\Router\Datastructures\Commander) { |
281
|
|
|
// Autoload Language |
282
|
|
|
language()->loadFile($commander->getParameter()); |
283
|
|
|
language()->loadFile($commander->getRequestMethod()); |
284
|
|
|
language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod()); |
285
|
|
|
|
286
|
|
|
// Autoload Model |
287
|
|
|
foreach ($this->modules as $module) { |
288
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
289
|
|
|
continue; |
290
|
|
|
} |
291
|
|
|
$module->loadModel(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Autoload Model |
295
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
296
|
|
|
|
297
|
|
|
if (class_exists($modelClassName)) { |
298
|
|
|
models()->load($modelClassName, 'commander'); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
// Initialize Controller |
302
|
|
|
if (profiler() !== false) { |
|
|
|
|
303
|
|
|
profiler()->watch('Calling Hooks Service: Pre Commander'); |
304
|
|
|
} |
305
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER); |
306
|
|
|
|
307
|
|
|
if (profiler() !== false) { |
|
|
|
|
308
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
309
|
|
|
} |
310
|
|
|
$requestCommander = $commander->getInstance(); |
311
|
|
|
|
312
|
|
|
if (profiler() !== false) { |
|
|
|
|
313
|
|
|
profiler()->watch('Calling Hooks Service: Post Commander'); |
314
|
|
|
} |
315
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER); |
316
|
|
|
|
317
|
|
|
if (profiler() !== false) { |
|
|
|
|
318
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
319
|
|
|
} |
320
|
|
|
$requestCommander->execute(); |
321
|
|
|
|
322
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// ------------------------------------------------------------------------ |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Framework::httpHandler |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
|
|
private function httpHandler() |
335
|
|
|
{ |
336
|
|
|
// Instantiate Http Router Service |
337
|
|
|
$this->services->load(Framework\Http\Router::class); |
|
|
|
|
338
|
|
|
|
339
|
|
|
if (profiler() !== false) { |
|
|
|
|
340
|
|
|
profiler()->watch('Parse Router Request'); |
341
|
|
|
} |
342
|
|
|
router()->parseRequest(); |
343
|
|
|
|
344
|
|
|
if (config()->loadFile('session') === true) { |
|
|
|
|
345
|
|
|
|
346
|
|
|
// Instantiate Session Service |
347
|
|
|
$session = new Session(config('session', true)); |
|
|
|
|
348
|
|
|
$session->setLogger($this->services->get('logger')); |
349
|
|
|
|
350
|
|
|
if ( ! $session->isStarted()) { |
351
|
|
|
$session->start(); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$this->services->add($session, 'session'); |
355
|
|
|
|
356
|
|
|
if ($session->has('language') and $this->services->has('language')) { |
357
|
|
|
language()->setDefault($session->get('language')); |
358
|
|
|
} else { |
359
|
|
|
$session->set('language', language()->getDefault()); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if (config('security')->protection[ 'csrf' ] === true) { |
|
|
|
|
363
|
|
|
$csrfProtection = new Security\Protections\Csrf(); |
364
|
|
|
$this->services->add($csrfProtection, 'csrfProtection'); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if (config('security')->protection[ 'xss' ] === true) { |
368
|
|
|
$csrfProtection = new Security\Protections\Xss(); |
|
|
|
|
369
|
|
|
$this->services->add($xssProtection, 'xssProtection'); |
|
|
|
|
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
if (config()->loadFile('view') === true) { |
374
|
|
|
// Instantiate Http UserAgent Service |
375
|
|
|
$this->services->load(Framework\Http\UserAgent::class); |
376
|
|
|
|
377
|
|
|
// Instantiate Http View Service |
378
|
|
|
$this->services->load(Framework\Http\Parser::class); |
379
|
|
|
|
380
|
|
|
// Instantiate Http View Service |
381
|
|
|
$this->services->load(Framework\Http\View::class); |
382
|
|
|
|
383
|
|
|
// Instantiate Http Presenter Service |
384
|
|
|
$this->services->load(Framework\Http\Presenter::class); |
385
|
|
|
presenter()->initialize(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
// Instantiate Http Middleware Service |
389
|
|
|
$this->services->load(Framework\Http\Middleware::class); |
390
|
|
|
|
391
|
|
|
if (profiler() !== false) { |
|
|
|
|
392
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
393
|
|
|
} |
394
|
|
|
middleware()->run(); |
395
|
|
|
|
396
|
|
|
if (false !== ($controller = $this->services->get('controller'))) { |
397
|
|
|
if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
398
|
|
|
$controllerParameter = dash($controller->getParameter()); |
399
|
|
|
$controllerRequestMethod = dash($controller->getRequestMethod()); |
400
|
|
|
|
401
|
|
|
// Autoload Language |
402
|
|
|
if ($this->services->has('language')) { |
403
|
|
|
language()->loadFile($controller->getParameter()); |
404
|
|
|
language()->loadFile($controller->getRequestMethod()); |
405
|
|
|
language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod()); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
// Autoload Model |
409
|
|
|
foreach ($this->modules as $module) { |
410
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
411
|
|
|
continue; |
412
|
|
|
} |
413
|
|
|
$module->loadModel(); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
// Autoload Model |
417
|
|
|
$modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName()); |
418
|
|
|
|
419
|
|
|
if (class_exists($modelClassName)) { |
420
|
|
|
$this->models->register('controller', new $modelClassName()); |
|
|
|
|
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// Autoload Assets |
424
|
|
|
if (config()->loadFile('view') === true) { |
425
|
|
|
$controllerAssets = []; |
426
|
|
|
$controllerAssetsDirs = []; |
427
|
|
|
|
428
|
|
|
// Autoload Assets |
429
|
|
|
foreach ($this->modules as $module) { |
430
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
431
|
|
|
continue; |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
$controllerAssets[] = $module->getParameter(); |
435
|
|
|
$controllerAssetsDirs[] = $module->getParameter(); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$controllerAssets = array_reverse($controllerAssets); |
439
|
|
|
$controllerAssetsDirs = array_reverse($controllerAssetsDirs); |
440
|
|
|
|
441
|
|
|
$controllerAssets[] = $controllerParameter; |
442
|
|
|
$controllerAssetsDirs[] = $controllerParameter; |
443
|
|
|
|
444
|
|
|
$controllerAssets[] = $controllerRequestMethod; |
445
|
|
|
|
446
|
|
|
foreach ($controllerAssetsDirs as $controllerAssetsDir) { |
447
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter; |
448
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerRequestMethod; |
449
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter . '/' . $controllerRequestMethod; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
// Autoload Presenter |
453
|
|
|
$presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName()); |
454
|
|
|
|
455
|
|
|
if (class_exists($presenterClassName)) { |
456
|
|
|
$presenterClassObject = new $presenterClassName(); |
457
|
|
|
if ($presenterClassObject instanceof Framework\Http\Presenter) { |
458
|
|
|
$this->services->add($presenterClassObject, 'presenter'); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
// Re-Initialize Presenter |
463
|
|
|
presenter()->initialize()->assets->loadFiles( |
|
|
|
|
464
|
|
|
[ |
465
|
|
|
'css' => $controllerAssets, |
466
|
|
|
'js' => $controllerAssets, |
467
|
|
|
] |
468
|
|
|
); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
// Initialize Controller |
472
|
|
|
if (profiler() !== false) { |
|
|
|
|
473
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER); |
477
|
|
|
|
478
|
|
|
if (profiler() !== false) { |
|
|
|
|
479
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
480
|
|
|
} |
481
|
|
|
$requestController = $controller->getInstance(); |
482
|
|
|
|
483
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
484
|
|
|
$requestController->__reconstruct(); |
485
|
|
|
} elseif (method_exists($requestController, 'initialize')) { |
486
|
|
|
$requestController->initialize(); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
$this->services->add($requestController, 'controller'); |
490
|
|
|
|
491
|
|
|
if (profiler() !== false) { |
|
|
|
|
492
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
493
|
|
|
} |
494
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER); |
495
|
|
|
|
496
|
|
|
$requestMethod = $controller->getRequestMethod(); |
497
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
498
|
|
|
|
499
|
|
|
// Call the requested controller method |
500
|
|
|
if (profiler() !== false) { |
|
|
|
|
501
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
502
|
|
|
} |
503
|
|
|
ob_start(); |
504
|
|
|
$requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
505
|
|
|
|
506
|
|
|
if (empty($requestControllerOutput)) { |
507
|
|
|
$requestControllerOutput = ob_get_contents(); |
508
|
|
|
ob_end_clean(); |
509
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
510
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
511
|
|
|
$requestController->sendError($requestControllerOutput); |
512
|
|
|
} else { |
513
|
|
|
if ($requestControllerOutput === true) { |
514
|
|
|
output()->sendError(200); |
515
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
516
|
|
|
} elseif ($requestControllerOutput === false) { |
|
|
|
|
517
|
|
|
output()->sendError(204); |
518
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
522
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
523
|
|
|
$requestController->sendPayload($requestControllerOutput); |
524
|
|
|
} else { |
525
|
|
|
output()->send($requestControllerOutput); |
|
|
|
|
526
|
|
|
} |
527
|
|
|
} elseif (is_numeric($requestControllerOutput)) { |
528
|
|
|
output()->sendError($requestControllerOutput); |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
532
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
533
|
|
|
$requestController->sendError(204); |
534
|
|
|
} elseif (config()->loadFile('view') === true) { |
535
|
|
|
$filenames = [ |
536
|
|
|
$controllerRequestMethod, |
537
|
|
|
$controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod, |
538
|
|
|
]; |
539
|
|
|
|
540
|
|
|
if ($controllerRequestMethod === 'index') { |
541
|
|
|
array_unshift($filenames, $controllerParameter); |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
foreach ($filenames as $filename) { |
545
|
|
|
if (view()->getFilePath($filename)) { |
546
|
|
|
view()->load($filename); |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
if (presenter()->partials->offsetExists('content')) { |
|
|
|
|
551
|
|
|
view()->render(); |
552
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
// Send default error 204 - No Content |
557
|
|
|
output()->sendError(204); |
558
|
|
|
} elseif (is_string($requestControllerOutput)) { |
559
|
|
|
if (is_json($requestControllerOutput)) { |
560
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
561
|
|
|
output()->send($requestControllerOutput); |
562
|
|
|
} elseif (is_serialized($requestControllerOutput)) { |
563
|
|
|
output()->send($requestControllerOutput); |
564
|
|
|
} elseif (config('presenter', true)->enabled === true) { |
|
|
|
|
565
|
|
|
presenter()->partials->offsetSet('content', $requestControllerOutput); |
566
|
|
|
view()->render(); |
567
|
|
|
} else { |
568
|
|
|
output()->send($requestControllerOutput); |
569
|
|
|
} |
570
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
// Show Error (404) Page Not Found |
576
|
|
|
output()->sendError(404); |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
|