Advertisement
Guest User

Untitled

a guest
Aug 9th, 2020
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Handlers;
  4.  
  5. class ControllerHandler
  6. {
  7. private $routeConfig;
  8. private const CONTROLLER = 0;
  9. private const ACTION = 1;
  10. private const ARGUMENTS = 2;
  11.  
  12. public function __construct(array $routeConfig) {
  13. $this->routeConfig = $routeConfig;
  14. }
  15.  
  16. public function handle() {
  17.  
  18. $controllerName = $this->getController($this->routeConfig);
  19. $actionName = $this->getAction($this->routeConfig);
  20. $controller = new $controllerName;
  21. $content = $controller->$actionName(...$this->getParams($this->routeConfig));
  22.  
  23. return $content;
  24. }
  25.  
  26. /**
  27. * @param array $routeInfo
  28. * @return string
  29. */
  30. private function getController(array $routeInfo): string {
  31. return 'App\Controllers\\' . $routeInfo[self::CONTROLLER];
  32. }
  33.  
  34. /**
  35. * @param array $routeInfo
  36. * @return string
  37. */
  38. private function getAction(array $routeInfo): string {
  39. return $routeInfo[self::ACTION];
  40. }
  41.  
  42. /**
  43. * @param array $routeInfo
  44. * @return array
  45. */
  46. private function getParams(array $routeInfo): array {
  47. return $routeInfo[self::ARGUMENTS];
  48. }
  49.  
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement