1 | <?php |
||
27 | class ControllerProcessor implements SingletonInterface |
||
28 | { |
||
29 | use ExtendedSelfInstantiateTrait; |
||
30 | |||
31 | /** |
||
32 | * @var FormObjectFactory |
||
33 | */ |
||
34 | protected $formObjectFactory; |
||
35 | |||
36 | /** |
||
37 | * @var FormObject[] |
||
38 | */ |
||
39 | protected $formArguments; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $dispatched = false; |
||
45 | |||
46 | /** |
||
47 | * @var Request |
||
48 | */ |
||
49 | protected $originalRequest; |
||
50 | |||
51 | /** |
||
52 | * @var Request |
||
53 | */ |
||
54 | protected $request; |
||
55 | |||
56 | /** |
||
57 | * @var Arguments |
||
58 | */ |
||
59 | protected $requestArguments; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $settings = []; |
||
65 | |||
66 | /** |
||
67 | * Contains information about the last request that was dispatched to FormZ. |
||
68 | * It is used to prevent infinite loop. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $lastDispatchedRequest; |
||
73 | |||
74 | /** |
||
75 | * @param MvcRequest $request |
||
76 | * @param Arguments $requestArguments |
||
77 | * @return $this |
||
78 | */ |
||
79 | public static function prepare(MvcRequest $request, Arguments $requestArguments) |
||
80 | { |
||
81 | return self::get()->setData($request, $requestArguments, []); |
||
|
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Injects the data needed for this class to work properly. This method must |
||
86 | * be called before the `dispatch` method is called. |
||
87 | * |
||
88 | * @param MvcRequest $request |
||
89 | * @param Arguments $requestArguments |
||
90 | * @param array $settings |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setData(MvcRequest $request, Arguments $requestArguments, array $settings) |
||
94 | { |
||
95 | /** @var Request $request */ |
||
96 | $dispatchedRequest = $request->getControllerObjectName() . '::' . $request->getControllerActionName(); |
||
97 | |||
98 | if ($dispatchedRequest !== $this->lastDispatchedRequest) { |
||
99 | $this->lastDispatchedRequest = $dispatchedRequest; |
||
100 | |||
101 | $this->originalRequest = $request; |
||
102 | $this->request = clone $request; |
||
103 | $this->requestArguments = $requestArguments; |
||
104 | $this->settings = $settings; |
||
105 | $this->formArguments = null; |
||
106 | $this->dispatched = false; |
||
107 | } |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Will dispatch the current request to the form controller, which will take |
||
114 | * care of processing everything properly. |
||
115 | * |
||
116 | * In case no form is found in the controller action parameters, the current |
||
117 | * request is not killed. |
||
118 | * |
||
119 | * @throws StopActionException |
||
120 | */ |
||
121 | public function dispatch() |
||
122 | { |
||
123 | if (false === $this->dispatched) { |
||
124 | $this->dispatched = true; |
||
125 | |||
126 | $this->doDispatch(); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Wrapper for unit testing. |
||
132 | */ |
||
133 | protected function doDispatch() |
||
134 | { |
||
135 | if (false === empty($this->getRequestForms())) { |
||
136 | $this->originalRequest->setDispatched(false); |
||
137 | $this->originalRequest->setControllerVendorName('Romm'); |
||
138 | $this->originalRequest->setControllerExtensionName('Formz'); |
||
139 | $this->originalRequest->setControllerName('Form'); |
||
140 | $this->originalRequest->setControllerActionName('processForm'); |
||
141 | |||
142 | $this->checkFormObjectsErrors(); |
||
143 | |||
144 | throw new StopActionException; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Will check if the form objects found in the request arguments contain |
||
150 | * configuration errors. If they do, we dispatch the request to the error |
||
151 | * view, where all errors will be explained properly to the user. |
||
152 | */ |
||
153 | protected function checkFormObjectsErrors() |
||
164 | |||
165 | /** |
||
166 | * Loops on the request arguments, and picks up each one that is a form |
||
167 | * instance (it implements `FormInterface`). |
||
168 | * |
||
169 | * @return FormObject[] |
||
170 | */ |
||
171 | public function getRequestForms() |
||
194 | |||
195 | /** |
||
196 | * @return Request |
||
197 | */ |
||
198 | public function getRequest() |
||
202 | |||
203 | /** |
||
204 | * @return Arguments |
||
205 | */ |
||
206 | public function getRequestArguments() |
||
210 | |||
211 | /** |
||
212 | * @return array |
||
213 | */ |
||
214 | public function getSettings() |
||
218 | |||
219 | /** |
||
220 | * @param FormObjectFactory $formObjectFactory |
||
221 | */ |
||
222 | public function injectFormObjectFactory(FormObjectFactory $formObjectFactory) |
||
226 | } |
||
227 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.