| Total Complexity | 49 |
| Total Lines | 537 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 |
||
| 25 | class BaseController |
||
| 26 | { |
||
| 27 | use HelperTrait; |
||
| 28 | |||
| 29 | public $appLangFiles = []; |
||
| 30 | |||
| 31 | public $appThemes = []; |
||
| 32 | |||
| 33 | public $appName = ''; |
||
| 34 | |||
| 35 | public $appVersion = ''; |
||
| 36 | |||
| 37 | public $form = ''; |
||
| 38 | |||
| 39 | public $href = ''; |
||
| 40 | |||
| 41 | public $lang = []; |
||
| 42 | |||
| 43 | public $action = ''; |
||
| 44 | |||
| 45 | public $controller_name; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Used. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public $view_name; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Used to print the title passing its value to $lang. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $controller_title = 'base'; |
||
| 60 | |||
| 61 | public $msg = ''; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ViewManager |
||
| 65 | */ |
||
| 66 | public $view; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Misc |
||
| 70 | */ |
||
| 71 | public $misc; |
||
| 72 | |||
| 73 | public $conf; |
||
| 74 | |||
| 75 | public $phpMinVer; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var ContainerUtils |
||
| 79 | */ |
||
| 80 | protected $container; |
||
| 81 | |||
| 82 | protected $script; |
||
| 83 | |||
| 84 | protected $data; |
||
| 85 | |||
| 86 | protected $database; |
||
| 87 | |||
| 88 | protected $server_id; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var XHtml\HTMLTableController |
||
| 92 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 93 | */ |
||
| 94 | protected $_table_controller; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var XHtml\HTMLFooterController |
||
| 98 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 99 | */ |
||
| 100 | protected $_footer_controller; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var XHtml\HTMLHeaderController |
||
| 104 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 105 | */ |
||
| 106 | protected $_header_controller; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var XHtml\HTMLNavbarController |
||
| 110 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 111 | */ |
||
| 112 | protected $_trail_controller; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var TreeController |
||
| 116 | * @psalm-suppress PropertyNotSetInConstructor |
||
| 117 | */ |
||
| 118 | protected $_tree_controller; |
||
| 119 | |||
| 120 | protected $scripts = ''; |
||
| 121 | |||
| 122 | protected $no_db_connection = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Constructs the base controller (common for almost all controllers). |
||
| 126 | * |
||
| 127 | * @param ContainerUtils $container the $app container |
||
| 128 | * @param bool $no_db_connection [optional] if true, sets $this->misc->setNoDBConnection(true); |
||
| 129 | */ |
||
| 130 | public function __construct(ContainerUtils $container) |
||
| 131 | { |
||
| 132 | $this->container = $container; |
||
| 133 | $this->lang = $container->get('lang'); |
||
| 134 | |||
| 135 | $this->controller_name = \str_replace(__NAMESPACE__ . '\\', '', \get_class($this)); |
||
| 136 | $this->view_name = \str_replace('controller', '', \mb_strtolower($this->controller_name)); |
||
| 137 | $this->script = $this->view_name; |
||
| 138 | |||
| 139 | $this->view = $container->get('view'); |
||
| 140 | |||
| 141 | $this->msg = $container->get('msg'); |
||
| 142 | $this->appLangFiles = $container->get('appLangFiles'); |
||
| 143 | |||
| 144 | $this->misc = $container->get('misc'); |
||
| 145 | $this->conf = $this->misc->getConf(); |
||
| 146 | |||
| 147 | $this->appThemes = $container->get('appThemes'); |
||
| 148 | $this->action = $container->get('action'); |
||
| 149 | |||
| 150 | $this->appName = $container->get('settings')['appName']; |
||
| 151 | $this->appVersion = $container->get('settings')['appVersion']; |
||
| 152 | $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer']; |
||
| 153 | $this->phpMinVer = $container->get('settings')['phpMinVer']; |
||
| 154 | |||
| 155 | $msg = $container->get('msg'); |
||
| 156 | |||
| 157 | if (true === $this->no_db_connection) { |
||
| 158 | $this->misc->setNoDBConnection(true); |
||
| 159 | } |
||
| 160 | |||
| 161 | if (!$this->container->IN_TEST) { |
||
| 162 | $this->renderInitialPageIfNotLogged(); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Default method to render the controller according to the action parameter. It should return with a PSR |
||
| 168 | * responseObject but it prints texts whatsoeever. |
||
| 169 | * |
||
| 170 | * @return string|void |
||
| 171 | */ |
||
| 172 | public function render() |
||
| 173 | { |
||
| 174 | $this->misc = $this->misc; |
||
| 175 | |||
| 176 | $this->printHeader(); |
||
| 177 | $this->printBody(); |
||
| 178 | |||
| 179 | switch ($this->action) { |
||
| 180 | default: |
||
| 181 | $this->doDefault(); |
||
| 182 | |||
| 183 | break; |
||
| 184 | } |
||
| 185 | |||
| 186 | $this->printFooter(); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function doDefault() |
||
| 190 | { |
||
| 191 | $html = '<div><h2>Section title</h2> <p>Main content</p></div>'; |
||
| 192 | echo $html; |
||
| 193 | |||
| 194 | return $html; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the page title for each controller. |
||
| 199 | * |
||
| 200 | * @param string $title The title |
||
| 201 | * @param string $prefix The prefix |
||
| 202 | * @param string $suffix The suffix |
||
| 203 | * |
||
| 204 | * @return string the page title |
||
| 205 | */ |
||
| 206 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
| 207 | { |
||
| 208 | $title = $title ? $title : $this->controller_title; |
||
| 209 | |||
| 210 | return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : ''); |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getContainer() |
||
| 214 | { |
||
| 215 | return $this->container; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Display a table of data. |
||
| 220 | * |
||
| 221 | * @param ADORecordSet|ArrayRecordSet $tabledata a set of data to be formatted |
||
| 222 | * @param array $columns An associative array of columns to be displayed: |
||
| 223 | * @param array $actions Actions that can be performed on each object: |
||
| 224 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
| 225 | * @param string $nodata (optional) Message to display if data set is empty |
||
| 226 | * @param callable $pre_fn (optional) callback closure for each row |
||
| 227 | * |
||
| 228 | * @return string the html of the table |
||
| 229 | */ |
||
| 230 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
| 231 | { |
||
| 232 | $html_table = $this->_getTableController(); |
||
| 233 | |||
| 234 | $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
| 235 | |||
| 236 | return $html_table->printTable(); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Hides or show tree tabs according to their properties. |
||
| 241 | * |
||
| 242 | * @param array $tabs The tabs |
||
| 243 | * |
||
| 244 | * @return ADORecordSet|ArrayRecordSet filtered tabs in the form of an ArrayRecordSet |
||
| 245 | */ |
||
| 246 | public function adjustTabsForTree(&$tabs) |
||
| 247 | { |
||
| 248 | $tree = $this->_getTreeController(); |
||
| 249 | |||
| 250 | return $tree->adjustTabsForTree($tabs); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Produce JSON data for the browser tree. |
||
| 255 | * |
||
| 256 | * @param ADORecordSet|ArrayRecordSet $_treedata a set of records to populate the tree |
||
| 257 | * @param array $attrs Attributes for tree items |
||
| 258 | * @param string $section The section where the branch is linked in the tree |
||
| 259 | * @param bool $print either to return or echo the result |
||
| 260 | * |
||
| 261 | * @return Response|string the json rendered tree |
||
| 262 | */ |
||
| 263 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 264 | { |
||
| 265 | $tree = $this->_getTreeController(); |
||
| 266 | |||
| 267 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
|
|
|||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Prints a trail. |
||
| 272 | * |
||
| 273 | * @param array|string $trail |
||
| 274 | * @param bool $do_print The do print |
||
| 275 | * |
||
| 276 | * @return string ( description_of_the_return_value ) |
||
| 277 | */ |
||
| 278 | public function printTrail($trail = [], bool $do_print = true) |
||
| 279 | { |
||
| 280 | $from = __METHOD__; |
||
| 281 | $html_trail = $this->_getNavbarController(); |
||
| 282 | |||
| 283 | return $html_trail->printTrail($trail, $do_print, $from); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param (array[][]|mixed)[][] $navlinks |
||
| 288 | * @param string $place |
||
| 289 | * @param array $env |
||
| 290 | * @param mixed $do_print |
||
| 291 | */ |
||
| 292 | public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true) |
||
| 293 | { |
||
| 294 | $from = __METHOD__; |
||
| 295 | $footer_controller = $this->_getFooterController(); |
||
| 296 | |||
| 297 | return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function printTabs(string $tabs, string $activetab, bool $do_print = true) |
||
| 301 | { |
||
| 302 | $from = __METHOD__; |
||
| 303 | $html_trail = $this->_getNavbarController(); |
||
| 304 | |||
| 305 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param true $do_print |
||
| 310 | * @param null|string $from |
||
| 311 | * @param mixed $link |
||
| 312 | */ |
||
| 313 | public function printLink($link, bool $do_print = true, ?string $from = null) |
||
| 314 | { |
||
| 315 | if (null === $from) { |
||
| 316 | $from = __METHOD__; |
||
| 317 | } |
||
| 318 | |||
| 319 | $html_trail = $this->_getNavbarController(); |
||
| 320 | |||
| 321 | return $html_trail->printLink($link, $do_print, $from); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param true $flag |
||
| 326 | */ |
||
| 327 | public function setReloadDropDatabase(bool $flag) |
||
| 328 | { |
||
| 329 | $footer_controller = $this->_getFooterController(); |
||
| 330 | |||
| 331 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param true $flag |
||
| 336 | */ |
||
| 337 | public function setNoBottomLink(bool $flag) |
||
| 338 | { |
||
| 339 | $footer_controller = $this->_getFooterController(); |
||
| 340 | |||
| 341 | return $footer_controller->setNoBottomLink($flag); |
||
| 342 | } |
||
| 343 | |||
| 344 | public function printFooter(bool $doBody = true, string $template = 'footer.twig') |
||
| 345 | { |
||
| 346 | $footer_controller = $this->_getFooterController(); |
||
| 347 | |||
| 348 | return $footer_controller->printFooter($doBody, $template); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function printReload($database, $do_print = true) |
||
| 352 | { |
||
| 353 | $footer_controller = $this->_getFooterController(); |
||
| 354 | |||
| 355 | return $footer_controller->printReload($database, $do_print); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Outputs JavaScript to set default focus. |
||
| 360 | * |
||
| 361 | * @param mixed $object eg. forms[0].username |
||
| 362 | */ |
||
| 363 | public function setFocus($object) |
||
| 364 | { |
||
| 365 | $footer_controller = $this->_getFooterController(); |
||
| 366 | |||
| 367 | return $footer_controller->setFocus($object); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Outputs JavaScript to set the name of the browser window. |
||
| 372 | * |
||
| 373 | * @param string $name the window name |
||
| 374 | * @param bool $addServer if true (default) then the server id is |
||
| 375 | * attached to the name |
||
| 376 | */ |
||
| 377 | public function setWindowName($name, $addServer = true) |
||
| 378 | { |
||
| 379 | $footer_controller = $this->_getFooterController(); |
||
| 380 | |||
| 381 | return $footer_controller->setWindowName($name, $addServer); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param true $flag |
||
| 386 | */ |
||
| 387 | public function setNoOutput(bool $flag) |
||
| 388 | { |
||
| 389 | $header_controller = $this->_getHeaderController(); |
||
| 390 | |||
| 391 | return $header_controller->setNoOutput((bool) $flag); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param null|string $script |
||
| 396 | * @param string $title |
||
| 397 | * @param bool $do_print |
||
| 398 | * @param string $template |
||
| 399 | */ |
||
| 400 | public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig') |
||
| 401 | { |
||
| 402 | $title = $title ? $title : $this->headerTitle(); |
||
| 403 | $header_controller = $this->_getHeaderController(); |
||
| 404 | |||
| 405 | return $header_controller->printHeader($title, $script, $do_print, $template); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false) |
||
| 409 | { |
||
| 410 | $header_controller = $this->_getHeaderController(); |
||
| 411 | |||
| 412 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param null|string $help |
||
| 417 | * @param string $title |
||
| 418 | * @param bool $do_print |
||
| 419 | */ |
||
| 420 | public function printTitle(string $title, ?string $help = null, bool $do_print = true) |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $key |
||
| 429 | * @param null|string $default |
||
| 430 | */ |
||
| 431 | public function getRequestParam(string $key, ?string $default = null) |
||
| 432 | { |
||
| 433 | return \requestInstance()->getParam($key, $default); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $key |
||
| 438 | * @param null|array|bool|float|int|string $default |
||
| 439 | * |
||
| 440 | * @return bool| null|array|string|int|float |
||
| 441 | */ |
||
| 442 | public function getPostParam(string $key, $default = null) |
||
| 443 | { |
||
| 444 | return \requestInstance()->getParsedBodyParam($key, $default); |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $key |
||
| 449 | * @param null|array|float|int|string $default |
||
| 450 | * |
||
| 451 | * @return null|array|float|int|string |
||
| 452 | */ |
||
| 453 | public function getQueryStrinParam($key, $default = null) |
||
| 454 | { |
||
| 455 | return \requestInstance()->getQueryParam($key, $default); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | public function getAllParams(): array |
||
| 462 | { |
||
| 463 | return \array_merge( |
||
| 464 | \requestInstance()->getQueryParams() ?? [], |
||
| 465 | \requestInstance()->getParsedBody() ?? [] |
||
| 466 | ); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Print out a message. |
||
| 471 | * |
||
| 472 | * @param string $msg The message |
||
| 473 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
| 474 | * |
||
| 475 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
| 476 | */ |
||
| 477 | public function printMsg($msg, $do_print = true) |
||
| 478 | { |
||
| 479 | $html = ''; |
||
| 480 | $msg = \htmlspecialchars(ContainerUtils::br2ln($msg)); |
||
| 481 | |||
| 482 | if ('' !== $msg) { |
||
| 483 | $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL; |
||
| 484 | } |
||
| 485 | |||
| 486 | if ($do_print) { |
||
| 487 | echo $html; |
||
| 488 | |||
| 489 | return $html; |
||
| 490 | } |
||
| 491 | |||
| 492 | return $html; |
||
| 493 | } |
||
| 494 | |||
| 495 | private function renderInitialPageIfNotLogged(): void |
||
| 496 | { |
||
| 497 | if (false === $this->misc->getNoDBConnection()) { |
||
| 498 | if (null === $this->misc->getServerId()) { |
||
| 499 | $servers_controller = new ServersController($this->container); |
||
| 500 | |||
| 501 | $servers_controller->render(); |
||
| 502 | } else { |
||
| 503 | $_server_info = $this->misc->getServerInfo(); |
||
| 504 | // Redirect to the login form if not logged in |
||
| 505 | if (!isset($_server_info['username'])) { |
||
| 506 | $msg = \sprintf( |
||
| 507 | $this->lang['strlogoutmsg'], |
||
| 508 | $_server_info['desc'] |
||
| 509 | ); |
||
| 510 | |||
| 511 | $servers_controller = new ServersController($container); |
||
| 512 | |||
| 513 | $servers_controller->render(); |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 | } |
||
| 518 | |||
| 519 | private function _getTableController() |
||
| 520 | { |
||
| 521 | if (null === $this->_table_controller) { |
||
| 522 | $this->_table_controller = new HTMLTableController($this->getContainer(), $this->controller_name); |
||
| 523 | } |
||
| 524 | |||
| 525 | return $this->_table_controller; |
||
| 526 | } |
||
| 527 | |||
| 528 | private function _getFooterController() |
||
| 529 | { |
||
| 530 | if (null === $this->_footer_controller) { |
||
| 531 | $this->_footer_controller = new HTMLFooterController($this->getContainer(), $this->controller_name); |
||
| 532 | } |
||
| 533 | |||
| 534 | return $this->_footer_controller; |
||
| 535 | } |
||
| 536 | |||
| 537 | private function _getHeaderController() |
||
| 544 | } |
||
| 545 | |||
| 546 | private function _getNavbarController() |
||
| 547 | { |
||
| 548 | if (null === $this->_trail_controller) { |
||
| 549 | $this->_trail_controller = new HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
| 550 | } |
||
| 551 | |||
| 552 | return $this->_trail_controller; |
||
| 553 | } |
||
| 554 | |||
| 555 | private function _getTreeController() |
||
| 562 | } |
||
| 563 | } |
||
| 564 |