| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 233 |
| Code Lines | 134 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 421 | private function httpHandler() |
||
| 422 | { |
||
| 423 | if (config()->loadFile('view') === true) { |
||
| 424 | // Instantiate Http UserAgent Service |
||
| 425 | $this->services->load(Framework\Http\UserAgent::class, 'userAgent'); |
||
| 426 | |||
| 427 | // Instantiate Http View Service |
||
| 428 | $this->services->load(Framework\Http\Parser::class); |
||
| 429 | |||
| 430 | // Instantiate Http View Service |
||
| 431 | $this->services->load(Framework\Http\View::class); |
||
| 432 | |||
| 433 | // Instantiate Http Presenter Service |
||
| 434 | $this->services->load(Framework\Http\Presenter::class); |
||
| 435 | } |
||
| 436 | |||
| 437 | // Instantiate Http Router Service |
||
| 438 | $this->services->load(Framework\Http\Router::class); |
||
| 439 | |||
| 440 | if (profiler() !== false) { |
||
| 441 | profiler()->watch('Parse Router Request'); |
||
| 442 | } |
||
| 443 | router()->handle(new Uri()); |
||
| 444 | |||
| 445 | if (config()->loadFile('session') === true) { |
||
| 446 | |||
| 447 | // Instantiate Session Service |
||
| 448 | $session = new Session(config('session', true)); |
||
| 449 | $session->setLogger($this->services->get('logger')); |
||
| 450 | |||
| 451 | if ( ! $session->isStarted()) { |
||
| 452 | $session->start(); |
||
| 453 | } |
||
| 454 | |||
| 455 | $this->services->add($session, 'session'); |
||
| 456 | |||
| 457 | if ($session->has('language') and $this->services->has('language')) { |
||
| 458 | language()->setDefault($session->get('language')); |
||
| 459 | } else { |
||
| 460 | $session->set('language', language()->getDefault()); |
||
| 461 | } |
||
| 462 | |||
| 463 | if (config('security')->protection[ 'csrf' ] === true) { |
||
| 464 | $csrfProtection = new Security\Protections\Csrf(); |
||
| 465 | $this->services->add($csrfProtection, 'csrfProtection'); |
||
| 466 | } |
||
| 467 | |||
| 468 | if (config('security')->protection[ 'xss' ] === true) { |
||
| 469 | $xssProtection = new Security\Protections\Xss(); |
||
| 470 | $this->services->add($xssProtection, 'xssProtection'); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | // Instantiate Http Middleware Service |
||
| 475 | $this->services->load(Framework\Http\Middleware::class); |
||
| 476 | |||
| 477 | if (profiler() !== false) { |
||
| 478 | profiler()->watch('Running Middleware Service: Pre Controller'); |
||
| 479 | } |
||
| 480 | middleware()->run(); |
||
| 481 | |||
| 482 | if ($this->services->has('controller')) { |
||
| 483 | $controller = $this->services->get('controller'); |
||
| 484 | |||
| 485 | $controllerParameter = dash($controller->getParameter()); |
||
| 486 | $controllerRequestMethod = dash($controller->getRequestMethod()); |
||
| 487 | |||
| 488 | $modules = $this->modules->getArrayCopy(); |
||
| 489 | |||
| 490 | // Run Module Autoloader |
||
| 491 | foreach ($modules as $module) { |
||
| 492 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
| 493 | continue; |
||
| 494 | } |
||
| 495 | |||
| 496 | // Autoload Module Language |
||
| 497 | if ($this->services->has('language')) { |
||
| 498 | language()->loadFile($module->getParameter()); |
||
| 499 | } |
||
| 500 | |||
| 501 | // Autoload Module Model |
||
| 502 | $module->loadModel(); |
||
| 503 | |||
| 504 | // Add View Resource Directory |
||
| 505 | if($this->services->has('view')) { |
||
| 506 | view()->addFilePath($module->getResourcesDir()); |
||
| 507 | presenter()->assets->pushFilePath($module->getResourcesDir()); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | if ($this->services->has('view')) { |
||
| 512 | presenter()->initialize(); |
||
| 513 | } |
||
| 514 | |||
| 515 | // Autoload Language |
||
| 516 | if ($this->services->has('language')) { |
||
| 517 | language()->loadFile($controller->getParameter()); |
||
| 518 | language()->loadFile($controller->getRequestMethod()); |
||
| 519 | language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod()); |
||
| 520 | } |
||
| 521 | |||
| 522 | // Autoload Model |
||
| 523 | $modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName()); |
||
| 524 | |||
| 525 | if (class_exists($modelClassName)) { |
||
| 526 | $this->models->load($modelClassName, 'controller'); |
||
| 527 | } |
||
| 528 | |||
| 529 | if ($this->services->has('view')) { |
||
| 530 | // Autoload Presenter |
||
| 531 | $presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName()); |
||
| 532 | |||
| 533 | if (class_exists($presenterClassName)) { |
||
| 534 | $presenterClassObject = new $presenterClassName(); |
||
| 535 | if ($presenterClassObject instanceof Framework\Http\Presenter) { |
||
| 536 | $this->services->add($presenterClassObject, 'presenter'); |
||
| 537 | } |
||
| 538 | } |
||
| 539 | } |
||
| 540 | |||
| 541 | // Initialize Controller |
||
| 542 | if (profiler() !== false) { |
||
| 543 | profiler()->watch('Calling Hooks Service: Pre Controller'); |
||
| 544 | } |
||
| 545 | |||
| 546 | hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER); |
||
| 547 | |||
| 548 | if (profiler() !== false) { |
||
| 549 | profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
||
| 550 | } |
||
| 551 | |||
| 552 | $requestController = $controller->getInstance(); |
||
| 553 | |||
| 554 | if (method_exists($requestController, '__reconstruct')) { |
||
| 555 | $requestController->__reconstruct(); |
||
| 556 | } |
||
| 557 | |||
| 558 | if (profiler() !== false) { |
||
| 559 | profiler()->watch('Calling Hooks Service: Post Controller'); |
||
| 560 | } |
||
| 561 | hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER); |
||
| 562 | |||
| 563 | if (profiler() !== false) { |
||
| 564 | profiler()->watch('Calling Middleware Service: Post Controller'); |
||
| 565 | } |
||
| 566 | middleware()->run(); |
||
| 567 | |||
| 568 | $requestMethod = $controller->getRequestMethod(); |
||
| 569 | $requestMethodArgs = $controller->getRequestMethodArgs(); |
||
| 570 | |||
| 571 | // Call the requested controller method |
||
| 572 | if (profiler() !== false) { |
||
| 573 | profiler()->watch('Execute Requested Controller Method'); |
||
| 574 | } |
||
| 575 | |||
| 576 | ob_start(); |
||
| 577 | $requestController->__call($requestMethod, $requestMethodArgs); |
||
| 578 | $requestControllerOutput = ob_get_contents(); |
||
| 579 | ob_end_clean(); |
||
| 580 | |||
| 581 | if (is_numeric($requestControllerOutput)) { |
||
| 582 | output()->sendError($requestControllerOutput); |
||
| 583 | } elseif (is_bool($requestControllerOutput)) { |
||
| 584 | if ($requestControllerOutput === true) { |
||
| 585 | output()->sendError(200); |
||
| 586 | } elseif ($requestControllerOutput === false) { |
||
| 587 | output()->sendError(204); |
||
| 588 | } |
||
| 589 | } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
||
| 590 | output()->sendPayload($requestControllerOutput); |
||
| 591 | } elseif ($requestController instanceof Framework\Http\Controllers\Restful) { |
||
| 592 | if (empty($requestControllerOutput)) { |
||
| 593 | $requestController->sendError(204); |
||
| 594 | } elseif (is_string($requestControllerOutput)) { |
||
| 595 | if (is_json($requestControllerOutput)) { |
||
| 596 | output()->setContentType('application/json'); |
||
| 597 | } else { |
||
| 598 | output()->setContentType('text/plain'); |
||
| 599 | } |
||
| 600 | |||
| 601 | echo $requestControllerOutput; |
||
| 602 | } |
||
| 603 | } elseif (is_string($requestControllerOutput)) { |
||
| 604 | if (is_json($requestControllerOutput)) { |
||
| 605 | output()->setContentType('application/json'); |
||
| 606 | echo $requestControllerOutput; |
||
| 607 | } elseif ($this->services->has('view')) { |
||
| 608 | if (empty($requestControllerOutput)) { |
||
| 609 | $filenames = [ |
||
| 610 | $controllerRequestMethod, |
||
| 611 | $controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod, |
||
| 612 | ]; |
||
| 613 | |||
| 614 | if ($controllerRequestMethod === 'index') { |
||
| 615 | array_unshift($filenames, $controllerParameter); |
||
| 616 | } |
||
| 617 | |||
| 618 | foreach ($filenames as $filename) { |
||
| 619 | if (false !== ($filePath = view()->getFilePath($filename))) { |
||
| 620 | view()->load($filePath); |
||
| 621 | break; |
||
| 622 | } |
||
| 623 | } |
||
| 624 | } else { |
||
| 625 | presenter()->partials->offsetSet('content', $requestControllerOutput); |
||
| 626 | } |
||
| 627 | |||
| 628 | if (presenter()->partials->offsetExists('content')) { |
||
| 629 | if(is_ajax()) { |
||
| 630 | echo presenter()->partials->content; |
||
| 631 | } else { |
||
| 632 | $htmlOutput = view()->render(); |
||
| 633 | |||
| 634 | if (empty($htmlOutput)) { |
||
| 635 | output()->sendError(204); |
||
| 636 | } else { |
||
| 637 | output()->setContentType('text/html'); |
||
| 638 | output()->send($htmlOutput); |
||
| 639 | } |
||
| 640 | } |
||
| 641 | } else { |
||
| 642 | output()->sendError(204); |
||
| 643 | } |
||
| 644 | } elseif (empty($requestControllerOutput) or $requestControllerOutput === '') { |
||
| 645 | output()->sendError(204); |
||
| 646 | } else { |
||
| 647 | output()->setContentType('text/plain'); |
||
| 648 | output()->send($requestControllerOutput); |
||
| 649 | } |
||
| 650 | } |
||
| 651 | } else { |
||
| 652 | // Show Error (404) Page Not Found |
||
| 653 | output()->sendError(404); |
||
| 654 | } |
||
| 657 |