1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System 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
|
|
|
|
27
|
|
|
use O2System\Kernel\Http\Message\Uri; |
28
|
|
|
|
29
|
|
|
switch (strtoupper(ENVIRONMENT)) { |
|
|
|
|
30
|
|
|
case 'DEVELOPMENT': |
31
|
|
|
error_reporting(-1); |
32
|
|
|
ini_set('display_errors', 1); |
33
|
|
|
break; |
34
|
|
|
case 'TESTING': |
35
|
|
|
case 'PRODUCTION': |
36
|
|
|
ini_set('display_errors', 0); |
37
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
header('HTTP/1.1 503 Service Unavailable.', true, 503); |
41
|
|
|
echo 'The application environment is not set correctly.'; |
42
|
|
|
exit(1); // EXIT_ERROR |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
*--------------------------------------------------------------- |
47
|
|
|
* VENDOR PATH |
48
|
|
|
*--------------------------------------------------------------- |
49
|
|
|
* |
50
|
|
|
* RealPath to vendor folder. |
51
|
|
|
* |
52
|
|
|
* WITH TRAILING SLASH! |
53
|
|
|
*/ |
54
|
|
|
if ( ! defined('PATH_VENDOR')) { |
55
|
|
|
define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
*--------------------------------------------------------------- |
60
|
|
|
* FRAMEWORK PATH |
61
|
|
|
*--------------------------------------------------------------- |
62
|
|
|
* |
63
|
|
|
* RealPath to framework folder. |
64
|
|
|
* |
65
|
|
|
* WITH TRAILING SLASH! |
66
|
|
|
*/ |
67
|
|
|
if ( ! defined('PATH_FRAMEWORK')) { |
68
|
|
|
define('PATH_FRAMEWORK', __DIR__ . DIRECTORY_SEPARATOR); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/* |
72
|
|
|
*--------------------------------------------------------------- |
73
|
|
|
* APP PATH |
74
|
|
|
*--------------------------------------------------------------- |
75
|
|
|
* |
76
|
|
|
* RealPath to application folder. |
77
|
|
|
* |
78
|
|
|
* WITH TRAILING SLASH! |
79
|
|
|
*/ |
80
|
|
|
if ( ! defined('PATH_APP')) { |
81
|
|
|
define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/* |
85
|
|
|
*--------------------------------------------------------------- |
86
|
|
|
* PUBLIC PATH |
87
|
|
|
*--------------------------------------------------------------- |
88
|
|
|
* |
89
|
|
|
* RealPath to public folder. |
90
|
|
|
* |
91
|
|
|
* WITH TRAILING SLASH! |
92
|
|
|
*/ |
93
|
|
|
if ( ! defined('PATH_PUBLIC')) { |
94
|
|
|
define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
*--------------------------------------------------------------- |
99
|
|
|
* CACHE PATH |
100
|
|
|
*--------------------------------------------------------------- |
101
|
|
|
* |
102
|
|
|
* RealPath to writable caching folder. |
103
|
|
|
* |
104
|
|
|
* WITH TRAILING SLASH! |
105
|
|
|
*/ |
106
|
|
|
if ( ! defined('PATH_CACHE')) { |
107
|
|
|
define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* |
111
|
|
|
*--------------------------------------------------------------- |
112
|
|
|
* STORAGE PATH |
113
|
|
|
*--------------------------------------------------------------- |
114
|
|
|
* |
115
|
|
|
* RealPath to writable storage folder. |
116
|
|
|
* |
117
|
|
|
* WITH TRAILING SLASH! |
118
|
|
|
*/ |
119
|
|
|
if ( ! defined('PATH_STORAGE')) { |
120
|
|
|
define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/* |
124
|
|
|
*--------------------------------------------------------------- |
125
|
|
|
* RESOURCES PATH |
126
|
|
|
*--------------------------------------------------------------- |
127
|
|
|
* |
128
|
|
|
* RealPath to writable resources folder. |
129
|
|
|
* |
130
|
|
|
* WITH TRAILING SLASH! |
131
|
|
|
*/ |
132
|
|
|
if ( ! defined('PATH_RESOURCES')) { |
133
|
|
|
define('PATH_RESOURCES', PATH_ROOT . DIR_RESOURCES . DIRECTORY_SEPARATOR); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/* |
137
|
|
|
*--------------------------------------------------------------- |
138
|
|
|
* DATABASE PATH |
139
|
|
|
*--------------------------------------------------------------- |
140
|
|
|
* |
141
|
|
|
* RealPath to writable database folder. |
142
|
|
|
* |
143
|
|
|
* WITH TRAILING SLASH! |
144
|
|
|
*/ |
145
|
|
|
if ( ! defined('PATH_DATABASE')) { |
146
|
|
|
define('PATH_DATABASE', PATH_ROOT . DIR_DATABASE . DIRECTORY_SEPARATOR); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/* |
150
|
|
|
*--------------------------------------------------------------- |
151
|
|
|
* FRAMEWORK CONSTANTS |
152
|
|
|
*--------------------------------------------------------------- |
153
|
|
|
*/ |
154
|
|
|
require __DIR__ . '/Config/Constants.php'; |
155
|
|
|
|
156
|
|
|
/* |
157
|
|
|
*--------------------------------------------------------------- |
158
|
|
|
* FRAMEWORK HELPERS |
159
|
|
|
*--------------------------------------------------------------- |
160
|
|
|
*/ |
161
|
|
|
require __DIR__ . '/Helpers/Framework.php'; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Class Framework |
165
|
|
|
* |
166
|
|
|
* @package O2System |
167
|
|
|
*/ |
168
|
|
|
class Framework extends Kernel |
169
|
|
|
{ |
170
|
|
|
/** |
171
|
|
|
* Framework::$config |
172
|
|
|
* |
173
|
|
|
* Framework Container Config |
174
|
|
|
* |
175
|
|
|
* @var Framework\Containers\Config |
176
|
|
|
*/ |
177
|
|
|
public $config; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Framework::$globals |
181
|
|
|
* |
182
|
|
|
* Framework Container Globals |
183
|
|
|
* |
184
|
|
|
* @var Framework\Containers\Globals |
185
|
|
|
*/ |
186
|
|
|
public $globals; |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Framework::$environment |
190
|
|
|
* |
191
|
|
|
* Framework Container Environment |
192
|
|
|
* |
193
|
|
|
* @var Framework\Containers\Environment |
194
|
|
|
*/ |
195
|
|
|
public $environment; |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Framework::$models |
199
|
|
|
* |
200
|
|
|
* Framework Container Models |
201
|
|
|
* |
202
|
|
|
* @var Framework\Containers\Models |
203
|
|
|
*/ |
204
|
|
|
public $models; |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Framework::$modules |
208
|
|
|
* |
209
|
|
|
* Framework Container Modules |
210
|
|
|
* |
211
|
|
|
* @var Framework\Containers\Modules |
212
|
|
|
*/ |
213
|
|
|
public $modules; |
214
|
|
|
|
215
|
|
|
// ------------------------------------------------------------------------ |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Framework::__construct |
219
|
|
|
*/ |
220
|
|
|
protected function __construct() |
221
|
|
|
{ |
222
|
|
|
parent::__construct(); |
223
|
|
|
|
224
|
|
|
if (profiler() !== false) { |
225
|
|
|
profiler()->watch('Starting O2System Framework Hooks Pre System'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$this->services->load(Framework\Services\Hooks::class, 'hooks'); |
229
|
|
|
|
230
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM); |
231
|
|
|
|
232
|
|
|
if (profiler() !== false) { |
233
|
|
|
profiler()->watch('Starting O2System Framework'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Add App Views Folder |
237
|
|
|
output()->addFilePath(PATH_APP); |
238
|
|
|
|
239
|
|
|
if (profiler() !== false) { |
240
|
|
|
profiler()->watch('Starting Framework Services'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$services = [ |
244
|
|
|
'Services\Shutdown' => 'shutdown', |
245
|
|
|
'Services\Logger' => 'logger', |
246
|
|
|
'Services\Loader' => 'loader', |
247
|
|
|
]; |
248
|
|
|
|
249
|
|
|
foreach ($services as $className => $classOffset) { |
250
|
|
|
$this->services->load($className, $classOffset); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
// Instantiate Config Container |
254
|
|
|
if (profiler() !== false) { |
255
|
|
|
profiler()->watch('Starting Config Container'); |
256
|
|
|
} |
257
|
|
|
$this->config = new Framework\Containers\Config(); |
258
|
|
|
|
259
|
|
|
// Instantiate Globals Container |
260
|
|
|
if (profiler() !== false) { |
261
|
|
|
profiler()->watch('Starting Globals Container'); |
262
|
|
|
} |
263
|
|
|
$this->globals = new Framework\Containers\Globals(); |
264
|
|
|
|
265
|
|
|
// Instantiate Environment Container |
266
|
|
|
if (profiler() !== false) { |
267
|
|
|
profiler()->watch('Starting Environment Container'); |
268
|
|
|
} |
269
|
|
|
$this->environment = new Framework\Containers\Environment(); |
270
|
|
|
|
271
|
|
|
// Instantiate Models Container |
272
|
|
|
if (profiler() !== false) { |
273
|
|
|
profiler()->watch('Starting Models Container'); |
274
|
|
|
} |
275
|
|
|
$this->models = new Framework\Containers\Models(); |
276
|
|
|
|
277
|
|
|
// Instantiate Modules Container |
278
|
|
|
if (profiler() !== false) { |
279
|
|
|
profiler()->watch('Starting Modules Container'); |
280
|
|
|
} |
281
|
|
|
$this->modules = new Framework\Containers\Modules(); |
282
|
|
|
|
283
|
|
|
if (config()->loadFile('cache') === true) { |
|
|
|
|
284
|
|
|
// Instantiate Cache Service |
285
|
|
|
if (profiler() !== false) { |
286
|
|
|
profiler()->watch('Starting Cache Service'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache'); |
|
|
|
|
290
|
|
|
|
291
|
|
|
// Language Service Load Registry |
292
|
|
|
if (profiler() !== false) { |
293
|
|
|
profiler()->watch('Loading Language Registry'); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
language()->loadRegistry(); |
|
|
|
|
297
|
|
|
|
298
|
|
|
// Modules Service Load Registry |
299
|
|
|
if (profiler() !== false) { |
300
|
|
|
profiler()->watch('Loading Modules Registry'); |
301
|
|
|
} |
302
|
|
|
$this->modules->loadRegistry(); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
// ------------------------------------------------------------------------ |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Framework::__reconstruct |
310
|
|
|
*/ |
311
|
|
|
protected function __reconstruct() |
312
|
|
|
{ |
313
|
|
|
// Modules default app |
314
|
|
|
if (null !== ($defaultApp = config('app'))) { |
|
|
|
|
315
|
|
|
if (false !== ($defaultModule = modules()->getApp($defaultApp))) { |
|
|
|
|
316
|
|
|
// Register Domain App Module Namespace |
317
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
318
|
|
|
|
319
|
|
|
// Push Domain App Module |
320
|
|
|
modules()->push($defaultModule); |
|
|
|
|
321
|
|
|
} elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) { |
|
|
|
|
322
|
|
|
// Register Path Module Namespace |
323
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
324
|
|
|
|
325
|
|
|
// Push Path Module |
326
|
|
|
modules()->push($defaultModule); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
if (profiler() !== false) { |
331
|
|
|
profiler()->watch('Calling Hooks Service: Post System'); |
332
|
|
|
} |
333
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM); |
334
|
|
|
|
335
|
|
|
if (is_cli()) { |
336
|
|
|
$this->cliHandler(); |
337
|
|
|
} else { |
338
|
|
|
$this->httpHandler(); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
// ------------------------------------------------------------------------ |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Framework::cliHandler |
346
|
|
|
* |
347
|
|
|
* @return void |
348
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
349
|
|
|
* @throws \ReflectionException |
350
|
|
|
*/ |
351
|
|
|
private function cliHandler() |
352
|
|
|
{ |
353
|
|
|
// Instantiate CLI Router Service |
354
|
|
|
$this->services->load(Kernel\Cli\Router::class); |
355
|
|
|
|
356
|
|
|
if (profiler() !== false) { |
357
|
|
|
profiler()->watch('Parse Router Request'); |
358
|
|
|
} |
359
|
|
|
router()->handle(); |
360
|
|
|
|
361
|
|
|
if ($commander = router()->getCommander()) { |
|
|
|
|
362
|
|
|
if ($commander instanceof Kernel\Cli\Router\DataStructures\Commander) { |
|
|
|
|
363
|
|
|
// Autoload Language |
364
|
|
|
language()->loadFile($commander->getParameter()); |
365
|
|
|
language()->loadFile($commander->getRequestMethod()); |
366
|
|
|
language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod()); |
367
|
|
|
|
368
|
|
|
$modules = $this->modules->getArrayCopy(); |
369
|
|
|
|
370
|
|
|
// Run Module Autoloader |
371
|
|
|
foreach ($modules as $module) { |
372
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
373
|
|
|
continue; |
374
|
|
|
} |
375
|
|
|
$module->loadModel(); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
// Autoload Model |
379
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
380
|
|
|
|
381
|
|
|
if (class_exists($modelClassName)) { |
382
|
|
|
$this->models->load($modelClassName, 'commander'); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
// Initialize Commander |
386
|
|
|
if (profiler() !== false) { |
387
|
|
|
profiler()->watch('Calling Hooks Service: Pre Commander'); |
388
|
|
|
} |
389
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER); |
390
|
|
|
|
391
|
|
|
if (profiler() !== false) { |
392
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
393
|
|
|
} |
394
|
|
|
$requestCommander = $commander->getInstance(); |
395
|
|
|
|
396
|
|
|
if (profiler() !== false) { |
397
|
|
|
profiler()->watch('Calling Hooks Service: Post Commander'); |
398
|
|
|
} |
399
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER); |
400
|
|
|
|
401
|
|
|
if (profiler() !== false) { |
402
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
$requestCommander->__call($commander->getRequestMethod()); |
406
|
|
|
|
407
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// ------------------------------------------------------------------------ |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Framework::httpHandler |
416
|
|
|
* |
417
|
|
|
* @return void |
418
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
419
|
|
|
* @throws \ReflectionException |
420
|
|
|
*/ |
421
|
|
|
private function httpHandler() |
422
|
|
|
{ |
423
|
|
|
if (config()->loadFile('view') === true) { |
424
|
|
|
// Instantiate Http UserAgent Service |
425
|
|
|
$this->services->load(Framework\Http\UserAgent::class, 'userAgent'); |
426
|
|
|
|
427
|
|
|
// Instantiate Http View Service |
428
|
|
|
$this->services->load(Framework\Http\Parser::class); |
429
|
|
|
|
430
|
|
|
// Instantiate Http View Service |
431
|
|
|
$this->services->load(Framework\Http\View::class); |
432
|
|
|
|
433
|
|
|
// Instantiate Http Presenter Service |
434
|
|
|
$this->services->load(Framework\Http\Presenter::class); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
// Instantiate Http Router Service |
438
|
|
|
$this->services->load(Framework\Http\Router::class); |
439
|
|
|
|
440
|
|
|
if (profiler() !== false) { |
441
|
|
|
profiler()->watch('Parse Router Request'); |
442
|
|
|
} |
443
|
|
|
router()->handle(new Uri()); |
|
|
|
|
444
|
|
|
|
445
|
|
|
if (config()->loadFile('session') === true) { |
446
|
|
|
|
447
|
|
|
// Instantiate Session Service |
448
|
|
|
$session = new Session(config('session', true)); |
|
|
|
|
449
|
|
|
$session->setLogger($this->services->get('logger')); |
450
|
|
|
|
451
|
|
|
if ( ! $session->isStarted()) { |
452
|
|
|
$session->start(); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$this->services->add($session, 'session'); |
456
|
|
|
|
457
|
|
|
if ($session->has('language') and $this->services->has('language')) { |
458
|
|
|
language()->setDefault($session->get('language')); |
|
|
|
|
459
|
|
|
} else { |
460
|
|
|
$session->set('language', language()->getDefault()); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
if (config('security')->protection[ 'csrf' ] === true) { |
|
|
|
|
464
|
|
|
$csrfProtection = new Security\Protections\Csrf(); |
465
|
|
|
$this->services->add($csrfProtection, 'csrfProtection'); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
if (config('security')->protection[ 'xss' ] === true) { |
469
|
|
|
$xssProtection = new Security\Protections\Xss(); |
470
|
|
|
$this->services->add($xssProtection, 'xssProtection'); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
// Instantiate Http Middleware Service |
475
|
|
|
$this->services->load(Framework\Http\Middleware::class); |
476
|
|
|
|
477
|
|
|
if (profiler() !== false) { |
478
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
479
|
|
|
} |
480
|
|
|
middleware()->run(); |
481
|
|
|
|
482
|
|
|
if ($this->services->has('controller')) { |
483
|
|
|
$controller = $this->services->get('controller'); |
484
|
|
|
|
485
|
|
|
$controllerParameter = dash($controller->getParameter()); |
486
|
|
|
$controllerRequestMethod = dash($controller->getRequestMethod()); |
487
|
|
|
|
488
|
|
|
$modules = $this->modules->getArrayCopy(); |
489
|
|
|
|
490
|
|
|
// Run Module Autoloader |
491
|
|
|
foreach ($modules as $module) { |
492
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
493
|
|
|
continue; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
// Autoload Module Language |
497
|
|
|
if ($this->services->has('language')) { |
498
|
|
|
language()->loadFile($module->getParameter()); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
// Autoload Module Model |
502
|
|
|
$module->loadModel(); |
503
|
|
|
|
504
|
|
|
// Add View Resource Directory |
505
|
|
|
if($this->services->has('view')) { |
506
|
|
|
view()->addFilePath($module->getResourcesDir()); |
507
|
|
|
presenter()->assets->pushFilePath($module->getResourcesDir()); |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
if ($this->services->has('view')) { |
512
|
|
|
presenter()->initialize(); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
// Autoload Language |
516
|
|
|
if ($this->services->has('language')) { |
517
|
|
|
language()->loadFile($controller->getParameter()); |
518
|
|
|
language()->loadFile($controller->getRequestMethod()); |
519
|
|
|
language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod()); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
// Autoload Model |
523
|
|
|
$modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName()); |
524
|
|
|
|
525
|
|
|
if (class_exists($modelClassName)) { |
526
|
|
|
$this->models->load($modelClassName, 'controller'); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
if ($this->services->has('view')) { |
530
|
|
|
// Autoload Presenter |
531
|
|
|
$presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName()); |
532
|
|
|
|
533
|
|
|
if (class_exists($presenterClassName)) { |
534
|
|
|
$presenterClassObject = new $presenterClassName(); |
535
|
|
|
if ($presenterClassObject instanceof Framework\Http\Presenter) { |
536
|
|
|
$this->services->add($presenterClassObject, 'presenter'); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
// Initialize Controller |
542
|
|
|
if (profiler() !== false) { |
543
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
544
|
|
|
} |
545
|
|
|
|
546
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER); |
547
|
|
|
|
548
|
|
|
if (profiler() !== false) { |
549
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
$requestController = $controller->getInstance(); |
553
|
|
|
|
554
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
555
|
|
|
$requestController->__reconstruct(); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
if (profiler() !== false) { |
559
|
|
|
profiler()->watch('Calling Hooks Service: Post Controller'); |
560
|
|
|
} |
561
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER); |
562
|
|
|
|
563
|
|
|
if (profiler() !== false) { |
564
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
565
|
|
|
} |
566
|
|
|
middleware()->run(); |
567
|
|
|
|
568
|
|
|
$requestMethod = $controller->getRequestMethod(); |
569
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
570
|
|
|
|
571
|
|
|
// Call the requested controller method |
572
|
|
|
if (profiler() !== false) { |
573
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
ob_start(); |
577
|
|
|
$requestController->__call($requestMethod, $requestMethodArgs); |
578
|
|
|
$requestControllerOutput = ob_get_contents(); |
579
|
|
|
ob_end_clean(); |
580
|
|
|
|
581
|
|
|
if (is_numeric($requestControllerOutput)) { |
582
|
|
|
output()->sendError($requestControllerOutput); |
|
|
|
|
583
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
|
|
|
|
584
|
|
|
if ($requestControllerOutput === true) { |
585
|
|
|
output()->sendError(200); |
586
|
|
|
} elseif ($requestControllerOutput === false) { |
587
|
|
|
output()->sendError(204); |
588
|
|
|
} |
589
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
|
|
|
|
590
|
|
|
output()->sendPayload($requestControllerOutput); |
591
|
|
|
} elseif ($requestController instanceof Framework\Http\Controllers\Restful) { |
592
|
|
|
if (empty($requestControllerOutput)) { |
593
|
|
|
$requestController->sendError(204); |
594
|
|
|
} elseif (is_string($requestControllerOutput)) { |
|
|
|
|
595
|
|
|
if (is_json($requestControllerOutput)) { |
596
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
597
|
|
|
} else { |
598
|
|
|
output()->setContentType('text/plain'); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
echo $requestControllerOutput; |
602
|
|
|
} |
603
|
|
|
} elseif (is_string($requestControllerOutput)) { |
|
|
|
|
604
|
|
|
if (is_json($requestControllerOutput)) { |
605
|
|
|
output()->setContentType('application/json'); |
606
|
|
|
echo $requestControllerOutput; |
607
|
|
|
} elseif ($this->services->has('view')) { |
608
|
|
|
if (empty($requestControllerOutput)) { |
609
|
|
|
$filenames = [ |
610
|
|
|
$controllerRequestMethod, |
611
|
|
|
$controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod, |
612
|
|
|
]; |
613
|
|
|
|
614
|
|
|
if ($controllerRequestMethod === 'index') { |
615
|
|
|
array_unshift($filenames, $controllerParameter); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
foreach ($filenames as $filename) { |
619
|
|
|
if (false !== ($filePath = view()->getFilePath($filename))) { |
620
|
|
|
view()->load($filePath); |
621
|
|
|
break; |
622
|
|
|
} |
623
|
|
|
} |
624
|
|
|
} else { |
625
|
|
|
presenter()->partials->offsetSet('content', $requestControllerOutput); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
if (presenter()->partials->offsetExists('content')) { |
629
|
|
|
if(is_ajax()) { |
630
|
|
|
echo presenter()->partials->content; |
|
|
|
|
631
|
|
|
} else { |
632
|
|
|
$htmlOutput = view()->render(); |
633
|
|
|
|
634
|
|
|
if (empty($htmlOutput)) { |
635
|
|
|
output()->sendError(204); |
636
|
|
|
} else { |
637
|
|
|
output()->setContentType('text/html'); |
638
|
|
|
output()->send($htmlOutput); |
|
|
|
|
639
|
|
|
} |
640
|
|
|
} |
641
|
|
|
} else { |
642
|
|
|
output()->sendError(204); |
643
|
|
|
} |
644
|
|
|
} elseif (empty($requestControllerOutput) or $requestControllerOutput === '') { |
645
|
|
|
output()->sendError(204); |
646
|
|
|
} else { |
647
|
|
|
output()->setContentType('text/plain'); |
648
|
|
|
output()->send($requestControllerOutput); |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
} else { |
652
|
|
|
// Show Error (404) Page Not Found |
653
|
|
|
output()->sendError(404); |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
} |
657
|
|
|
|