Complex classes like Application 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Application |
||
| 54 | { |
||
| 55 | use Common\Helper; |
||
| 56 | use Common\Singleton; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string Application path |
||
| 60 | */ |
||
| 61 | protected $path; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string Environment name |
||
| 65 | */ |
||
| 66 | protected $environment = 'production'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \Exception |
||
| 70 | */ |
||
| 71 | protected $exception; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool Debug application flag |
||
| 75 | */ |
||
| 76 | protected $debugFlag = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool Layout flag |
||
| 80 | */ |
||
| 81 | protected $layoutFlag = true; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array Stack of widgets closures |
||
| 85 | */ |
||
| 86 | protected $widgets = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var array Stack of API closures |
||
| 90 | */ |
||
| 91 | protected $api = array(); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get application environment |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getEnvironment() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get path to Application |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 669 | public function getPath() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Check debug flag |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 4 | public function isDebug() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Check Layout flag |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | 5 | public function hasLayout() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Set Layout template and/or flag |
||
| 144 | * |
||
| 145 | * @param bool|string $flag |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | 669 | public function useLayout($flag = true) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Set JSON presentation |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function useJson() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Initialize process |
||
| 172 | * |
||
| 173 | * @param string $environment |
||
| 174 | * @throws ApplicationException |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function init($environment = 'production') |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Initial Request instance |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function initRequest() |
||
| 230 | { |
||
| 231 | $request = RequestFactory::fromGlobals(); |
||
| 232 | |||
| 233 | Request::setInstance($request); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Initial Response instance |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | protected function initResponse() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Initial Router instance |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | 669 | protected function initRouter() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Initial controller view |
||
| 263 | * |
||
| 264 | * @param string $module |
||
| 265 | * @param string $controller |
||
| 266 | * @return View |
||
| 267 | */ |
||
| 268 | 2 | protected function initView($module, $controller) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Process application |
||
| 290 | * |
||
| 291 | * Note: |
||
| 292 | * - Why you don't use "X-" prefix for custom headers? |
||
| 293 | * - Because it deprecated ({@link http://tools.ietf.org/html/rfc6648}) |
||
| 294 | * |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | 2 | public function process() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Pre process |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | * @throws ApplicationException |
||
| 311 | */ |
||
| 312 | 2 | protected function preProcess() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Do process |
||
| 326 | * |
||
| 327 | * @return void |
||
| 328 | */ |
||
| 329 | 2 | protected function doProcess() |
|
| 330 | { |
||
| 331 | 2 | Logger::info("app:process:do"); |
|
| 332 | |||
| 333 | 2 | $module = Request::getModule(); |
|
| 334 | 2 | $controller = Request::getController(); |
|
| 335 | 2 | $params = Request::getParams(); |
|
| 336 | |||
| 337 | // try to dispatch controller |
||
| 338 | try { |
||
| 339 | // get reflection of requested controller |
||
| 340 | 2 | $controllerFile = $this->getControllerFile($module, $controller); |
|
| 341 | 1 | $reflection = $this->reflection($controllerFile); |
|
| 342 | |||
| 343 | // check header "accept" for catch JSON requests, and switch response to JSON |
||
| 344 | // it's some magic for AJAX and REST requests |
||
| 345 | $acceptMap = [ |
||
| 346 | 1 | 'HTML' => 'text/html', |
|
| 347 | 'JSON' => 'application/json' |
||
| 348 | 1 | ]; |
|
| 349 | |||
| 350 | // some controller has @accept tag |
||
| 351 | 1 | if ($allowAccept = $reflection->getAccept()) { |
|
| 352 | // convert list of controller @accept to MIME types |
||
| 353 | $allowAccept = array_filter( |
||
| 354 | $acceptMap, |
||
| 355 | function ($key) use ($allowAccept) { |
||
| 356 | return in_array($key, $allowAccept); |
||
| 357 | }, |
||
| 358 | ARRAY_FILTER_USE_KEY |
||
| 359 | ); |
||
| 360 | $allowAccept = array_values($allowAccept); |
||
| 361 | } else { |
||
| 362 | // by default allow just HTML output |
||
| 363 | 1 | $allowAccept = ['text/html']; |
|
| 364 | } |
||
| 365 | |||
| 366 | // choose MIME type by browser accept header |
||
| 367 | // filtered by controller @accept |
||
| 368 | 1 | $accept = Request::getAccept($allowAccept); |
|
| 369 | |||
| 370 | // switch statement for Accept header |
||
| 371 | switch ($accept) { |
||
| 372 | 1 | case 'text/html': |
|
| 373 | // HTML response with layout |
||
| 374 | 1 | break; |
|
| 375 | case 'application/json': |
||
| 376 | $this->useJson(); |
||
| 377 | break; |
||
| 378 | default: |
||
| 379 | if (PHP_SAPI == 'cli') { |
||
| 380 | // all ok |
||
| 381 | } else { |
||
| 382 | // not acceptable MIME type |
||
| 383 | throw new NotAcceptableException(); |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | // check call method(s) |
||
| 388 | 1 | if ($reflection->getMethod() && !in_array(Request::getMethod(), $reflection->getMethod())) { |
|
| 389 | throw new NotAllowedException(join(',', $reflection->getMethod())); |
||
| 390 | } |
||
| 391 | |||
| 392 | // check HTML cache |
||
| 393 | 1 | if ($reflection->getCacheHtml() && Request::getMethod() == Request::METHOD_GET) { |
|
| 394 | $htmlKey = 'html:' . $module . ':' . $controller . ':' . http_build_query($params); |
||
| 395 | if ($cachedHtml = Cache::get($htmlKey)) { |
||
| 396 | Response::setBody($cachedHtml); |
||
| 397 | return; |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | // dispatch controller |
||
| 402 | 1 | $dispatchResult = $this->dispatch($module, $controller, $params); |
|
| 403 | |||
| 404 | 2 | } catch (RedirectException $e) { |
|
| 405 | $this->setException($e); |
||
| 406 | |||
| 407 | Response::removeHeaders(); |
||
| 408 | Response::clearBody(); |
||
| 409 | |||
| 410 | if (Request::isXmlHttpRequest()) { |
||
| 411 | Response::setStatusCode(204); |
||
| 412 | Response::setHeader('Bluz-Redirect', $e->getMessage()); |
||
| 413 | return; |
||
| 414 | } else { |
||
| 415 | Response::setStatusCode(302); |
||
| 416 | Response::setHeader('Location', $e->getMessage()); |
||
| 417 | return; |
||
| 418 | } |
||
| 419 | 1 | } catch (ReloadException $e) { |
|
| 420 | $this->setException($e); |
||
| 421 | |||
| 422 | Response::removeHeaders(); |
||
| 423 | Response::clearBody(); |
||
| 424 | |||
| 425 | if (Request::isXmlHttpRequest()) { |
||
| 426 | Response::setStatusCode(204); |
||
| 427 | Response::setHeader('Bluz-Reload', 'true'); |
||
| 428 | return; |
||
| 429 | } else { |
||
| 430 | Response::setStatusCode(302); |
||
| 431 | Response::setHeader('Location', Request::getRequestUri()); |
||
| 432 | return; |
||
| 433 | } |
||
| 434 | 1 | } catch (\Exception $e) { |
|
| 435 | 1 | $this->setException($e); |
|
| 436 | |||
| 437 | 1 | Response::removeHeaders(); |
|
| 438 | 1 | Response::clearBody(); |
|
| 439 | |||
| 440 | // cast to valid HTTP error code |
||
| 441 | // 500 - Internal Server Error |
||
| 442 | 1 | $statusCode = (100 <= $e->getCode() && $e->getCode() <= 505) ? $e->getCode() : 500; |
|
| 443 | 1 | Response::setStatusCode($statusCode); |
|
| 444 | |||
| 445 | // TODO: if `error` controller consist error |
||
| 446 | 1 | $dispatchResult = $this->dispatch( |
|
| 447 | 1 | Router::getErrorModule(), |
|
| 448 | 1 | Router::getErrorController(), |
|
| 449 | array( |
||
| 450 | 1 | 'code' => $e->getCode(), |
|
| 451 | 1 | 'message' => $e->getMessage() |
|
| 452 | 1 | ) |
|
| 453 | 1 | ); |
|
| 454 | } |
||
| 455 | |||
| 456 | 2 | if ($this->hasLayout()) { |
|
| 457 | 2 | Layout::setContent($dispatchResult); |
|
| 458 | 2 | $dispatchResult = Layout::getInstance(); |
|
| 459 | 2 | } |
|
| 460 | |||
| 461 | 2 | if (isset($htmlKey, $reflection)) { |
|
| 462 | // @TODO: Added ETag header |
||
| 463 | Cache::set($htmlKey, $dispatchResult(), $reflection->getCacheHtml()); |
||
| 464 | Cache::addTag($htmlKey, $module); |
||
| 465 | Cache::addTag($htmlKey, 'html'); |
||
| 466 | Cache::addTag($htmlKey, 'html:' . $module); |
||
| 467 | Cache::addTag($htmlKey, 'html:' . $module . ':' . $controller); |
||
| 468 | } |
||
| 469 | 2 | Response::setBody($dispatchResult); |
|
| 470 | 2 | } |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Post process |
||
| 474 | * |
||
| 475 | * @return void |
||
| 476 | */ |
||
| 477 | 2 | protected function postProcess() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Dispatch controller with params |
||
| 484 | * |
||
| 485 | * Call dispatch from any \Bluz\Package |
||
| 486 | * Application::getInstance()->dispatch($module, $controller, array $params); |
||
| 487 | * |
||
| 488 | * @param string $module |
||
| 489 | * @param string $controller |
||
| 490 | * @param array $params |
||
| 491 | * @return View|callable |
||
| 492 | * @throws ApplicationException |
||
| 493 | */ |
||
| 494 | 3 | public function dispatch($module, $controller, $params = array()) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Pre dispatch mount point |
||
| 507 | * |
||
| 508 | * @param string $module |
||
| 509 | * @param string $controller |
||
| 510 | * @param array $params |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | 3 | protected function preDispatch($module, $controller, $params = array()) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Do dispatch |
||
| 525 | * |
||
| 526 | * @param string $module |
||
| 527 | * @param string $controller |
||
| 528 | * @param array $params |
||
| 529 | * @return View|callable |
||
| 530 | * @throws ApplicationException |
||
| 531 | */ |
||
| 532 | 2 | protected function doDispatch($module, $controller, $params = array()) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Post dispatch mount point |
||
| 608 | * |
||
| 609 | * @param string $module |
||
| 610 | * @param string $controller |
||
| 611 | * @param array $params |
||
| 612 | * @return void |
||
| 613 | */ |
||
| 614 | 2 | protected function postDispatch($module, $controller, $params = array()) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Render, is send Response |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | public function render() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get Response instance |
||
| 633 | * |
||
| 634 | * @return \Bluz\Response\Response |
||
| 635 | */ |
||
| 636 | 1 | public function getResponse() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Get Request instance |
||
| 643 | * |
||
| 644 | * @return \Zend\Diactoros\ServerRequest |
||
| 645 | */ |
||
| 646 | 1 | public function getRequest() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * setException |
||
| 653 | * |
||
| 654 | * @param \Exception $exception |
||
| 655 | * @return void |
||
| 656 | */ |
||
| 657 | 1 | public function setException($exception) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * getException |
||
| 664 | * |
||
| 665 | * @return \Exception |
||
| 666 | */ |
||
| 667 | public function getException() |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Widget call |
||
| 674 | * |
||
| 675 | * Call widget from any \Bluz\Package |
||
| 676 | * <code> |
||
| 677 | * Application::getInstance()->widget($module, $widget, array $params); |
||
| 678 | * </code> |
||
| 679 | * |
||
| 680 | * @param string $module |
||
| 681 | * @param string $widget |
||
| 682 | * @param array $params |
||
| 683 | * @return \Closure |
||
| 684 | * @throws ApplicationException |
||
| 685 | */ |
||
| 686 | 1 | public function widget($module, $widget, $params = array()) |
|
| 718 | |||
| 719 | /** |
||
| 720 | * Api call |
||
| 721 | * |
||
| 722 | * Call API from any \Bluz\Package |
||
| 723 | * <code> |
||
| 724 | * Application::getInstance()->api($module, $widget, array $params); |
||
| 725 | * </code> |
||
| 726 | * |
||
| 727 | * @param string $module |
||
| 728 | * @param string $method |
||
| 729 | * @return \Closure |
||
| 730 | * @throws ApplicationException |
||
| 731 | */ |
||
| 732 | 1 | public function api($module, $method) |
|
| 759 | |||
| 760 | /** |
||
| 761 | * Retrieve reflection for anonymous function |
||
| 762 | * |
||
| 763 | * @param string $file |
||
| 764 | * @return Reflection |
||
| 765 | * @throws ApplicationException |
||
| 766 | */ |
||
| 767 | 670 | public function reflection($file) |
|
| 779 | |||
| 780 | /** |
||
| 781 | * Is allowed controller/widget/etc |
||
| 782 | * |
||
| 783 | * @param string $module |
||
| 784 | * @param string $controller |
||
| 785 | * @return bool |
||
| 786 | * @throws ApplicationException |
||
| 787 | */ |
||
| 788 | 4 | public function isAllowed($module, $controller) |
|
| 798 | |||
| 799 | /** |
||
| 800 | * Get controller file |
||
| 801 | * |
||
| 802 | * @param string $module |
||
| 803 | * @param string $controller |
||
| 804 | * @return string |
||
| 805 | * @throws ApplicationException |
||
| 806 | */ |
||
| 807 | 4 | protected function getControllerFile($module, $controller) |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Get widget file |
||
| 821 | * |
||
| 822 | * @param string $module |
||
| 823 | * @param string $widget |
||
| 824 | * @return string |
||
| 825 | * @throws ApplicationException |
||
| 826 | */ |
||
| 827 | 1 | protected function getWidgetFile($module, $widget) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Get API file |
||
| 841 | * |
||
| 842 | * @param string $module |
||
| 843 | * @param string $method |
||
| 844 | * @return string |
||
| 845 | * @throws ApplicationException |
||
| 846 | */ |
||
| 847 | 1 | protected function getApiFile($module, $method) |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Finally method |
||
| 861 | * |
||
| 862 | * @return void |
||
| 863 | */ |
||
| 864 | public function finish() |
||
| 868 | } |
||
| 869 |