1 | <?php |
||
36 | class FormController extends ActionController |
||
37 | { |
||
38 | /** |
||
39 | * @var ControllerProcessor |
||
40 | */ |
||
41 | protected $processor; |
||
42 | |||
43 | /** |
||
44 | * Main action used to dispatch the request properly, depending on FormZ |
||
45 | * configuration. |
||
46 | * |
||
47 | * The request is based on the previously called controller action, and is |
||
48 | * used to list which forms are handled (every argument of the action method |
||
49 | * that implements the interface `FormInterface`). |
||
50 | * |
||
51 | * Middlewares will be called for each form argument, and may modify the |
||
52 | * request, which is then dispatched again with modified data. |
||
53 | * |
||
54 | * @throws Throwable |
||
55 | */ |
||
56 | public function processFormAction() |
||
57 | { |
||
58 | try { |
||
59 | $this->invokeMiddlewares(); |
||
60 | $this->manageRequestResult(); |
||
61 | } catch (Throwable $exception) { |
||
|
|||
62 | if ($exception instanceof StopPropagationException) { |
||
63 | if ($exception instanceof RedirectException) { |
||
64 | $this->redirectFromException($exception); |
||
65 | } elseif (false === $exception instanceof ForwardException) { |
||
66 | $this->resetSubstepsLevel(); |
||
67 | $this->forwardToReferrer(); |
||
68 | } |
||
69 | } else { |
||
70 | $this->callExceptionHandler($exception); |
||
71 | } |
||
72 | } finally { |
||
73 | $this->persistForms(); |
||
74 | } |
||
75 | |||
76 | $this->continueRequest(); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Wrapping the rendering with a try/catch to handle the exception callback |
||
81 | * if there is one. |
||
82 | */ |
||
83 | protected function callActionMethod() |
||
84 | { |
||
85 | try { |
||
86 | parent::callActionMethod(); |
||
87 | } catch (Throwable $exception) { |
||
88 | $this->callExceptionHandler($exception); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param FormObject $formObject |
||
94 | */ |
||
95 | public function formObjectErrorAction(FormObject $formObject) |
||
96 | { |
||
97 | $this->view->assign('formObject', $formObject); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Throwable $exception |
||
102 | * @throws Throwable |
||
103 | */ |
||
104 | protected function callExceptionHandler(Throwable $exception) |
||
105 | { |
||
106 | if ($exception instanceof StopActionException |
||
107 | || !$this->processor->hasExceptionCallback() |
||
108 | ) { |
||
109 | throw $exception; |
||
110 | } |
||
111 | |||
112 | call_user_func($this->processor->getExceptionCallback(), $exception); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Will fetch every form argument for this request, and dispatch every |
||
117 | * middleware that was registered in its TypoScript configuration. |
||
118 | */ |
||
119 | protected function invokeMiddlewares() |
||
128 | |||
129 | /** |
||
130 | * @todo |
||
131 | */ |
||
132 | protected function resetSubstepsLevel() |
||
139 | |||
140 | /** |
||
141 | * Will check if the request result contains error; if errors are found, the |
||
142 | * request is forwarded to the referring request, with the arguments of the |
||
143 | * current request. |
||
144 | */ |
||
145 | protected function manageRequestResult() |
||
154 | |||
155 | /** |
||
156 | * Loops on every form of this request, and persists each one. |
||
157 | */ |
||
158 | protected function persistForms() |
||
174 | |||
175 | /** |
||
176 | * Forwards the request to the original request that led to this controller. |
||
177 | * |
||
178 | * @throws StopActionException |
||
179 | */ |
||
180 | protected function continueRequest() |
||
194 | |||
195 | /** |
||
196 | * Forwards to the referrer request. It will also fill the arguments of the |
||
197 | * action with the ones from the source request. |
||
198 | * |
||
199 | * @throws StopActionException |
||
200 | */ |
||
201 | protected function forwardToReferrer() |
||
233 | |||
234 | /** |
||
235 | * @param RedirectException $redirectException |
||
236 | */ |
||
237 | protected function redirectFromException(RedirectException $redirectException) |
||
251 | |||
252 | /** |
||
253 | * @param ControllerProcessor $processor |
||
254 | */ |
||
255 | public function injectProcessor(ControllerProcessor $processor) |
||
259 | } |
||
260 |