Dispatcher.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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->view ()->build ();
  47. }
  48. } catch (Minz_Exception $e) {
  49. throw $e;
  50. }
  51. } while (self::$needsReset);
  52. }
  53. /**
  54. * Informe le contrôleur qu'il doit recommancer car la requête a été modifiée
  55. */
  56. public static function reset() {
  57. self::$needsReset = true;
  58. }
  59. /**
  60. * Instancie le Controller
  61. * @param $base_name le nom du controller à instancier
  62. * @exception ControllerNotExistException le controller n'existe pas
  63. * @exception ControllerNotActionControllerException controller n'est
  64. * > 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. $controller_name,
  76. Minz_Exception::ERROR
  77. );
  78. }
  79. $this->controller = new $controller_name ();
  80. if (! ($this->controller instanceof Minz_ActionController)) {
  81. throw new Minz_ControllerNotActionControllerException (
  82. $controller_name,
  83. Minz_Exception::ERROR
  84. );
  85. }
  86. }
  87. /**
  88. * Lance l'action sur le controller du dispatcher
  89. * @param $action_name le nom de l'action
  90. * @exception ActionException si on ne peut pas exécuter l'action sur
  91. * le controller
  92. */
  93. private function launchAction ($action_name) {
  94. if (!is_callable (array (
  95. $this->controller,
  96. $action_name
  97. ))) {
  98. throw new Minz_ActionException (
  99. get_class ($this->controller),
  100. $action_name,
  101. Minz_Exception::ERROR
  102. );
  103. }
  104. call_user_func (array (
  105. $this->controller,
  106. $action_name
  107. ));
  108. }
  109. /**
  110. * Register a controller file.
  111. *
  112. * @param $base_name the base name of the controller (i.e. ./?c=<base_name>)
  113. * @param $base_path the base path where we should look into to find info.
  114. */
  115. public static function registerController($base_name, $base_path) {
  116. if (!self::isRegistered($base_name)) {
  117. self::$registrations[$base_name] = $base_path;
  118. }
  119. }
  120. /**
  121. * Return if a controller is registered.
  122. *
  123. * @param $base_name the base name of the controller.
  124. * @return true if the controller has been registered, false else.
  125. */
  126. public static function isRegistered($base_name) {
  127. return isset(self::$registrations[$base_name]);
  128. }
  129. /**
  130. * Load a controller file (include).
  131. *
  132. * @param $base_name the base name of the controller.
  133. */
  134. private static function loadController($base_name) {
  135. $base_path = self::$registrations[$base_name];
  136. $controller_filename = $base_path . '/Controllers/' . $base_name . 'Controller.php';
  137. include_once $controller_filename;
  138. }
  139. private static function setViewPath($controller, $base_name) {
  140. $base_path = self::$registrations[$base_name];
  141. $controller->view()->setBasePathname($base_path);
  142. }
  143. }