1 | <?php |
||
36 | class FormController extends ActionController |
||
37 | { |
||
38 | /** |
||
39 | * @var ControllerProcessor |
||
40 | */ |
||
41 | protected $processor; |
||
42 | |||
43 | /** |
||
44 | * @var Request |
||
45 | */ |
||
46 | protected $originalRequest; |
||
47 | |||
48 | /** |
||
49 | * Main action used to dispatch the request properly, depending on FormZ |
||
50 | * configuration. |
||
51 | * |
||
52 | * The request is based on the previously called controller action, and is |
||
53 | * used to list which forms are handled (every argument of the action method |
||
54 | * that implements the interface `FormInterface`). |
||
55 | * |
||
56 | * Middlewares will be called for each form argument, and may modify the |
||
57 | * request, which is then dispatched again with modified data. |
||
58 | * |
||
59 | * @param Request $originalRequest |
||
60 | * @throws Exception |
||
61 | */ |
||
62 | public function processFormAction(Request $originalRequest) |
||
63 | { |
||
64 | $exception = null; |
||
65 | $this->originalRequest = $originalRequest; |
||
66 | |||
67 | try { |
||
68 | $this->invokeMiddlewares(); |
||
69 | $this->manageRequestResult(); |
||
70 | } catch (Exception $exception) { |
||
|
|||
71 | } |
||
72 | |||
73 | $this->commitMetadata(); |
||
74 | |||
75 | if ($exception instanceof Exception) { |
||
76 | if ($exception instanceof StopPropagationException) { |
||
77 | if ($exception instanceof RedirectException) { |
||
78 | $this->redirectFromException($exception); |
||
79 | } elseif (false === $exception instanceof ForwardException) { |
||
80 | $this->forwardToReferrer(); |
||
81 | } |
||
82 | } else { |
||
83 | throw $exception; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | $this->continueRequest(); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param FormObject $formObject |
||
92 | */ |
||
93 | public function formObjectErrorAction(FormObject $formObject) |
||
97 | |||
98 | /** |
||
99 | * Will fetch every form argument for this request, and dispatch every |
||
100 | * middleware that was registered in its TypoScript configuration. |
||
101 | */ |
||
102 | protected function invokeMiddlewares() |
||
111 | |||
112 | /** |
||
113 | * Will check if the request result contains error; if errors are found, the |
||
114 | * request is forwarded to the referring request, with the arguments of the |
||
115 | * current request. |
||
116 | */ |
||
117 | protected function manageRequestResult() |
||
118 | { |
||
119 | $result = $this->processor->getRequest()->getOriginalRequestMappingResults(); |
||
120 | $this->request->setOriginalRequestMappingResults($result); |
||
121 | |||
122 | if ($result->hasErrors()) { |
||
123 | $this->forwardToReferrer(); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Loops on every form of this request, and commits the metadata in database |
||
129 | * for each one. |
||
130 | */ |
||
131 | protected function commitMetadata() |
||
141 | |||
142 | /** |
||
143 | * Forwards the request to the original request that led to this controller. |
||
144 | * |
||
145 | * @throws StopActionException |
||
146 | */ |
||
147 | protected function continueRequest() |
||
161 | |||
162 | /** |
||
163 | * Forwards to the referrer request. It will also fill the arguments of the |
||
164 | * action with the ones from the source request. |
||
165 | * |
||
166 | * @throws StopActionException |
||
167 | */ |
||
168 | protected function forwardToReferrer() |
||
169 | { |
||
170 | $referringRequest = $this->originalRequest->getReferringRequest(); |
||
171 | |||
172 | if ($referringRequest) { |
||
173 | $this->request->setDispatched(false); |
||
174 | $this->request->setOriginalRequest($this->originalRequest); |
||
175 | |||
176 | $this->request->setControllerVendorName($referringRequest->getControllerVendorName()); |
||
177 | $this->request->setControllerExtensionName($referringRequest->getControllerExtensionName()); |
||
178 | $this->request->setControllerName($referringRequest->getControllerName()); |
||
179 | $this->request->setControllerActionName($referringRequest->getControllerActionName()); |
||
180 | $this->request->setArguments($this->processor->getRequest()->getArguments()); |
||
181 | } else { |
||
182 | // @todo ? |
||
183 | } |
||
184 | |||
185 | throw new StopActionException; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param RedirectException $redirectException |
||
190 | */ |
||
191 | protected function redirectFromException(RedirectException $redirectException) |
||
205 | |||
206 | /** |
||
207 | * @param ControllerProcessor $processor |
||
208 | */ |
||
209 | public function injectProcessor(ControllerProcessor $processor) |
||
213 | } |
||
214 |