Total Complexity | 50 |
Total Lines | 462 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like BaseController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class BaseController |
||
14 | { |
||
15 | use \PHPPgAdmin\Traits\HelperTrait; |
||
16 | |||
17 | protected $container; |
||
18 | protected $app; |
||
19 | protected $data; |
||
20 | protected $database; |
||
21 | protected $server_id; |
||
22 | public $appLangFiles = []; |
||
23 | public $appThemes = []; |
||
24 | public $appName = ''; |
||
25 | public $appVersion = ''; |
||
26 | public $form = ''; |
||
27 | public $href = ''; |
||
28 | public $lang = []; |
||
29 | public $action = ''; |
||
30 | public $controller_name; |
||
31 | |||
32 | /** |
||
33 | * Used. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | public $view_name; |
||
38 | /** |
||
39 | * Used to print the title passing its value to $lang. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $controller_title = 'base'; |
||
44 | protected $table_controller; |
||
45 | protected $trail_controller; |
||
46 | private static $_tree_controller = null; |
||
47 | |||
48 | protected $footer_controller; |
||
49 | protected $header_controller; |
||
50 | protected $scripts = ''; |
||
51 | public $msg = ''; |
||
52 | public $view; |
||
53 | public $plugin_manager; |
||
54 | public $misc; |
||
55 | public $conf; |
||
56 | public $phpMinVer; |
||
57 | protected $no_db_connection = false; |
||
58 | |||
59 | /** |
||
60 | * Constructs the base controller (common for almost all controllers). |
||
61 | * |
||
62 | * @param \Slim\Container $container the $app container |
||
63 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
64 | */ |
||
65 | public function __construct(\Slim\Container $container) |
||
66 | { |
||
67 | $this->container = $container; |
||
68 | $this->lang = $container->get('lang'); |
||
69 | |||
70 | $this->controller_name = str_replace(__NAMESPACE__ . '\\', '', get_class($this)); |
||
71 | $this->view_name = str_replace('controller', '', strtolower($this->controller_name)); |
||
72 | $this->script = $this->view_name; |
||
73 | |||
74 | $this->view = $container->get('view'); |
||
75 | $this->plugin_manager = $container->get('plugin_manager'); |
||
76 | $this->msg = $container->get('msg'); |
||
77 | $this->appLangFiles = $container->get('appLangFiles'); |
||
78 | |||
79 | $this->misc = $container->get('misc'); |
||
80 | $this->conf = $this->misc->getConf(); |
||
81 | |||
82 | $this->appThemes = $container->get('appThemes'); |
||
83 | $this->action = $container->get('action'); |
||
84 | |||
85 | $this->appName = $container->get('settings')['appName']; |
||
86 | $this->appVersion = $container->get('settings')['appVersion']; |
||
87 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
88 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
89 | |||
90 | $msg = $container->get('msg'); |
||
91 | |||
92 | if (true === $this->no_db_connection) { |
||
93 | $this->misc->setNoDBConnection(true); |
||
94 | } |
||
95 | |||
96 | if (false === $this->misc->getNoDBConnection()) { |
||
97 | if (null === $this->misc->getServerId()) { |
||
98 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
99 | |||
100 | return $servers_controller->render(); |
||
101 | } |
||
102 | $_server_info = $this->misc->getServerInfo(); |
||
103 | // Redirect to the login form if not logged in |
||
104 | if (!isset($_server_info['username'])) { |
||
105 | $msg = sprintf($this->lang['strlogoutmsg'], $_server_info['desc']); |
||
106 | |||
107 | $servers_controller = new \PHPPgAdmin\Controller\ServersController($container); |
||
108 | |||
109 | return $servers_controller->render(); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Default method to render the controller according to the action parameter. |
||
116 | */ |
||
117 | public function render() |
||
118 | { |
||
119 | $this->misc = $this->misc; |
||
120 | |||
121 | $this->printHeader(); |
||
122 | $this->printBody(); |
||
123 | |||
124 | switch ($this->action) { |
||
125 | default: |
||
126 | $this->doDefault(); |
||
127 | |||
128 | break; |
||
129 | } |
||
130 | |||
131 | $this->printFooter(); |
||
132 | } |
||
133 | |||
134 | public function doDefault() |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Returns the page title for each controller. |
||
144 | * |
||
145 | * @param string $title The title |
||
146 | * @param string $prefix The prefix |
||
147 | * @param string $suffix The suffix |
||
148 | * |
||
149 | * @return string the page title |
||
150 | */ |
||
151 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
152 | { |
||
153 | $title = $title ? $title : $this->controller_title; |
||
154 | |||
155 | return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : ''); |
||
156 | } |
||
157 | |||
158 | public function getContainer() |
||
159 | { |
||
160 | return $this->container; |
||
161 | } |
||
162 | |||
163 | private function _getTableController() |
||
164 | { |
||
165 | if (null === $this->table_controller) { |
||
166 | $this->prtrace(__METHOD__); |
||
167 | $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController( |
||
168 | $this->getContainer(), |
||
169 | $this->controller_name |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | return $this->table_controller; |
||
174 | } |
||
175 | |||
176 | private function _getFooterController() |
||
177 | { |
||
178 | if (null === $this->footer_controller) { |
||
179 | $this->prtrace(__METHOD__); |
||
180 | $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController( |
||
181 | $this->getContainer(), |
||
182 | $this->controller_name |
||
183 | ); |
||
184 | } |
||
185 | |||
186 | return $this->footer_controller; |
||
187 | } |
||
188 | |||
189 | private function _getHeaderController() |
||
190 | { |
||
191 | if (null === $this->header_controller) { |
||
192 | $this->prtrace(__METHOD__); |
||
193 | $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController( |
||
194 | $this->getContainer(), |
||
195 | $this->controller_name |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | return $this->header_controller; |
||
200 | } |
||
201 | |||
202 | private function _getNavbarController() |
||
203 | { |
||
204 | if (null === $this->trail_controller) { |
||
205 | $this->prtrace(__METHOD__); |
||
206 | $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController( |
||
207 | $this->getContainer(), |
||
208 | $this->controller_name |
||
209 | ); |
||
210 | } |
||
211 | |||
212 | return $this->trail_controller; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Gets the tree instance. |
||
217 | * |
||
218 | * @param \Slim\Container $container the $app container |
||
219 | * @param <type> $controller_name The controller name |
||
|
|||
220 | * |
||
221 | * @return \PHPPgAdmin\XHtml\TreeController The tree controller instance. |
||
222 | */ |
||
223 | private static function _getTreeInstance($container, $controller_name) |
||
224 | { |
||
225 | if (null === self::$_tree_controller) { |
||
226 | self::$_tree_controller = new \PHPPgAdmin\XHtml\TreeController( |
||
227 | $container, |
||
228 | $controller_name |
||
229 | ); |
||
230 | } |
||
231 | |||
232 | return self::$_tree_controller; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Gets the tree controller. |
||
237 | * |
||
238 | * @param string $from From where is this method being called, for debug purposes |
||
239 | * |
||
240 | * @return \PHPPgAdmin\XHtml\TreeController The tree controller. |
||
241 | */ |
||
242 | private function _getTreeController($from) |
||
243 | { |
||
244 | //$tree = self::_getTreeInstance($this->getContainer(), $this->controller_name); |
||
245 | $tree = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name); |
||
246 | $this->prtrace(__METHOD__); |
||
247 | return $tree; |
||
248 | |||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Display a table of data. |
||
253 | * |
||
254 | * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted |
||
255 | * @param array $columns An associative array of columns to be displayed: |
||
256 | * @param array $actions Actions that can be performed on each object: |
||
257 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
258 | * @param string $nodata (optional) Message to display if data set is empty |
||
259 | * @param callable $pre_fn (optional) callback closure for each row |
||
260 | * |
||
261 | * @return string the html of the table |
||
262 | */ |
||
263 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
264 | { |
||
265 | $html_table = $this->_getTableController(); |
||
266 | |||
267 | $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
268 | |||
269 | return $html_table->printTable(); |
||
270 | } |
||
271 | |||
272 | public function adjustTabsForTree($tabs) |
||
273 | { |
||
274 | //$tree = self::_getTreeInstance($this->getContainer(), $this->controller_name); |
||
275 | $tree = $this->_getTreeController(__METHOD__); |
||
276 | |||
277 | return $tree->adjustTabsForTree($tabs); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Produce JSON data for the browser tree. |
||
282 | * |
||
283 | * @param \PHPPgAdmin\ArrayRecordSet $_treedata a set of records to populate the tree |
||
284 | * @param array $attrs Attributes for tree items |
||
285 | * @param string $section The section where the branch is linked in the tree |
||
286 | * |
||
287 | * @return string the json rendered tree |
||
288 | */ |
||
289 | public function printTree(&$_treedata, &$attrs, $section) |
||
290 | { |
||
291 | |||
292 | $tree = $this->_getTreeController(__METHOD__); |
||
293 | |||
294 | $treedata = []; |
||
295 | |||
296 | if ($_treedata->recordCount() > 0) { |
||
297 | while (!$_treedata->EOF) { |
||
298 | $treedata[] = $_treedata->fields; |
||
299 | $_treedata->moveNext(); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | return $tree->printTreeJSON($treedata, $attrs, $section); |
||
304 | } |
||
305 | |||
306 | public function printTrail($trail = [], $do_print = true) |
||
312 | } |
||
313 | |||
314 | public function printNavLinks($navlinks, $place, $env = [], $do_print = true) |
||
315 | { |
||
316 | $from = __METHOD__; |
||
317 | $footer_controller = $this->_getFooterController(); |
||
318 | |||
319 | return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
320 | } |
||
321 | |||
322 | public function printTabs($tabs, $activetab, $do_print = true) |
||
323 | { |
||
324 | $from = __METHOD__; |
||
325 | $html_trail = $this->_getNavbarController(); |
||
326 | |||
327 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
328 | } |
||
329 | |||
330 | public function printLink($link, $do_print = true, $from = null) |
||
331 | { |
||
332 | if (null === $from) { |
||
333 | $from = __METHOD__; |
||
334 | } |
||
335 | |||
336 | $html_trail = $this->_getNavbarController(); |
||
337 | |||
338 | return $html_trail->printLink($link, $do_print, $from); |
||
339 | } |
||
340 | |||
341 | public function setReloadDropDatabase($flag) |
||
342 | { |
||
343 | $footer_controller = $this->_getFooterController(); |
||
344 | |||
345 | return $footer_controller->setReloadDropDatabase($flag); |
||
346 | } |
||
347 | |||
348 | public function setNoBottomLink($flag) |
||
349 | { |
||
350 | $footer_controller = $this->_getFooterController(); |
||
351 | |||
352 | return $footer_controller->setNoBottomLink($flag); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Prints or returns the controller's footer |
||
357 | * |
||
358 | * @param boolean $doBody If false, return the body instead of print. Defaults to true |
||
359 | * @param string $template The template to render |
||
360 | * @param array $viewParams Optional - extra view parameters |
||
361 | * |
||
362 | * @return <type> ( description_of_the_return_value ) |
||
363 | */ |
||
364 | public function printFooter($doBody = true, $template = 'footer.twig', $viewParams = []) |
||
365 | { |
||
366 | $footer_controller = $this->_getFooterController(); |
||
367 | |||
368 | $footer_html = $footer_controller->getFooter($template, $viewParams); |
||
369 | if ($doBody) { |
||
370 | echo $footer_html; |
||
371 | } else { |
||
372 | return $footer_html; |
||
373 | } |
||
374 | } |
||
375 | |||
376 | public function printReload($database, $do_print = true) |
||
377 | { |
||
378 | $footer_controller = $this->_getFooterController(); |
||
379 | |||
380 | return $footer_controller->printReload($database, $do_print); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Outputs JavaScript to set default focus. |
||
385 | * |
||
386 | * @param mixed $object eg. forms[0].username |
||
387 | */ |
||
388 | public function setFocus($object) |
||
389 | { |
||
390 | $footer_controller = $this->_getFooterController(); |
||
391 | |||
392 | return $footer_controller->setFocus($object); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Outputs JavaScript to set the name of the browser window. |
||
397 | * |
||
398 | * @param string $name the window name |
||
399 | * @param bool $addServer if true (default) then the server id is |
||
400 | * attached to the name |
||
401 | */ |
||
402 | public function setWindowName($name, $addServer = true) |
||
403 | { |
||
404 | $footer_controller = $this->_getFooterController(); |
||
405 | |||
406 | return $footer_controller->setWindowName($name, $addServer); |
||
407 | } |
||
408 | |||
409 | public function setNoOutput($flag) |
||
410 | { |
||
411 | $header_controller = $this->_getHeaderController(); |
||
412 | |||
413 | return $header_controller->setNoOutput((bool) $flag); |
||
414 | } |
||
415 | |||
416 | public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig') |
||
422 | } |
||
423 | |||
424 | public function printBody($doBody = true, $bodyClass = 'detailbody', $onloadInit = false) |
||
425 | { |
||
426 | $header_controller = $this->_getHeaderController(); |
||
427 | |||
428 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
429 | } |
||
430 | |||
431 | public function printTitle($title, $help = null, $do_print = true) |
||
436 | } |
||
437 | |||
438 | public function getRequestParam($key, $default = null) |
||
439 | { |
||
440 | return $this->container->requestobj->getParam($key, $default); |
||
441 | } |
||
442 | |||
443 | public function getPostParam($key, $default = null) |
||
444 | { |
||
445 | return $this->container->requestobj->getParsedBodyParam($key, $default); |
||
446 | } |
||
447 | |||
448 | public function getQueryParam($key, $default = null) |
||
449 | { |
||
450 | return $this->container->requestobj->getQueryParam($key, $default); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Print out a message. |
||
455 | * |
||
456 | * @param string $msg The message |
||
457 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
458 | * |
||
459 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
460 | */ |
||
461 | public function printMsg($msg, $do_print = true) |
||
475 | } |
||
476 | } |
||
477 |