-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
721 lines (663 loc) · 17.5 KB
/
controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
<?php
/**
* PMVC.
*
* PHP version 5
*
* @category CategoryName
*
* @package PMVC
*
* @author Hill <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*
* @version GIT: <git_id>
*
* @link https://packagist.org/packages/pmvc/pmvc
*/
namespace PMVC;
use DomainException;
l(__DIR__.'/src/Constants');
l(__DIR__.'/src/Action');
l(__DIR__.'/src/ActionForm');
l(__DIR__.'/src/ActionForward');
l(__DIR__.'/src/ActionMapping');
l(__DIR__.'/src/ActionMappings');
l(__DIR__.'/src/MappingBuilder');
l(__DIR__.'/src/Request');
l(__DIR__.'/src/RouterInterface');
l(__DIR__.'/src/Task');
l(__DIR__.'/src/Queue');
${_INIT_CONFIG}[_CLASS] = __NAMESPACE__.'\controller';
/**
* PMVC Action.
*
* @category CategoryName
*
* @package PMVC
*
* @author Hill <[email protected]>
* @license http://opensource.org/licenses/MIT MIT
*
* @link https://packagist.org/packages/pmvc/pmvc
*/
// @codingStandardsIgnoreStart
class controller extends PlugIn // @codingStandardsIgnoreEnd
{
/**
* Mapping.
*
* @var ActionMappings
*/
private $_mappings;
/**
* Request.
*
* @var HttpRequestServlet
*/
private $_request;
/**
* Finish flag.
*
* @var bool
*/
private $_isFinish;
/**
* ActionController construct with the options.
*
* {Controller} -> plugapp -> process -> execute -> processForm ->
* _processValidate -> _processAction -> processForward -> _finish
*/
public function __construct()
{
$this->_addAppFolders([__DIR__.'/../../pmvc-app']);
$this->_mappings = new ActionMappings();
$this->_request = new Request();
}
/**
* Plug App.
*
* Controller -> {plugapp} -> process -> execute -> _processForm ->
* _processValidate -> _processAction -> processForward -> _finish
*
* @param array $folders defaultAppFolder
* @param array $appAlias appAlias
* @param string $indexFile index.php
*
* @return bool Check is plug success or failed.
*/
public function plugApp(
array $folders = [],
array $appAlias = [],
$indexFile = DEFAULT_INDEX
) {
if (exists(_RUN_APP, 'plugin')) {
return !trigger_error('APP was pluged.', E_USER_WARNING);
}
callPlugin('dispatcher', 'notify', [Event\MAP_REQUEST, true]);
$this->_handleAlias($appAlias);
if (empty($folders) && $this[_RUN_APPS]) {
$folders = toArray($this[_RUN_APPS]);
}
$folders = $this->_addAppFolders($folders);
$parents = $folders['folders'];
$path = $this->getAppFile($parents, $indexFile);
if (!$path) {
$path = $this->app_not_found($parents, $indexFile, $folders);
if (!$path) {
return false;
}
}
$parent = realpath(dirname(dirname($path)));
$this[_RUN_APPS] = $parent;
$appPlugin = plug(
_RUN_APP,
[
_PLUGIN_FILE => $path,
_DEFAULT_CLASS => '\PMVC\Action',
]
);
addPlugInFolders(
[
$parent.'/'.$this[_REAL_APP].'/plugins',
$this->getAppsParent().'plugins',
]
);
$names = explode('_', $this[_REAL_APP]);
set(
$appPlugin,
array_replace(
value(option('get', $names[0], []), array_slice($names, 1), []),
value(option('get', 'PW', []), $names, [])
)
);
if (isset($appPlugin[_INIT_BUILDER])) {
$builder = $this->addMapping($appPlugin[_INIT_BUILDER]);
unset($appPlugin[_INIT_BUILDER]);
return $builder;
} else {
return true;
}
}
/**
* Process the request.
*
* Controller -> plugapp -> {process} -> execute -> processForm ->
* _processValidate -> _processAction -> processForward -> _finish
*
* @param MappingBuilder $builder Get mappings
*
* @return mixed
*/
public function process(MappingBuilder $builder = null)
{
callPlugin('dispatcher', 'notify', [Event\MAP_REQUEST, true]);
if (callPlugin('dispatcher', 'stop')) {
// Stop for authentication plugin verify failed
return;
}
if (!is_null($builder)) {
$this->addMapping($builder);
}
$forward = (object) [
'action' => $this->getAppAction(),
];
$results = [];
while ($forward && isset($forward->action)) {
$forward = $this->execute($forward->action);
$results[] = $this->processForward($forward);
}
return $this->_finish($results);
}
/**
* Execute mapping.
*
* Controller -> plugapp -> process -> {execute} -> processForm ->
* _processValidate -> _processAction -> processForward -> _finish
*
* @param string $index pass run action
*
* @return ActionMapping
*/
public function execute($index)
{
if (DEFAULT_INDEX !== $index
&& !$this->_mappings->actionExists($index)
) {
return !trigger_error(
'No mappings found for action: ['.$index.']',
E_USER_WARNING
);
}
$actionMapping = $this->_mappings->findAction($index);
$actionForm = $this->processForm($actionMapping);
$this[_RUN_FORM] = $actionForm;
//validate the form if necesarry
if ($actionMapping->validate) {
$errorForward = $this->_processValidate($actionForm);
}
if (!empty($errorForward)) {
$actionForward = $errorForward;
} else {
$actionForward = $this->_processAction($actionMapping, $actionForm);
}
dev(
/**
* Dev.
*
* @help MVC debug.
*/
function () use ($actionMapping, $actionForm, $actionForward) {
$func = $this->getActionFunc($actionMapping);
$annot = \PMVC\plug('annotation');
$doc = $annot->get($func);
$line = $doc->getStartLine();
$file = $doc->getFile();
$actionFile = compact('file', 'line', 'func');
return compact(
'actionMapping',
'actionForm',
'actionForward',
'actionFile'
);
},
'mvc'
);
return $actionForward;
}
/**
* Process form to handle user input.
*
* Controller -> plugapp -> process -> execute -> {processForm} ->
* _processValidate -> _processAction -> processForward -> _finish
*
* @param ActionMapping $actionMapping actionMapping
*
* @return ActionForm
*/
public function processForm($actionMapping)
{
if (empty($actionMapping->form)) {
$actionForm = $this[_RUN_FORM];
if (empty($actionForm)) {
$defaultForm = option(
'get',
_DEFAULT_FORM,
__NAMESPACE__.'\ActionForm'
);
$actionForm = new $defaultForm();
}
} else {
$actionForm = $this->_mappings->findForm($actionMapping->form);
if (empty($actionForm)) {
throw new DomainException(
'ActionForm: ['.$actionMapping->form.'] not exists.'
);
}
}
//add request parameters
$this->_initActionFormValue($actionForm, $actionMapping);
return $actionForm;
}
/**
* Call the validate() with ActionForm.
*
* Controller -> plugapp -> process -> execute -> processForm ->
* {_processValidate} -> _processAction -> processForward -> _finish
*
* @param ActionForm $actionForm actionForm
*
* @return bool if good to go return false else return true to block.
*/
private function _processValidate($actionForm)
{
$isValid = (string) $actionForm->validate();
$error = $this->getErrorForward();
if ($error) {
return $error;
}
return !$isValid;
}
/**
* Process action.
*
* Controller -> plugapp -> process -> execute -> processForm ->
* _processValidate -> {_processAction} -> processForward -> _finish
*
* @param ActionMapping $actionMapping actionMapping
* @param ActionForm $actionForm actionForm
*
* @return ActionForward
*/
private function _processAction($actionMapping, $actionForm)
{
callPlugin('dispatcher', 'notify', [Event\WILL_PROCESS_ACTION, true]);
return call_user_func_array(
$this->getActionFunc($actionMapping),
[
$actionMapping,
$actionForm,
]
);
}
/**
* Process forward.
*
* Controller -> plugapp -> process -> execute -> processForm ->
* _processValidate -> _processAction -> {processForward} -> _finish
*
* @param ActionForward $actionForward actionForward
*
* @return mixed
*/
public function processForward($actionForward)
{
if (!is_callable([$actionForward, 'go'])) {
dev(
function () use ($actionForward) {
/*
* If actionForward
* wiil call dev @actionForward::__destruct
*/
return ['nonForward' => $actionForward];
},
'view'
);
return $actionForward;
}
$this[_FORWARD] = $actionForward;
if (callPlugin('dispatcher', 'stop')) {
unset($actionForward->action);
return;
}
return $actionForward->go();
}
/**
* Finish request and take down the controller.
*
* Controller -> plugapp -> process -> execute -> processForm ->
* _processValidate -> _processAction -> processForward -> {_finish}
*
* @param mixed $done Return data.
*
* @return void
*/
private function _finish($done = null)
{
dev(
/**
* Dev.
*
* @help Finish.
*/
function () use ($done) {
return [
'done' => $done,
];
},
'finish'
);
if ($this[Event\FINISH] || $this->_isFinish) {
return $done;
}
$this->_isFinish = true;
/*Only parse user error, not contain system and app errors*/
$errorForward = $this->getErrorForward();
if ($errorForward) {
$this->processForward($errorForward);
}
/* <!-- Need located after processForward to avoid json view trigger twice*/
callPlugin('dispatcher', 'notify', [Event\FINISH, true]);
// Need located after callPlugin to avoid unexpedted trigger.
option('set', Event\FINISH, true);
/* --> */
return $done;
}
/**
* Destruct.
*
* @return void
*/
public function __destruct()
{
$this->_finish();
}
/**
* <!-- Start Sub function.
*/
/**
* Handle Alias.
*
* @param array $alias Assign form plugApp
*
* @return void
*/
private function _handleAlias(array $alias)
{
$folders = $this->_addAppFolders([], $alias);
$alias = $folders['alias'];
$app = $this->getApp();
$aliasApp = $app ? get($alias, $app) : null;
if ($aliasApp) {
$this[_REAL_APP] = $aliasApp;
} else {
$this[_REAL_APP] = $app;
// Get default after dimension back
$this[_REAL_APP] = $this->getApp();
}
}
/**
* Get app file.
*
* @param string $parents Multiple app folder
* @param string $indexFile index.php
*
* @return mixed
*/
public function getAppFile($parents, $indexFile)
{
$file = $this[_REAL_APP].'/'.$indexFile.'.php';
return find($file, $parents);
}
/**
* Add mapping.
*
* @param mixed $mappings mappings
*
* @return bool
*/
public function addMapping(MappingBuilder $mappings)
{
return $this->_mappings->add($mappings);
}
/**
* Init Action Form Value.
*
* @param ActionForm $actionForm actionForm
* @param ActionMapping $actionMapping actionMapping
*
* @return ActionForm
*/
private function _initActionFormValue($actionForm, $actionMapping)
{
$scope = &$actionMapping->scope;
$this[_SCOPE] = $actionMapping;
if (!is_array($scope)) {
$scope = $this->_request->keySet();
}
foreach ($scope as $name) {
if (is_array($name)) {
$actionForm[$name[1]] = $this->_request[$name[0]];
} else {
$actionForm[$name] = $this->_request[$name];
}
}
}
/**
* Get action call.
*
* @param ActionMapping $actionMapping actionMapping
*
* @return callable
*/
public function getActionFunc(ActionMapping $actionMapping)
{
$func = $actionMapping->func;
if (!is_callable($func)) {
if (exists(_RUN_APP, 'plugin')) {
$func = [plug(_RUN_APP), $func];
} else {
return !trigger_error(
'parse action error, function not exists. '.
print_r($func, true),
E_USER_WARNING
);
}
}
return $func;
}
/**
* <!-- Start public get/set function.
*/
/**
* Init Error Action Forward.
*
* @return ActionForward
*/
public function getErrorForward()
{
$allErrors = $this[ERRORS];
if (empty($allErrors[USER_LAST_ERROR])) {
return false;
}
return $this->process_error($allErrors);
}
/**
* Get Mapping.
*
* @return mixed
*/
public function getMappings()
{
return $this->_mappings;
}
/**
* Get Request.
*
* @return mixed
*/
public function getRequest()
{
return $this->_request;
}
/**
* Get apps parent.
*
* @return mixed
*/
public function getAppsParent()
{
$folder = $this[_RUN_APPS];
$i = strrpos($folder || '', '/vendor/');
$folder = $i !== false ?
substr($folder, 0, $i) : lastSlash($folder).'../';
return realpath($folder).'/';
}
/**
* Get apps folder.
*
* @return string
*/
public function getAppsFolder()
{
return realpath($this[_RUN_APPS]).'/';
}
/**
* Get app.
*
* @return string
*/
public function getApp()
{
return $this[_RUN_APP] ?: $this[_DEFAULT_APP];
}
/**
* Set app.
*
* @param string $app app
*
* @return string
*/
public function setApp($app)
{
return $this[_RUN_APP] = strtolower($app);
}
/**
* Get App Action.
*
* @return mixed
*/
public function getAppAction()
{
$action = $this[_RUN_ACTION];
if (!$this->_mappings->actionExists($action)) {
$action = DEFAULT_INDEX;
}
return $action;
}
/**
* Set App Action.
*
* @param string $action action
*
* @return mixed
*/
public function setAppAction($action)
{
return $this[_RUN_ACTION] = $action;
}
/**
* Set option (Will trigger Event).
*
* @param mixed $k key
* @param mixed $v value
*
* @return void
*/
public function offsetSet($k, $v = null)
{
option('set', $k, $v);
callPlugin('dispatcher', 'set', [Event\SET_CONFIG, $k]);
}
/**
* Get Option.
*
* @param mixed $k key
*
* @return mixed
*/
public function &offsetGet($k = null)
{
return option('get', $k);
}
/**
* Get Option.
*
* @param string $k key
*
* @return mixed
*/
public function __get($k)
{
$val = &option('get', $k);
return new BaseObject($val);
}
/**
* Contains key.
*
* @param string $k key
*
* @return bool
*/
public function offsetExists($k)
{
return !empty(option('get', $k));
}
/**
* Add App Folder.
*
* Let _addAppFolders keep private,
* if need pass multiple folders, could use plugApp.
*
* @param array $folders folders
* @param array $alias alias
*
* @return mixed
*/
private function _addAppFolders(array $folders, array $alias = [])
{
$prev = folders(_RUN_APP);
$next = folders(_RUN_APP, $folders, $alias);
dev(
/**
* Dev.
*
* @help Debug for PMVC add app folder.
*/
function () use ($folders, $alias, $prev, $next) {
$trace = plug('debug')->parseTrace(debug_backtrace(), 12);
return [
'previous' => $prev,
'next' => $next,
'params' => [
'folders' => $folders,
'alias' => $alias,
],
'trace' => $trace,
];
},
'app-folder'
);
return $next;
}
}