Dispatcher.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * @exception 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 $base_name le nom du controller à instancier
  63. * @exception ControllerNotExistException le controller n'existe pas
  64. * @exception ControllerNotActionControllerException controller n'est
  65. * > pas une instance de ActionController
  66. */
  67. private function createController ($base_name) {
  68. if (self::isRegistered($base_name)) {
  69. self::loadController($base_name);
  70. $controller_name = 'FreshExtension_' . $base_name . '_Controller';
  71. } else {
  72. $controller_name = 'FreshRSS_' . $base_name . '_Controller';
  73. }
  74. if (!class_exists ($controller_name)) {
  75. throw new Minz_ControllerNotExistException (
  76. $controller_name,
  77. Minz_Exception::ERROR
  78. );
  79. }
  80. $this->controller = new $controller_name ();
  81. if (! ($this->controller instanceof Minz_ActionController)) {
  82. throw new Minz_ControllerNotActionControllerException (
  83. $controller_name,
  84. Minz_Exception::ERROR
  85. );
  86. }
  87. }
  88. /**
  89. * Lance l'action sur le controller du dispatcher
  90. * @param $action_name le nom de l'action
  91. * @exception ActionException si on ne peut pas exécuter l'action sur
  92. * le controller
  93. */
  94. private function launchAction ($action_name) {
  95. if (!is_callable (array (
  96. $this->controller,
  97. $action_name
  98. ))) {
  99. throw new Minz_ActionException (
  100. get_class ($this->controller),
  101. $action_name,
  102. Minz_Exception::ERROR
  103. );
  104. }
  105. call_user_func (array (
  106. $this->controller,
  107. $action_name
  108. ));
  109. }
  110. /**
  111. * Register a controller file.
  112. *
  113. * @param $base_name the base name of the controller (i.e. ./?c=<base_name>)
  114. * @param $base_path the base path where we should look into to find info.
  115. */
  116. public static function registerController($base_name, $base_path) {
  117. if (!self::isRegistered($base_name)) {
  118. self::$registrations[$base_name] = $base_path;
  119. }
  120. }
  121. /**
  122. * Return if a controller is registered.
  123. *
  124. * @param $base_name the base name of the controller.
  125. * @return true if the controller has been registered, false else.
  126. */
  127. public static function isRegistered($base_name) {
  128. return isset(self::$registrations[$base_name]);
  129. }
  130. /**
  131. * Load a controller file (include).
  132. *
  133. * @param $base_name the base name of the controller.
  134. */
  135. private static function loadController($base_name) {
  136. $base_path = self::$registrations[$base_name];
  137. $controller_filename = $base_path . '/Controllers/' . $base_name . 'Controller.php';
  138. include_once $controller_filename;
  139. }
  140. private static function setViewPath($controller, $base_name) {
  141. $base_path = self::$registrations[$base_name];
  142. $controller->view()->setBasePathname($base_path);
  143. }
  144. }