Dispatcher.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * The Dispatcher is in charge of initialising the Controller and exectue the action as specified in the Request object.
  8. * It is a singleton.
  9. */
  10. class Minz_Dispatcher {
  11. /**
  12. * Singleton
  13. */
  14. private static ?Minz_Dispatcher $instance = null;
  15. private static bool $needsReset;
  16. /** @var array<string,string> */
  17. private static array $registrations = [];
  18. private Minz_ActionController $controller;
  19. /**
  20. * Retrieves the Dispatcher instance
  21. */
  22. public static function getInstance(): Minz_Dispatcher {
  23. if (self::$instance === null) {
  24. self::$instance = new Minz_Dispatcher();
  25. }
  26. return self::$instance;
  27. }
  28. /**
  29. * Launches the controller specified in Request
  30. * Fills the Response body from the View
  31. * @throws Minz_Exception
  32. */
  33. public function run(): void {
  34. do {
  35. self::$needsReset = false;
  36. try {
  37. $this->createController (Minz_Request::controllerName ());
  38. $this->controller->init ();
  39. $this->controller->firstAction ();
  40. // @phpstan-ignore-next-line
  41. if (!self::$needsReset) {
  42. $this->launchAction (
  43. Minz_Request::actionName ()
  44. . 'Action'
  45. );
  46. }
  47. $this->controller->lastAction ();
  48. // @phpstan-ignore-next-line
  49. if (!self::$needsReset) {
  50. $this->controller->declareCspHeader();
  51. $this->controller->view ()->build ();
  52. }
  53. } catch (Minz_Exception $e) {
  54. throw $e;
  55. }
  56. // @phpstan-ignore-next-line
  57. } while (self::$needsReset);
  58. }
  59. /**
  60. * Informs the controller that it must restart because the request has been modified
  61. */
  62. public static function reset(): void {
  63. self::$needsReset = true;
  64. }
  65. /**
  66. * Instantiates the Controller
  67. * @param string $base_name the name of the controller to instantiate
  68. * @throws Minz_ControllerNotExistException the controller does not exist
  69. * @throws Minz_ControllerNotActionControllerException controller is not an instance of ActionController
  70. */
  71. private function createController(string $base_name): void {
  72. if (self::isRegistered($base_name)) {
  73. self::loadController($base_name);
  74. $controller_name = 'FreshExtension_' . $base_name . '_Controller';
  75. } else {
  76. $controller_name = 'FreshRSS_' . $base_name . '_Controller';
  77. }
  78. if (!class_exists($controller_name)) {
  79. throw new Minz_ControllerNotExistException (
  80. Minz_Exception::ERROR
  81. );
  82. }
  83. $controller = new $controller_name();
  84. if (!($controller instanceof Minz_ActionController)) {
  85. throw new Minz_ControllerNotActionControllerException (
  86. $controller_name,
  87. Minz_Exception::ERROR
  88. );
  89. }
  90. $this->controller = $controller;
  91. }
  92. /**
  93. * Launch the action on the dispatcher’s controller
  94. * @param string $action_name the name of the action
  95. * @throws Minz_ActionException if the action cannot be executed on the controller
  96. */
  97. private function launchAction(string $action_name): void {
  98. $call = [$this->controller, $action_name];
  99. if (!is_callable($call)) {
  100. throw new Minz_ActionException (
  101. get_class($this->controller),
  102. $action_name,
  103. Minz_Exception::ERROR
  104. );
  105. }
  106. call_user_func($call);
  107. }
  108. /**
  109. * Register a controller file.
  110. *
  111. * @param string $base_name the base name of the controller (i.e. ./?c=<base_name>)
  112. * @param string $base_path the base path where we should look into to find info.
  113. */
  114. public static function registerController(string $base_name, string $base_path): void {
  115. if (!self::isRegistered($base_name)) {
  116. self::$registrations[$base_name] = $base_path;
  117. }
  118. }
  119. /**
  120. * Return if a controller is registered.
  121. *
  122. * @param string $base_name the base name of the controller.
  123. * @return bool true if the controller has been registered, false else.
  124. */
  125. public static function isRegistered(string $base_name): bool {
  126. return isset(self::$registrations[$base_name]);
  127. }
  128. /**
  129. * Load a controller file (include).
  130. *
  131. * @param string $base_name the base name of the controller.
  132. */
  133. private static function loadController(string $base_name): void {
  134. $base_path = self::$registrations[$base_name];
  135. $controller_filename = $base_path . '/Controllers/' . $base_name . 'Controller.php';
  136. include_once $controller_filename;
  137. }
  138. }