|
1
|
|
|
<?php |
|
2
|
|
|
namespace rtens\domin\delivery\web\resources; |
|
3
|
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
|
5
|
|
|
use rtens\domin\delivery\ParameterReader; |
|
6
|
|
|
use rtens\domin\delivery\web\ActionForm; |
|
7
|
|
|
use rtens\domin\delivery\web\ActionResult; |
|
8
|
|
|
use rtens\domin\delivery\web\BreadCrumb; |
|
9
|
|
|
use rtens\domin\delivery\web\BreadCrumbsTrail; |
|
10
|
|
|
use rtens\domin\delivery\web\HeadElements; |
|
11
|
|
|
use rtens\domin\delivery\web\WebApplication; |
|
12
|
|
|
use rtens\domin\Executor; |
|
13
|
|
|
|
|
14
|
|
|
class ExecutionResource { |
|
15
|
|
|
|
|
16
|
|
|
const FORCE_ARG = '__force'; |
|
17
|
|
|
|
|
18
|
|
|
/** @var WebApplication */ |
|
19
|
|
|
private $app; |
|
20
|
|
|
|
|
21
|
|
|
/** @var BreadCrumbsTrail */ |
|
22
|
|
|
private $crumbs; |
|
23
|
|
|
|
|
24
|
|
|
/** @var ParameterReader */ |
|
25
|
|
|
private $reader; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(WebApplication $app, ParameterReader $reader, BreadCrumbsTrail $crumbs) { |
|
28
|
|
|
$this->app = $app; |
|
29
|
|
|
$this->crumbs = $crumbs; |
|
30
|
|
|
$this->reader = $reader; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private static function baseHeadElements() { |
|
34
|
|
|
return [ |
|
35
|
|
|
HeadElements::jquery(), |
|
36
|
|
|
HeadElements::jqueryUi(), // not actually needed but it needs to be included before bootstrap.js too avoid conflicts |
|
37
|
|
|
HeadElements::bootstrap(), |
|
38
|
|
|
HeadElements::bootstrapJs(), |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $actionId |
|
44
|
|
|
* @param bool $force |
|
45
|
|
|
* @return string |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public function handleGet($actionId, $force = false) { |
|
49
|
|
|
return $this->doExecute($actionId, $force); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $actionId |
|
54
|
|
|
* @return string |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
*/ |
|
57
|
|
|
public function handlePost($actionId) { |
|
58
|
|
|
return $this->doExecute($actionId, true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function doExecute($actionId, $mayBeModifying = false) { |
|
62
|
|
|
$action = $this->getAction($actionId); |
|
63
|
|
|
$headElements = self::baseHeadElements(); |
|
64
|
|
|
|
|
65
|
|
|
$form = new ActionForm($this->reader, $this->app->fields, $action, $actionId); |
|
66
|
|
|
$headElements = array_merge($headElements, $form->getHeadElements()); |
|
67
|
|
|
|
|
68
|
|
|
if ($mayBeModifying || !$action->isModifying()) { |
|
69
|
|
|
$executor = new Executor($this->app->actions, $this->app->fields, $this->reader); |
|
70
|
|
|
$result = new ActionResult($executor, $this->app->renderers, $action, $actionId, $this->crumbs); |
|
71
|
|
|
$headElements = array_merge($headElements, $result->getHeadElements()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
global $model; |
|
|
|
|
|
|
75
|
|
|
$model = [ |
|
76
|
|
|
'name' => $this->app->name, |
|
77
|
|
|
'caption' => $action->caption(), |
|
78
|
|
|
'menu' => $this->app->menu->render(), |
|
79
|
|
|
'breadcrumbs' => $this->assembleBreadCrumbs(), |
|
80
|
|
|
'action' => $form->getModel(), |
|
81
|
|
|
'result' => isset($result) ? $result->getModel() : null, |
|
82
|
|
|
'headElements' => HeadElements::filter($headElements), |
|
83
|
|
|
'executed' => isset($result) && $result->wasExecuted() |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
|
|
ob_start(); |
|
87
|
|
|
include __DIR__ . '/ExecutionTemplate.html.php'; |
|
88
|
|
|
return ob_get_clean(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $actionId |
|
93
|
|
|
* @return Action |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getAction($actionId) { |
|
97
|
|
|
try { |
|
98
|
|
|
return $this->app->actions->getAction($actionId); |
|
99
|
|
|
} catch (\Exception $e) { |
|
100
|
|
|
throw new \Exception("Action [$actionId] is not registered.", 0, $e); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function assembleBreadCrumbs() { |
|
105
|
|
|
return array_map(function (BreadCrumb $crumb) { |
|
106
|
|
|
return [ |
|
107
|
|
|
'target' => $crumb->getTarget(), |
|
108
|
|
|
'caption' => $crumb->getCaption() |
|
109
|
|
|
]; |
|
110
|
|
|
}, $this->crumbs->getCrumbs()); |
|
111
|
|
|
} |
|
112
|
|
|
} |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state