1 | <?php |
||
37 | class FormController extends ActionController |
||
38 | { |
||
39 | /** |
||
40 | * @var ControllerProcessor |
||
41 | */ |
||
42 | protected $processor; |
||
43 | |||
44 | /** |
||
45 | * @var Request |
||
46 | */ |
||
47 | protected $originalRequest; |
||
48 | |||
49 | /** |
||
50 | * Main action used to dispatch the request properly, depending on FormZ |
||
51 | * configuration. |
||
52 | * |
||
53 | * The request is based on the previously called controller action, and is |
||
54 | * used to list which forms are handled (every argument of the action method |
||
55 | * that implements the interface `FormInterface`). |
||
56 | * |
||
57 | * Middlewares will be called for each form argument, and may modify the |
||
58 | * request, which is then dispatched again with modified data. |
||
59 | * |
||
60 | * @param Request $originalRequest |
||
61 | * @throws Exception |
||
62 | */ |
||
63 | public function processFormAction(Request $originalRequest) |
||
64 | { |
||
65 | $exception = null; |
||
66 | $this->originalRequest = $originalRequest; |
||
67 | |||
68 | try { |
||
69 | $this->invokeMiddlewares(); |
||
70 | $this->manageRequestResult(); |
||
71 | } catch (Exception $exception) { |
||
|
|||
72 | } |
||
73 | |||
74 | if ($exception instanceof Exception) { |
||
75 | if ($exception instanceof StopPropagationException) { |
||
76 | if ($exception instanceof RedirectException) { |
||
77 | $this->redirectFromException($exception); |
||
78 | } elseif (false === $exception instanceof ForwardException) { |
||
79 | $this->forwardToReferrer(); |
||
80 | } |
||
81 | } else { |
||
82 | throw $exception; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | $this->continueRequest(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param FormObject $formObject |
||
91 | */ |
||
92 | public function formObjectErrorAction(FormObject $formObject) |
||
96 | |||
97 | /** |
||
98 | * Will fetch every form argument for this request, and dispatch every |
||
99 | * middleware registered in its definition. |
||
100 | */ |
||
101 | protected function invokeMiddlewares() |
||
110 | |||
111 | /** |
||
112 | * Will check if the request result contains error; if errors are found, the |
||
113 | * request is forwarded to the referring request, with the arguments of the |
||
114 | * current request. |
||
115 | */ |
||
116 | protected function manageRequestResult() |
||
125 | |||
126 | /** |
||
127 | * Forwards the request to the original request that led to this controller. |
||
128 | * |
||
129 | * @throws StopActionException |
||
130 | */ |
||
131 | protected function continueRequest() |
||
145 | |||
146 | /** |
||
147 | * Forwards to the referrer request. It will also fill the arguments of the |
||
148 | * action with the ones from the source request. |
||
149 | * |
||
150 | * @throws StopActionException |
||
151 | */ |
||
152 | protected function forwardToReferrer() |
||
171 | |||
172 | /** |
||
173 | * If an exception of type `RedirectException` was thrown, the request is |
||
174 | * forwarded, using the arguments sent to the exception. |
||
175 | * |
||
176 | * @param RedirectException $redirectException |
||
177 | */ |
||
178 | protected function redirectFromException(RedirectException $redirectException) |
||
192 | |||
193 | /** |
||
194 | * @param ControllerProcessor $processor |
||
195 | */ |
||
196 | public function injectProcessor(ControllerProcessor $processor) |
||
200 | } |
||
201 |