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\Framework\Http; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Framework\Containers\Modules\DataStructures\Module as FrameworkModuleDataStructure; |
19
|
|
|
use O2System\Kernel\Http\Message\Uri as KernelMessageUri; |
20
|
|
|
use O2System\Kernel\Http\Message\Uri\Segments as KernelMessageUriSegments; |
21
|
|
|
use O2System\Kernel\Http\Router as KernelRouter; |
22
|
|
|
use O2System\Kernel\Http\Router\Addresses as KernelAddresses; |
23
|
|
|
use O2System\Kernel\Http\Router\DataStructures\Action as KernelActionDataStructure; |
24
|
|
|
use O2System\Kernel\Http\Router\DataStructures\Controller as KernelControllerDataStructure; |
25
|
|
|
use O2System\Spl\Info\SplFileInfo; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Router |
29
|
|
|
* |
30
|
|
|
* @package O2System |
31
|
|
|
*/ |
32
|
|
|
class Router extends KernelRouter |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Router::handle |
36
|
|
|
* |
37
|
|
|
* @param KernelMessageUri|null $uri |
38
|
|
|
* |
39
|
|
|
* @return bool |
40
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
41
|
|
|
* @throws \ReflectionException |
42
|
|
|
*/ |
43
|
|
|
public function handle(KernelMessageUri $uri = null) |
44
|
|
|
{ |
45
|
|
|
$this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
46
|
|
|
|
47
|
|
|
// Handle Extension Request |
48
|
|
|
if ($this->uri->segments->count()) { |
49
|
|
|
$this->handleExtensionRequest(); |
50
|
|
|
} else { |
51
|
|
|
$uriPath = urldecode( |
52
|
|
|
parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$uriPathParts = explode('public/', $uriPath); |
56
|
|
|
$uriPath = end($uriPathParts); |
57
|
|
|
|
58
|
|
|
if ($uriPath !== '/') { |
59
|
|
|
$this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
60
|
|
|
array_filter(explode('/', $uriPath))) |
|
|
|
|
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
unset($uriPathParts, $uriPath); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Load app addresses config |
68
|
|
|
$this->addresses = config()->loadFile('addresses', true); |
|
|
|
|
69
|
|
|
|
70
|
|
|
if ($this->addresses instanceof KernelAddresses) { |
71
|
|
|
// Domain routing |
72
|
|
|
if (null !== ($domain = $this->addresses->getDomain())) { |
73
|
|
|
if (is_array($domain)) { |
74
|
|
|
$this->uri->segments->exchangeArray( |
75
|
|
|
array_merge($domain, $this->uri->segments->getArrayCopy()) |
76
|
|
|
); |
77
|
|
|
$domain = $this->uri->segments->first(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (false !== ($app = modules()->getApp($domain))) { |
|
|
|
|
81
|
|
|
$this->registerModule($app); |
|
|
|
|
82
|
|
|
} elseif (false !== ($module = modules()->getModule($domain))) { |
|
|
|
|
83
|
|
|
$this->registerModule($module); |
84
|
|
|
} |
85
|
|
|
} elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
86
|
|
|
if (false !== ($app = modules()->getApp($subdomain))) { |
|
|
|
|
87
|
|
|
$this->registerModule($app); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// App and Module routing |
93
|
|
|
if ($numOfUriSegments = $this->uri->segments->count()) { |
|
|
|
|
94
|
|
|
if (empty($app)) { |
95
|
|
|
if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
96
|
|
|
$this->registerModule($module); |
97
|
|
|
|
98
|
|
|
if ($module->getType() === 'APP') { |
99
|
|
|
$this->uri->segments->shift(); |
100
|
|
|
$this->handleAppRequest($module); |
|
|
|
|
101
|
|
|
} else { |
102
|
|
|
$this->handleSegmentsRequest(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} elseif (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
106
|
|
|
$this->registerModule($module); |
107
|
|
|
|
108
|
|
|
if ($module->getType() === 'APP') { |
109
|
|
|
$this->uri->segments->shift(); |
110
|
|
|
$this->handleAppRequest($module); |
111
|
|
|
} else { |
112
|
|
|
$this->handleSegmentsRequest(); |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
$this->handleAppRequest($app); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Try to translate from uri string |
120
|
|
|
if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
|
|
|
|
121
|
|
|
if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
122
|
|
|
output()->sendError(405); |
123
|
|
|
} else { |
124
|
|
|
// Checks if action closure is an array |
125
|
|
|
if (is_array($closureSegments = $action->getClosure())) { |
126
|
|
|
$this->uri->segments->exchangeArray($closureSegments); |
127
|
|
|
|
128
|
|
|
if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
129
|
|
|
$this->registerModule($module); |
130
|
|
|
|
131
|
|
|
if ($module->getType() === 'APP') { |
132
|
|
|
$this->uri->segments->shift(); |
133
|
|
|
$this->handleAppRequest($module); |
134
|
|
|
} else { |
135
|
|
|
$this->handleSegmentsRequest(); |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$this->handleSegmentsRequest(); |
139
|
|
|
} |
140
|
|
|
} else { |
141
|
|
|
if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
142
|
|
|
$uriSegments = $parseSegments; |
143
|
|
|
} else { |
144
|
|
|
$uriSegments = []; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
|
|
|
|
148
|
|
|
$uriString = $this->uri->segments->__toString(); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$this->parseAction($action, $uriSegments); |
|
|
|
|
151
|
|
|
if ( ! empty(services()->has('controller'))) { |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Try to get route from controller & page |
159
|
|
|
if ($numOfUriSegments = $this->uri->segments->count()) { |
160
|
|
|
$uriSegments = $this->uri->segments->getArrayCopy(); |
161
|
|
|
$uriString = $this->uri->segments->__toString(); |
162
|
|
|
|
163
|
|
|
for ($i = 0; $i <= $numOfUriSegments; $i++) { |
164
|
|
|
$uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
165
|
|
|
$modules = modules()->getArrayCopy(); |
|
|
|
|
166
|
|
|
|
167
|
|
|
foreach ($modules as $module) { |
168
|
|
|
$controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
169
|
|
|
|
170
|
|
|
if ($module->getNamespace() === 'O2System\Framework\\') { |
171
|
|
|
$controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Try to find requested controller |
176
|
|
|
*/ |
177
|
|
|
if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
178
|
|
|
array_map('studlycase', $uriRoutedSegments)))) { |
179
|
|
|
|
180
|
|
|
if ($controllerClassName::$inherited) { |
|
|
|
|
181
|
|
|
$uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
182
|
|
|
$this->setController(new KernelControllerDataStructure($controllerClassName), |
183
|
|
|
$uriSegments); |
184
|
|
|
|
185
|
|
|
break; |
186
|
|
|
} else { |
187
|
|
|
$uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
188
|
|
|
$this->setController(new KernelControllerDataStructure($controllerClassName), |
189
|
|
|
$uriSegments); |
190
|
|
|
|
191
|
|
|
break; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Try to find requested page |
197
|
|
|
*/ |
198
|
|
|
if (false !== ($pagesDir = $module->getResourcesDir('pages'))) { |
199
|
|
|
if ($controllerClassName = $this->getPagesControllerClassName()) { |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Try to find from database |
203
|
|
|
*/ |
204
|
|
|
$modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
205
|
|
|
|
206
|
|
|
if (class_exists($modelClassName)) { |
207
|
|
|
models()->load($modelClassName, 'controller'); |
|
|
|
|
208
|
|
|
|
209
|
|
|
if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
|
|
|
|
210
|
|
|
if (isset($page->content)) { |
|
|
|
|
211
|
|
|
presenter()->partials->offsetSet('content', $page->content); |
212
|
|
|
|
213
|
|
|
$this->setController( |
214
|
|
|
(new KernelControllerDataStructure($controllerClassName)) |
215
|
|
|
->setRequestMethod('index') |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
return true; |
219
|
|
|
break; |
|
|
|
|
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Try to find from page file |
226
|
|
|
*/ |
227
|
|
|
foreach (['.phtml', '.vue'] as $pageExtension) { |
228
|
|
|
$pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
229
|
|
|
array_map('dash', $uriRoutedSegments)) . $pageExtension; |
230
|
|
|
|
231
|
|
|
if (is_file($pageFilePath)) { |
232
|
|
|
presenter()->page->setFile($pageFilePath); |
233
|
|
|
break; |
234
|
|
|
} else { |
235
|
|
|
$pageFilePath = str_replace($pageExtension, |
236
|
|
|
DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath); |
237
|
|
|
if (is_file($pageFilePath)) { |
238
|
|
|
presenter()->page->setFile($pageFilePath); |
239
|
|
|
break; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (presenter()->page->file instanceof SplFileInfo) { |
245
|
|
|
$this->setController( |
246
|
|
|
(new KernelControllerDataStructure($controllerClassName)) |
247
|
|
|
->setRequestMethod('index') |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
return true; |
251
|
|
|
break; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// break the loop if the controller has been set |
258
|
|
|
if (services()->has('controller')) { |
259
|
|
|
return true; |
260
|
|
|
break; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) { |
|
|
|
|
266
|
|
|
$this->setController(new KernelControllerDataStructure($controllerClassName), |
267
|
|
|
$this->uri->segments->getArrayCopy()); |
268
|
|
|
|
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// Let's the framework do the rest when there is no controller found |
273
|
|
|
// the framework will redirect to PAGE 404 |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// ------------------------------------------------------------------------ |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Router::handleExtensionRequest |
280
|
|
|
*/ |
281
|
|
|
protected function handleExtensionRequest() |
282
|
|
|
{ |
283
|
|
|
$lastSegment = $this->uri->segments->last(); |
284
|
|
|
|
285
|
|
|
if (strpos($lastSegment, '.json') !== false) { |
286
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
287
|
|
|
$lastSegment = str_replace('.json', '', $lastSegment); |
288
|
|
|
$this->uri->segments->pop(); |
289
|
|
|
$this->uri->segments->push($lastSegment); |
290
|
|
|
} elseif (strpos($lastSegment, '.xml') !== false) { |
291
|
|
|
output()->setContentType('application/xml'); |
292
|
|
|
$lastSegment = str_replace('.xml', '', $lastSegment); |
293
|
|
|
$this->uri->segments->pop(); |
294
|
|
|
$this->uri->segments->push($lastSegment); |
295
|
|
|
} elseif (strpos($lastSegment, '.js') !== false) { |
296
|
|
|
output()->setContentType('application/x-javascript'); |
297
|
|
|
$lastSegment = str_replace('.js', '', $lastSegment); |
298
|
|
|
$this->uri->segments->pop(); |
299
|
|
|
$this->uri->segments->push($lastSegment); |
300
|
|
|
} elseif (strpos($lastSegment, '.css') !== false) { |
301
|
|
|
output()->setContentType('text/css'); |
302
|
|
|
$lastSegment = str_replace('.css', '', $lastSegment); |
303
|
|
|
$this->uri->segments->pop(); |
304
|
|
|
$this->uri->segments->push($lastSegment); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
// ------------------------------------------------------------------------ |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Router::handleAppRequest |
312
|
|
|
* |
313
|
|
|
* @param \O2System\Framework\Containers\Modules\DataStructures\Module $app |
314
|
|
|
*/ |
315
|
|
|
public function handleAppRequest(FrameworkModuleDataStructure $app) |
316
|
|
|
{ |
317
|
|
|
// Find App module |
318
|
|
|
foreach([null,'modules', 'plugins'] as $additionalSegment) { |
319
|
|
|
if(empty($additionalSegment)) { |
320
|
|
|
$segments = [ |
321
|
|
|
$app->getParameter(), |
322
|
|
|
$this->uri->segments->first(), |
323
|
|
|
]; |
324
|
|
|
} else { |
325
|
|
|
$segments = [ |
326
|
|
|
$app->getParameter(), |
327
|
|
|
$additionalSegment, |
328
|
|
|
$this->uri->segments->first(), |
329
|
|
|
]; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (false !== ($module = modules()->getModule($segments))) { |
333
|
|
|
$this->uri->segments->shift(); |
334
|
|
|
|
335
|
|
|
$this->registerModule($module); |
|
|
|
|
336
|
|
|
$this->handleSegmentsRequest(); |
337
|
|
|
break; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
// ------------------------------------------------------------------------ |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Router::handleModuleRequest |
346
|
|
|
*/ |
347
|
|
|
public function handleSegmentsRequest() |
348
|
|
|
{ |
349
|
|
|
$module = modules()->getActiveModule(); |
|
|
|
|
350
|
|
|
|
351
|
|
|
if ($numOfUriSegments = $this->uri->segments->count()) { |
352
|
|
|
$uriSegments = $this->uri->segments->getArrayCopy(); |
353
|
|
|
|
354
|
|
|
for ($i = 0; $i <= $numOfUriSegments; $i++) { |
355
|
|
|
$uriRoutedSegments = array_diff($uriSegments, |
356
|
|
|
array_slice($uriSegments, ($numOfUriSegments - $i))); |
357
|
|
|
|
358
|
|
|
if(count($uriRoutedSegments)) { |
359
|
|
|
if($module instanceof FrameworkModuleDataStructure) { |
360
|
|
|
$moduleSegments = $module->getSegments(); |
361
|
|
|
|
362
|
|
|
if(count($moduleSegments)) { |
363
|
|
|
$uriRoutedSegments = array_merge($moduleSegments, $uriRoutedSegments); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
if (false !== ($module = modules()->getModule($uriRoutedSegments))) { |
368
|
|
|
$uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
369
|
|
|
$this->uri->segments->exchangeArray($uriSegments); |
370
|
|
|
|
371
|
|
|
$this->registerModule($module); |
|
|
|
|
372
|
|
|
$this->handleSegmentsRequest(); |
373
|
|
|
break; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// ------------------------------------------------------------------------ |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Router::getPagesControllerClassName |
384
|
|
|
* |
385
|
|
|
* @return bool|string |
386
|
|
|
*/ |
387
|
|
|
final protected function getPagesControllerClassName() |
388
|
|
|
{ |
389
|
|
|
$modules = modules()->getArrayCopy(); |
390
|
|
|
|
391
|
|
|
foreach ($modules as $module) { |
392
|
|
|
$controllerClassName = $module->getNamespace() . 'Controllers\Pages'; |
393
|
|
|
if ($module->getNamespace() === 'O2System\Framework\\') { |
394
|
|
|
$controllerClassName = 'O2System\Framework\Http\Controllers\Pages'; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
if (class_exists($controllerClassName)) { |
398
|
|
|
return $controllerClassName; |
399
|
|
|
break; |
|
|
|
|
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
if (class_exists('O2System\Framework\Http\Controllers\Pages')) { |
404
|
|
|
return 'O2System\Framework\Http\Controllers\Pages'; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
return false; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
// ------------------------------------------------------------------------ |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Router::registerModule |
414
|
|
|
* |
415
|
|
|
* @param FrameworkModuleDataStructure $module |
416
|
|
|
*/ |
417
|
|
|
final public function registerModule(FrameworkModuleDataStructure $module) |
418
|
|
|
{ |
419
|
|
|
// Push Subdomain App Module |
420
|
|
|
modules()->push($module); |
|
|
|
|
421
|
|
|
|
422
|
|
|
// Add Config FilePath |
423
|
|
|
config()->addFilePath($module->getRealPath()); |
|
|
|
|
424
|
|
|
|
425
|
|
|
// Reload Config |
426
|
|
|
config()->reload(); |
|
|
|
|
427
|
|
|
|
428
|
|
|
// Load modular addresses config |
429
|
|
|
if (false !== ($configDir = $module->getDir('config', true))) { |
430
|
|
|
unset($addresses); |
|
|
|
|
431
|
|
|
|
432
|
|
|
$reconfig = false; |
433
|
|
|
if (is_file( |
434
|
|
|
$filePath = $configDir . ucfirst( |
435
|
|
|
strtolower(ENVIRONMENT) |
|
|
|
|
436
|
|
|
) . DIRECTORY_SEPARATOR . 'Addresses.php' |
437
|
|
|
)) { |
438
|
|
|
require($filePath); |
439
|
|
|
$reconfig = true; |
440
|
|
|
} elseif (is_file( |
441
|
|
|
$filePath = $configDir . 'Addresses.php' |
442
|
|
|
)) { |
443
|
|
|
require($filePath); |
444
|
|
|
$reconfig = true; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if ( ! $reconfig) { |
448
|
|
|
$controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
449
|
|
|
$controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
450
|
|
|
|
451
|
|
|
if (class_exists($controllerClassName)) { |
452
|
|
|
$this->addresses->any( |
453
|
|
|
'/', |
454
|
|
|
function () use ($controllerClassName) { |
455
|
|
|
return new $controllerClassName(); |
456
|
|
|
} |
457
|
|
|
); |
458
|
|
|
} |
459
|
|
|
} elseif (isset($addresses)) { |
|
|
|
|
460
|
|
|
$this->addresses = $addresses; |
461
|
|
|
} |
462
|
|
|
} else { |
463
|
|
|
$controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
464
|
|
|
$controllerClassName = $controllerNamespace . studlycase($module->getParameter()); |
465
|
|
|
|
466
|
|
|
if (class_exists($controllerClassName)) { |
467
|
|
|
$this->addresses->any( |
468
|
|
|
'/', |
469
|
|
|
function () use ($controllerClassName) { |
470
|
|
|
return new $controllerClassName(); |
471
|
|
|
} |
472
|
|
|
); |
473
|
|
|
} |
474
|
|
|
} |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
// ------------------------------------------------------------------------ |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Router::parseAction |
481
|
|
|
* |
482
|
|
|
* @param KernelActionDataStructure $action |
483
|
|
|
* @param array $uriSegments |
484
|
|
|
* |
485
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
486
|
|
|
* @throws \ReflectionException |
487
|
|
|
*/ |
488
|
|
|
protected function parseAction(KernelActionDataStructure $action, array $uriSegments = []) |
489
|
|
|
{ |
490
|
|
|
ob_start(); |
491
|
|
|
$closure = $action->getClosure(); |
492
|
|
|
if (empty($closure)) { |
493
|
|
|
$closure = ob_get_contents(); |
494
|
|
|
} |
495
|
|
|
ob_end_clean(); |
496
|
|
|
|
497
|
|
|
if ($closure instanceof Controller) { |
498
|
|
|
$uriSegments = empty($uriSegments) |
499
|
|
|
? $action->getClosureParameters() |
500
|
|
|
: $uriSegments; |
501
|
|
|
$this->setController( |
502
|
|
|
(new KernelControllerDataStructure($closure)) |
|
|
|
|
503
|
|
|
->setRequestMethod('index'), |
504
|
|
|
$uriSegments |
505
|
|
|
); |
506
|
|
|
} elseif ($closure instanceof KernelControllerDataStructure) { |
507
|
|
|
$this->setController($closure, $action->getClosureParameters()); |
508
|
|
|
} elseif (is_array($closure)) { |
509
|
|
|
$this->uri = (new KernelMessageUri()) |
510
|
|
|
->withSegments(new KernelMessageUriSegments('')) |
511
|
|
|
->withQuery(''); |
512
|
|
|
$this->handle($this->uri->addSegments($closure)); |
513
|
|
|
} else { |
514
|
|
|
if (class_exists($closure)) { |
515
|
|
|
$this->setController( |
516
|
|
|
(new KernelControllerDataStructure($closure)) |
517
|
|
|
->setRequestMethod('index'), |
518
|
|
|
$uriSegments |
519
|
|
|
); |
520
|
|
|
} elseif (preg_match("/([a-zA-Z0-9\\\]+)(@)([a-zA-Z0-9\\\]+)/", $closure, $matches)) { |
521
|
|
|
$this->setController( |
522
|
|
|
(new KernelControllerDataStructure($matches[ 1 ])) |
523
|
|
|
->setRequestMethod($matches[ 3 ]), |
524
|
|
|
$uriSegments |
525
|
|
|
); |
526
|
|
|
} elseif ($theme = presenter()->theme) { |
527
|
|
|
if($theme->use === true) { |
|
|
|
|
528
|
|
|
if ( ! presenter()->partials->offsetExists('content') && $closure !== '') { |
529
|
|
|
presenter()->partials->offsetSet('content', $closure); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
if (presenter()->partials->offsetExists('content')) { |
533
|
|
|
profiler()->watch('VIEW_SERVICE_RENDER'); |
534
|
|
|
view()->render(); |
535
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
536
|
|
|
} else { |
537
|
|
|
output()->sendError(204); |
538
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
} elseif (is_string($closure) && $closure !== '') { |
542
|
|
|
if (is_json($closure)) { |
543
|
|
|
output()->setContentType('application/json'); |
544
|
|
|
output()->send($closure); |
|
|
|
|
545
|
|
|
} else { |
546
|
|
|
output()->send($closure); |
547
|
|
|
} |
548
|
|
|
} elseif (is_array($closure) || is_object($closure)) { |
549
|
|
|
output()->send($closure); |
550
|
|
|
} elseif (is_numeric($closure)) { |
551
|
|
|
output()->sendError($closure); |
|
|
|
|
552
|
|
|
} else { |
553
|
|
|
output()->sendError(204); |
554
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
} |