Dispatcher.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * Le Dispatcher s'occupe d'initialiser le Controller et d'executer l'action
  8. * déterminée dans la Request
  9. * C'est un singleton
  10. */
  11. class Minz_Dispatcher {
  12. /* singleton */
  13. private static $instance = null;
  14. private static $needsReset;
  15. private static $registrations = array();
  16. private $controller;
  17. /**
  18. * Récupère l'instance du Dispatcher
  19. */
  20. public static function getInstance () {
  21. if (self::$instance === null) {
  22. self::$instance = new Minz_Dispatcher ();
  23. }
  24. return self::$instance;
  25. }
  26. /**
  27. * Lance le controller indiqué dans Request
  28. * Remplit le body de Response à partir de la Vue
  29. * @throws Minz_Exception
  30. */
  31. public function run () {
  32. do {
  33. self::$needsReset = false;
  34. try {
  35. $this->createController (Minz_Request::controllerName ());
  36. $this->controller->init ();
  37. $this->controller->firstAction ();
  38. if (!self::$needsReset) {
  39. $this->launchAction (
  40. Minz_Request::actionName ()
  41. . 'Action'
  42. );
  43. }
  44. $this->controller->lastAction ();
  45. if (!self::$needsReset) {
  46. $this->controller->declareCspHeader();
  47. $this->controller->view ()->build ();
  48. }
  49. } catch (Minz_Exception $e) {
  50. throw $e;
  51. }
  52. } while (self::$needsReset);
  53. }
  54. /**
  55. * Informe le contrôleur qu'il doit recommancer car la requête a été modifiée
  56. */
  57. public static function reset() {
  58. self::$needsReset = true;
  59. }
  60. /**
  61. * Instancie le Controller
  62. * @param string $base_name le nom du controller à instancier
  63. * @throws Minz_ControllerNotExistException le controller n'existe pas
  64. * @throws Minz_ControllerNotActionControllerException controller n'est pas une instance de ActionController
  65. */
  66. private function createController ($base_name) {
  67. if (self::isRegistered($base_name)) {
  68. self::loadController($base_name);
  69. $controller_name = 'FreshExtension_' . $base_name . '_Controller';
  70. } else {
  71. $controller_name = 'FreshRSS_' . $base_name . '_Controller';
  72. }
  73. if (!class_exists ($controller_name)) {
  74. throw new Minz_ControllerNotExistException (
  75. Minz_Exception::ERROR
  76. );
  77. }
  78. $this->controller = new $controller_name ();
  79. if (! ($this->controller instanceof Minz_ActionController)) {
  80. throw new Minz_ControllerNotActionControllerException (
  81. $controller_name,
  82. Minz_Exception::ERROR
  83. );
  84. }
  85. }
  86. /**
  87. * Lance l'action sur le controller du dispatcher
  88. * @param string $action_name le nom de l'action
  89. * @throws Minz_ActionException si on ne peut pas exécuter l'action sur le controller
  90. */
  91. private function launchAction ($action_name) {
  92. if (!is_callable (array (
  93. $this->controller,
  94. $action_name
  95. ))) {
  96. throw new Minz_ActionException (
  97. get_class ($this->controller),
  98. $action_name,
  99. Minz_Exception::ERROR
  100. );
  101. }
  102. call_user_func (array (
  103. $this->controller,
  104. $action_name
  105. ));
  106. }
  107. /**
  108. * Register a controller file.
  109. *
  110. * @param string $base_name the base name of the controller (i.e. ./?c=<base_name>)
  111. * @param string $base_path the base path where we should look into to find info.
  112. */
  113. public static function registerController($base_name, $base_path) {
  114. if (!self::isRegistered($base_name)) {
  115. self::$registrations[$base_name] = $base_path;
  116. }
  117. }
  118. /**
  119. * Return if a controller is registered.
  120. *
  121. * @param string $base_name the base name of the controller.
  122. * @return boolean true if the controller has been registered, false else.
  123. */
  124. public static function isRegistered($base_name) {
  125. return isset(self::$registrations[$base_name]);
  126. }
  127. /**
  128. * Load a controller file (include).
  129. *
  130. * @param string $base_name the base name of the controller.
  131. */
  132. private static function loadController($base_name) {
  133. $base_path = self::$registrations[$base_name];
  134. $controller_filename = $base_path . '/Controllers/' . $base_name . 'Controller.php';
  135. include_once $controller_filename;
  136. }
  137. private static function setViewPath($controller, $base_name) {
  138. $base_path = self::$registrations[$base_name];
  139. $controller->view()->setBasePathname($base_path);
  140. }
  141. }