1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\web\root; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\delivery\web\ActionForm; |
6
|
|
|
use rtens\domin\delivery\web\ActionResult; |
7
|
|
|
use rtens\domin\delivery\web\BreadCrumbs; |
8
|
|
|
use rtens\domin\delivery\web\HeadElements; |
9
|
|
|
use rtens\domin\delivery\web\WebApplication; |
10
|
|
|
use watoki\curir\cookie\CookieStore; |
11
|
|
|
use watoki\curir\delivery\WebRequest; |
12
|
|
|
use watoki\curir\delivery\WebResponse; |
13
|
|
|
use watoki\curir\error\HttpError; |
14
|
|
|
use watoki\curir\rendering\PhpRenderer; |
15
|
|
|
use watoki\curir\Resource; |
16
|
|
|
use watoki\factory\Factory; |
17
|
|
|
|
18
|
|
|
class ExecuteResource extends Resource { |
19
|
|
|
|
20
|
|
|
const ACTION_ARG = '__action'; |
21
|
|
|
|
22
|
|
|
/** @var CookieStore */ |
23
|
|
|
private $cookies; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Factory $factory <- |
27
|
|
|
* @param WebApplication $app <- |
28
|
|
|
* @param CookieStore $cookies <- |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Factory $factory, WebApplication $app, CookieStore $cookies) { |
31
|
|
|
parent::__construct($factory); |
32
|
|
|
$this->app = $app; |
|
|
|
|
33
|
|
|
$this->cookies = $cookies; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private static function baseHeadElements() { |
37
|
|
|
return [ |
38
|
|
|
HeadElements::jquery(), |
39
|
|
|
HeadElements::jqueryUi(), // not actually needed but it needs to be included before bootstrap.js too avoid conflicts |
40
|
|
|
HeadElements::bootstrap(), |
41
|
|
|
HeadElements::bootstrapJs(), |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $__action |
47
|
|
|
* @param WebRequest $__request <- |
48
|
|
|
* @return array |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function doGet($__action, WebRequest $__request) { |
52
|
|
|
return $this->doExecute($__action, $__request); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $__action |
57
|
|
|
* @param WebRequest $__request <- |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function doPost($__action, WebRequest $__request) { |
61
|
|
|
return $this->doExecute($__action, $__request, true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function doExecute($actionId, WebRequest $request, $mayBeModifying = false) { |
65
|
|
|
$action = $this->getAction($actionId); |
66
|
|
|
|
67
|
|
|
$crumbs = new BreadCrumbs($this->cookies, $request); |
68
|
|
|
$form = new ActionForm($request, $this->app, $action, $actionId); |
69
|
|
|
|
70
|
|
|
$headElements = array_merge( |
71
|
|
|
self::baseHeadElements(), |
72
|
|
|
$form->getHeadElements() |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ($mayBeModifying || !$action->isModifying()) { |
76
|
|
|
$result = new ActionResult($request, $this->app, $action, $actionId, $crumbs); |
77
|
|
|
$headElements = array_merge($headElements, $result->getHeadElements()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return [ |
81
|
|
|
'name' => $this->app->name, |
82
|
|
|
'caption' => $action->caption(), |
83
|
|
|
'menu' => $this->app->menu->render($request), |
84
|
|
|
'breadcrumbs' => $crumbs->readCrumbs(), |
85
|
|
|
'action' => $form->getModel(), |
86
|
|
|
'result' => isset($result) ? $result->getModel() : null, |
87
|
|
|
'headElements' => HeadElements::filter($headElements), |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function createDefaultRenderer() { |
92
|
|
|
return new PhpRenderer(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $actionId |
97
|
|
|
* @return Action |
98
|
|
|
* @throws HttpError |
99
|
|
|
*/ |
100
|
|
|
private function getAction($actionId) { |
101
|
|
|
try { |
102
|
|
|
return $this->app->actions->getAction($actionId); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
throw new HttpError(WebResponse::STATUS_NOT_FOUND, "Action does not exist.", |
105
|
|
|
"Action [$actionId] is not registered.", 0, $e); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: