1 | <?php declare(strict_types=1); |
||
17 | abstract class AbstractController |
||
18 | { |
||
19 | /** |
||
20 | * @var BaseApp |
||
21 | */ |
||
22 | protected $app; |
||
23 | |||
24 | /** |
||
25 | * @var Request |
||
26 | */ |
||
27 | protected $request; |
||
28 | |||
29 | /** |
||
30 | * @var View |
||
31 | */ |
||
32 | protected $view; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $autoRenderView = true; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $autoRenderViewScript; |
||
43 | |||
44 | 29 | final public function __construct(BaseApp $app, Request $request) |
|
54 | |||
55 | /** |
||
56 | * Initialization method meant to be overridden in descendant classes (optional). |
||
57 | */ |
||
58 | 29 | protected function init() |
|
61 | |||
62 | /** |
||
63 | * Pre dispatch method meant to be overridden in descendant classes (optional). |
||
64 | * |
||
65 | * This method is called right before the actual action method is called/dispatched. |
||
66 | * Override this instead of init() if access to dispatch properties is required (like |
||
67 | * action name) or you need to return a response. |
||
68 | * |
||
69 | * @param string $action |
||
70 | * @return Response|null |
||
71 | */ |
||
72 | 7 | protected function preDispatch(string $action): ?Response |
|
76 | |||
77 | 2 | public function setAutoRenderView(bool $autoRenderView): void |
|
81 | |||
82 | 1 | public function setAutoRenderViewScript(string $autoRenderViewScript): void |
|
86 | |||
87 | /** |
||
88 | * Dispatch the requested action |
||
89 | * |
||
90 | * @param string|null $action action id/name (lowercase, - word separation) |
||
91 | * @param array $actionArgs |
||
92 | * @return Response |
||
93 | * @throws Routing\Exception\ResourceNotFoundException |
||
94 | */ |
||
95 | 11 | public function dispatch(string $action = null, array $actionArgs = []): Response |
|
119 | |||
120 | /** |
||
121 | * @throws Routing\Exception\ResourceNotFoundException |
||
122 | */ |
||
123 | 11 | protected function getCollectedDispatchArgs(string $actionMethod, array $actionArgs = []): array |
|
148 | |||
149 | /** |
||
150 | * @param string $action |
||
151 | * @param mixed $actionResponse |
||
152 | * @return Response |
||
153 | */ |
||
154 | 7 | protected function getDispatchResponse(string $action, $actionResponse): Response |
|
175 | |||
176 | 1 | public function getAutoRenderViewScriptName(string $action, string $controller, string $module = null): string |
|
182 | |||
183 | /** |
||
184 | * Post dispatch method meant to be overridden in descendant classes (optional). |
||
185 | * This method is called right after an action method has returned it's response, |
||
186 | * but before the dispatch method returns the response. |
||
187 | */ |
||
188 | 7 | protected function postDispatch(): void |
|
191 | |||
192 | /** |
||
193 | * Forwards request to another action and/or controller |
||
194 | * |
||
195 | * @param string $action Action name as lowercase separated string |
||
196 | * @param string|null $controller Controller name as lowercase separated string |
||
197 | * @param string|null $module Module name as lowercase separated string |
||
198 | * @param array $actionArgs |
||
199 | * @return Response |
||
200 | */ |
||
201 | 3 | protected function forward( |
|
225 | |||
226 | /** |
||
227 | * Get current or a new url merged with provided parameters. |
||
228 | * |
||
229 | * @param string $relativeUrl |
||
230 | * @param array $parameters |
||
231 | * @return string |
||
232 | */ |
||
233 | 2 | protected function getUrl(string $relativeUrl = null, array $parameters = []): string |
|
249 | |||
250 | /** |
||
251 | * Shortcut method to access GET/query parameters. |
||
252 | * |
||
253 | * @param string $key |
||
254 | * @param mixed $default |
||
255 | * @return mixed|null array if the key is null, otherwise the value for the key or its default value |
||
256 | * which is null by default |
||
257 | */ |
||
258 | 2 | protected function get(string $key = null, $default = null) |
|
266 | |||
267 | /** |
||
268 | * Shortcut method to access POST/request parameters. |
||
269 | * |
||
270 | * @param string $key |
||
271 | * @param mixed $default |
||
272 | * @return string|array |
||
273 | */ |
||
274 | 1 | protected function post(string $key = null, $default = null) |
|
282 | } |
||
283 |