Dispatcher.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. const CONTROLLERS_PATH_NAME = '/Controllers';
  13. /* singleton */
  14. private static $instance = null;
  15. private static $needsReset;
  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 ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
  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 $controller_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 ($controller_name) {
  67. $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
  68. . $controller_name . '.php';
  69. if (!class_exists ($controller_name)) {
  70. throw new Minz_ControllerNotExistException (
  71. $controller_name,
  72. Minz_Exception::ERROR
  73. );
  74. }
  75. $this->controller = new $controller_name ();
  76. if (! ($this->controller instanceof Minz_ActionController)) {
  77. throw new Minz_ControllerNotActionControllerException (
  78. $controller_name,
  79. Minz_Exception::ERROR
  80. );
  81. }
  82. }
  83. /**
  84. * Lance l'action sur le controller du dispatcher
  85. * @param $action_name le nom de l'action
  86. * @exception ActionException si on ne peut pas exécuter l'action sur
  87. * le controller
  88. */
  89. private function launchAction ($action_name) {
  90. if (!is_callable (array (
  91. $this->controller,
  92. $action_name
  93. ))) {
  94. throw new Minz_ActionException (
  95. get_class ($this->controller),
  96. $action_name,
  97. Minz_Exception::ERROR
  98. );
  99. }
  100. call_user_func (array (
  101. $this->controller,
  102. $action_name
  103. ));
  104. }
  105. }