Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
22 | abstract class AbstractController |
||
23 | { |
||
24 | /** |
||
25 | * @var BaseApp |
||
26 | */ |
||
27 | protected $app; |
||
28 | |||
29 | /** |
||
30 | * @var Request |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * @var View |
||
36 | */ |
||
37 | protected $view; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $autoRenderView = true; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $autoRenderViewScript; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param BaseApp $app |
||
53 | * @param Request $request |
||
54 | */ |
||
55 | 19 | final public function __construct(BaseApp $app, Request $request) |
|
65 | |||
66 | /** |
||
67 | * Initialization method meant to be overridden in descendant classes (optional). |
||
68 | */ |
||
69 | 19 | protected function init() |
|
72 | |||
73 | /** |
||
74 | * Pre dispatch method meant to be overridden in descendant classes (optional). |
||
75 | * |
||
76 | * This method is called right before the actual action method is called/dispatched. |
||
77 | * Override this instead of init() if access to dispatch properties is required (like |
||
78 | * action name) or you need to return a response. |
||
79 | * |
||
80 | * @param string $action |
||
81 | * @return Response|null |
||
82 | */ |
||
83 | protected function preDispatch($action) |
||
84 | { |
||
85 | return null; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param bool $autoRenderView |
||
90 | */ |
||
91 | 1 | public function setAutoRenderView($autoRenderView) |
|
95 | |||
96 | /** |
||
97 | * @param string $autoRenderViewScript |
||
98 | */ |
||
99 | 1 | public function setAutoRenderViewScript($autoRenderViewScript) |
|
103 | |||
104 | /** |
||
105 | * Dispatch the requested action |
||
106 | * |
||
107 | * @param string|null $action action id/name (lowercase, - word separation) |
||
108 | * @param array $actionArgs |
||
109 | * @return Response |
||
110 | * @throws Routing\Exception\ResourceNotFoundException |
||
111 | */ |
||
112 | 9 | public function dispatch($action = null, array $actionArgs = []) |
|
113 | { |
||
114 | // If not special action is provided, try to get from request |
||
115 | 9 | $router = $this->app->get(RouterInterface::class); |
|
116 | 9 | $action = Str::camelToSeparator( |
|
117 | 9 | $action ?: $router->getRequestAction($this->request), |
|
118 | 9 | '-' |
|
119 | ); |
||
120 | 9 | $actionMethod = $router->getActionMethod($action); |
|
121 | $collectedArgs = $this->getCollectedDispatchArgs($actionMethod, $actionArgs); |
||
122 | |||
123 | // Call pre dispatch method and return it's response if there is one (uncommon) |
||
124 | $preDispatchResponse = $this->preDispatch($action); |
||
125 | if ($preDispatchResponse instanceof Response) { |
||
126 | return $preDispatchResponse; |
||
127 | } |
||
128 | |||
129 | // Call action method |
||
130 | $actionResponse = \call_user_func_array([$this, $actionMethod], $collectedArgs); |
||
131 | |||
132 | $this->postDispatch(); |
||
133 | |||
134 | return $this->getDispatchResponse($action, $actionResponse); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param string $actionMethod |
||
139 | * @param array $actionArgs |
||
140 | * @return array |
||
141 | * @throws Routing\Exception\ResourceNotFoundException |
||
142 | */ |
||
143 | protected function getCollectedDispatchArgs($actionMethod, array $actionArgs = []) |
||
144 | { |
||
145 | // Check that method is a valid action method |
||
146 | try { |
||
147 | $reflectionMethod = new \ReflectionMethod($this, $actionMethod); |
||
148 | if (!$reflectionMethod->isPublic() || $reflectionMethod->isConstructor()) { |
||
149 | throw new Routing\Exception\ResourceNotFoundException( |
||
150 | "\"{$actionMethod}\" is not a valid action method." |
||
151 | ); |
||
152 | } |
||
153 | } catch (\ReflectionException $e) { |
||
154 | throw new Routing\Exception\ResourceNotFoundException("\"{$actionMethod}\" action method does not exist."); |
||
155 | } |
||
156 | |||
157 | |||
158 | // Get action arguments from request or method default values. |
||
159 | // Throws an exception if arguments are not provided. |
||
160 | $collectedArgs = []; |
||
161 | foreach ($reflectionMethod->getParameters() as $param) { |
||
162 | if (array_key_exists($param->name, $actionArgs)) { |
||
163 | $collectedArgs[] = $actionArgs[$param->name]; |
||
164 | } elseif ($this->request->attributes->has($param->name)) { |
||
165 | $collectedArgs[] = $this->request->attributes->get($param->name); |
||
166 | } elseif ($param->isDefaultValueAvailable()) { |
||
167 | $collectedArgs[] = $param->getDefaultValue(); |
||
168 | } else { |
||
169 | throw new \LogicException( |
||
170 | "Action method \"{$actionMethod}\" requires that you provide a value for the \"\${$param->name}\"" |
||
171 | ); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return $collectedArgs; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $action |
||
180 | * @param mixed $actionResponse |
||
181 | * @return Response |
||
182 | */ |
||
183 | protected function getDispatchResponse($action, $actionResponse) |
||
184 | { |
||
185 | if ($actionResponse instanceof Response) { |
||
186 | return $actionResponse->prepare($this->request); |
||
187 | } elseif ($actionResponse !== null) { |
||
188 | return $this->app->get(Response::class)->setContent((string) $actionResponse)->prepare($this->request); |
||
189 | } elseif ($this->autoRenderView) { |
||
190 | $viewScript = $this->autoRenderViewScript ?: $this->getAutoRenderViewScriptName( |
||
191 | $action, |
||
192 | $this->app->get(RouterInterface::class)->getRequestController($this->request), |
||
193 | $this->app->get(RouterInterface::class)->getRequestModule($this->request) |
||
194 | ); |
||
195 | |||
196 | return $this->app->get(Response::class)->setContent($this->view->render($viewScript, true)) |
||
197 | ->prepare($this->request); |
||
198 | } else { |
||
199 | // Empty response if no other response is set |
||
200 | return $this->app->get(Response::class)->setContent('') |
||
201 | ->prepare($this->request); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param string $action |
||
207 | * @param string $controller |
||
208 | * @param string|null $module |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getAutoRenderViewScriptName($action, $controller, $module = null) |
||
212 | { |
||
213 | $viewScriptName = implode('/', array_filter([$module, $controller, $action])); |
||
214 | |||
215 | return $viewScriptName; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Post dispatch method meant to be overridden in descendant classes (optional). |
||
220 | * This method is called right after an action method has returned it's response, |
||
221 | * but before the dispatch method returns the response. |
||
222 | */ |
||
223 | protected function postDispatch() |
||
224 | { |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Forwards request to another action and/or controller |
||
229 | * |
||
230 | * @param string $action Action name as lowercase separated string |
||
231 | * @param string|null $controller Controller name as lowercase separated string |
||
232 | * @param string|null $module Module name as lowercase separated string |
||
233 | * @param array $actionArgs |
||
234 | * @return Response |
||
235 | */ |
||
236 | 3 | protected function forward($action, $controller = null, $module = null, array $actionArgs = []) |
|
237 | { |
||
238 | // Forward inside same controller (easy) |
||
239 | 3 | if (empty($controller)) { |
|
240 | 1 | return $this->dispatch($action, $actionArgs); |
|
241 | // Forward to another controller |
||
242 | } else { |
||
243 | 2 | $router = $this->app->get(RouterInterface::class); |
|
244 | 2 | $controller = $controller ?: $router->getRequestController($this->request); |
|
245 | 2 | $module = $module ?: $router->getRequestModule($this->request); |
|
246 | |||
247 | 1 | $controllerClass = $router->getControllerClass($controller, $module); |
|
248 | $actualController = new $controllerClass($this->app, $this->request); |
||
249 | |||
250 | // Set new request properties |
||
251 | $this->request->attributes->add(compact('module', 'controller', 'action')); |
||
252 | |||
253 | return $actualController->dispatch($action, $actionArgs); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Get current or a new url merged with provided parameters. |
||
259 | * |
||
260 | * @param string $relativeUrl |
||
261 | * @param array $parameters |
||
262 | * @return string |
||
263 | */ |
||
264 | 2 | protected function getUrl($relativeUrl = null, array $parameters = []) |
|
280 | |||
281 | /** |
||
282 | * Shortcut method to access GET/query parameters. |
||
283 | * |
||
284 | * @param string $key |
||
285 | * @param mixed $default |
||
286 | * @return string|array |
||
287 | */ |
||
288 | 2 | View Code Duplication | protected function get($key = null, $default = null) |
296 | |||
297 | /** |
||
298 | * Shortcut method to access POST/request parameters. |
||
299 | * |
||
300 | * @param string $key |
||
301 | * @param mixed $default |
||
302 | * @return string|array |
||
303 | */ |
||
304 | 1 | View Code Duplication | protected function post($key = null, $default = null) |
312 | } |
||
313 |