| Total Complexity | 49 |
| Total Lines | 556 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 | public function render() |
||
| 171 | { |
||
| 172 | $this->misc = $this->misc; |
||
| 173 | |||
| 174 | $this->printHeader(); |
||
| 175 | $this->printBody(); |
||
| 176 | |||
| 177 | switch ($this->action) { |
||
| 178 | default: |
||
| 179 | $this->doDefault(); |
||
| 180 | |||
| 181 | break; |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->printFooter(); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function doDefault() |
||
| 191 | { |
||
| 192 | $html = '<div><h2>Section title</h2> <p>Main content</p></div>'; |
||
| 193 | echo $html; |
||
| 194 | |||
| 195 | return $html; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the page title for each controller. |
||
| 200 | * |
||
| 201 | * @param string $title The title |
||
| 202 | * @param string $prefix The prefix |
||
| 203 | * @param string $suffix The suffix |
||
| 204 | * |
||
| 205 | * @return string the page title |
||
| 206 | */ |
||
| 207 | public function headerTitle($title = '', $prefix = '', $suffix = '') |
||
| 208 | { |
||
| 209 | $title = $title ? $title : $this->controller_title; |
||
| 210 | |||
| 211 | return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : ''); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return ContainerUtils |
||
| 216 | */ |
||
| 217 | public function getContainer() |
||
| 218 | { |
||
| 219 | return $this->container; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Display a table of data. |
||
| 224 | * |
||
| 225 | * @param ADORecordSet|ArrayRecordSet $tabledata a set of data to be formatted |
||
| 226 | * @param array $columns An associative array of columns to be displayed: |
||
| 227 | * @param array $actions Actions that can be performed on each object: |
||
| 228 | * @param string $place Place where the $actions are displayed. Like 'display-browse', |
||
| 229 | * @param string $nodata (optional) Message to display if data set is empty |
||
| 230 | * @param callable $pre_fn (optional) callback closure for each row |
||
| 231 | * |
||
| 232 | * @return string the html of the table |
||
| 233 | */ |
||
| 234 | public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null) |
||
| 235 | { |
||
| 236 | $html_table = $this->_getTableController(); |
||
| 237 | |||
| 238 | $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn); |
||
| 239 | |||
| 240 | return $html_table->printTable(); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Hides or show tree tabs according to their properties. |
||
| 245 | * |
||
| 246 | * @param array $tabs The tabs |
||
| 247 | * |
||
| 248 | * @return ADORecordSet|ArrayRecordSet filtered tabs in the form of an ArrayRecordSet |
||
| 249 | */ |
||
| 250 | public function adjustTabsForTree(&$tabs) |
||
| 251 | { |
||
| 252 | $tree = $this->_getTreeController(); |
||
| 253 | |||
| 254 | return $tree->adjustTabsForTree($tabs); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Produce JSON data for the browser tree. |
||
| 259 | * |
||
| 260 | * @param ADORecordSet|ArrayRecordSet $_treedata a set of records to populate the tree |
||
| 261 | * @param array $attrs Attributes for tree items |
||
| 262 | * @param string $section The section where the branch is linked in the tree |
||
| 263 | * @param bool $print either to return or echo the result |
||
| 264 | * |
||
| 265 | * @return Response|string the json rendered tree |
||
| 266 | */ |
||
| 267 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 268 | { |
||
| 269 | $tree = $this->_getTreeController(); |
||
| 270 | |||
| 271 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Prints a trail. |
||
| 276 | * |
||
| 277 | * @param array|string $trail |
||
| 278 | * @param bool $do_print The do print |
||
| 279 | * |
||
| 280 | * @return string ( description_of_the_return_value ) |
||
| 281 | */ |
||
| 282 | public function printTrail($trail = [], bool $do_print = true) |
||
| 283 | { |
||
| 284 | $from = __METHOD__; |
||
| 285 | $html_trail = $this->_getNavbarController(); |
||
| 286 | |||
| 287 | return $html_trail->printTrail($trail, $do_print, $from); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param (array[][]|mixed)[][] $navlinks |
||
| 292 | * @param string $place |
||
| 293 | * @param array $env |
||
| 294 | * @param mixed $do_print |
||
| 295 | */ |
||
| 296 | public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true) |
||
| 297 | { |
||
| 298 | $from = __METHOD__; |
||
| 299 | $footer_controller = $this->_getFooterController(); |
||
| 300 | |||
| 301 | return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function printTabs(string $tabs, string $activetab, bool $do_print = true) |
||
| 305 | { |
||
| 306 | $from = __METHOD__; |
||
| 307 | $html_trail = $this->_getNavbarController(); |
||
| 308 | |||
| 309 | return $html_trail->printTabs($tabs, $activetab, $do_print, $from); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param true $do_print |
||
| 314 | * @param null|string $from |
||
| 315 | * @param mixed $link |
||
| 316 | */ |
||
| 317 | public function printLink($link, bool $do_print = true, ?string $from = null) |
||
| 318 | { |
||
| 319 | if (null === $from) { |
||
| 320 | $from = __METHOD__; |
||
| 321 | } |
||
| 322 | |||
| 323 | $html_trail = $this->_getNavbarController(); |
||
| 324 | |||
| 325 | return $html_trail->printLink($link, $do_print, $from); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param true $flag |
||
| 330 | */ |
||
| 331 | public function setReloadDropDatabase(bool $flag) |
||
| 332 | { |
||
| 333 | $footer_controller = $this->_getFooterController(); |
||
| 334 | |||
| 335 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param true $flag |
||
| 340 | */ |
||
| 341 | public function setNoBottomLink(bool $flag) |
||
| 342 | { |
||
| 343 | $footer_controller = $this->_getFooterController(); |
||
| 344 | |||
| 345 | return $footer_controller->setNoBottomLink($flag); |
||
| 346 | } |
||
| 347 | |||
| 348 | public function printFooter(bool $doBody = true, string $template = 'footer.twig') |
||
| 349 | { |
||
| 350 | $footer_controller = $this->_getFooterController(); |
||
| 351 | |||
| 352 | return $footer_controller->printFooter($doBody, $template); |
||
| 353 | } |
||
| 354 | |||
| 355 | public function printReload($database, $do_print = true) |
||
| 356 | { |
||
| 357 | $footer_controller = $this->_getFooterController(); |
||
| 358 | |||
| 359 | return $footer_controller->printReload($database, $do_print); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Outputs JavaScript to set default focus. |
||
| 364 | * |
||
| 365 | * @param mixed $object eg. forms[0].username |
||
| 366 | */ |
||
| 367 | public function setFocus($object) |
||
| 368 | { |
||
| 369 | $footer_controller = $this->_getFooterController(); |
||
| 370 | |||
| 371 | return $footer_controller->setFocus($object); |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Outputs JavaScript to set the name of the browser window. |
||
| 376 | * |
||
| 377 | * @param string $name the window name |
||
| 378 | * @param bool $addServer if true (default) then the server id is |
||
| 379 | * attached to the name |
||
| 380 | */ |
||
| 381 | public function setWindowName($name, $addServer = true) |
||
| 382 | { |
||
| 383 | $footer_controller = $this->_getFooterController(); |
||
| 384 | |||
| 385 | return $footer_controller->setWindowName($name, $addServer); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param true $flag |
||
| 390 | */ |
||
| 391 | public function setNoOutput(bool $flag) |
||
| 392 | { |
||
| 393 | $header_controller = $this->_getHeaderController(); |
||
| 394 | |||
| 395 | return $header_controller->setNoOutput((bool) $flag); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param null|string $script |
||
| 400 | * @param string $title |
||
| 401 | * @param bool $do_print |
||
| 402 | * @param string $template |
||
| 403 | */ |
||
| 404 | public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig') |
||
| 410 | } |
||
| 411 | |||
| 412 | public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false) |
||
| 413 | { |
||
| 414 | $header_controller = $this->_getHeaderController(); |
||
| 415 | |||
| 416 | return $header_controller->printBody($doBody, $bodyClass, $onloadInit); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param null|string $help |
||
| 421 | * @param string $title |
||
| 422 | * @param bool $do_print |
||
| 423 | */ |
||
| 424 | public function printTitle(string $title, ?string $help = null, bool $do_print = true) |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $key |
||
| 433 | * @param null|string $default |
||
| 434 | */ |
||
| 435 | public function getRequestParam(string $key, ?string $default = null) |
||
| 436 | { |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $key |
||
| 442 | * @param null|array|bool|float|int|string $default |
||
| 443 | * |
||
| 444 | * @return bool| null|array|string|int|float |
||
| 445 | */ |
||
| 446 | public function getPostParam(string $key, $default = null) |
||
| 447 | { |
||
| 448 | return \requestInstance()->getParsedBodyParam($key, $default); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $key |
||
| 453 | * @param null|array|float|int|string $default |
||
| 454 | * |
||
| 455 | * @return null|array|float|int|string |
||
| 456 | */ |
||
| 457 | public function getQueryStrinParam($key, $default = null) |
||
| 458 | { |
||
| 459 | return \requestInstance()->getQueryParam($key, $default); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return array |
||
| 464 | */ |
||
| 465 | public function getAllParams(): array |
||
| 466 | { |
||
| 467 | return \array_merge( |
||
| 468 | \requestInstance()->getQueryParams() ?? [], |
||
| 469 | \requestInstance()->getParsedBody() ?? [] |
||
| 470 | ); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Print out a message. |
||
| 475 | * |
||
| 476 | * @param string $msg The message |
||
| 477 | * @param bool $do_print if true, print the message. Return the string otherwise |
||
| 478 | * |
||
| 479 | * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements |
||
| 480 | */ |
||
| 481 | public function printMsg($msg, $do_print = true) |
||
| 482 | { |
||
| 483 | $html = ''; |
||
| 484 | $msg = \htmlspecialchars(ContainerUtils::br2ln($msg)); |
||
| 485 | |||
| 486 | if ('' !== $msg) { |
||
| 487 | $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL; |
||
| 488 | } |
||
| 489 | |||
| 490 | if ($do_print) { |
||
| 491 | echo $html; |
||
| 492 | |||
| 493 | return $html; |
||
| 494 | } |
||
| 495 | |||
| 496 | return $html; |
||
| 497 | } |
||
| 498 | |||
| 499 | private function renderInitialPageIfNotLogged(): void |
||
| 500 | { |
||
| 501 | if (false === $this->misc->getNoDBConnection()) { |
||
| 502 | if (null === $this->misc->getServerId()) { |
||
| 503 | $servers_controller = new ServersController($this->container); |
||
| 504 | |||
| 505 | $servers_controller->render(); |
||
| 506 | } else { |
||
| 507 | $_server_info = $this->misc->getServerInfo(); |
||
| 508 | // Redirect to the login form if not logged in |
||
| 509 | if (!isset($_server_info['username'])) { |
||
| 510 | $msg = \sprintf( |
||
| 511 | $this->lang['strlogoutmsg'], |
||
| 512 | $_server_info['desc'] |
||
| 513 | ); |
||
| 514 | |||
| 515 | $servers_controller = new ServersController($this->container); |
||
| 516 | |||
| 517 | $servers_controller->render(); |
||
| 518 | } |
||
| 519 | } |
||
| 520 | } |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @return HTMLTableController |
||
| 525 | */ |
||
| 526 | private function _getTableController() |
||
| 527 | { |
||
| 528 | if (null === $this->_table_controller) { |
||
| 529 | $this->_table_controller = new HTMLTableController($this->getContainer(), $this->controller_name); |
||
| 530 | } |
||
| 531 | |||
| 532 | return $this->_table_controller; |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * @return HTMLFooterController |
||
| 537 | */ |
||
| 538 | private function _getFooterController() |
||
| 539 | { |
||
| 540 | if (null === $this->_footer_controller) { |
||
| 541 | $this->_footer_controller = new HTMLFooterController($this->getContainer(), $this->controller_name); |
||
| 542 | } |
||
| 543 | |||
| 544 | return $this->_footer_controller; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @return HTMLHeaderController |
||
| 549 | */ |
||
| 550 | private function _getHeaderController() |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return HTMLNavbarController |
||
| 561 | */ |
||
| 562 | private function _getNavbarController() |
||
| 563 | { |
||
| 564 | if (null === $this->_trail_controller) { |
||
| 565 | $this->_trail_controller = new HTMLNavbarController($this->getContainer(), $this->controller_name); |
||
| 566 | } |
||
| 567 | |||
| 568 | return $this->_trail_controller; |
||
| 569 | } |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @return TreeController |
||
| 573 | */ |
||
| 574 | private function _getTreeController() |
||
| 581 | } |
||
| 582 | } |
||
| 583 |