Dispatcher.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 $router;
  16. private $controller;
  17. /**
  18. * Récupère l'instance du Dispatcher
  19. */
  20. public static function getInstance ($router) {
  21. if (self::$instance === null) {
  22. self::$instance = new Minz_Dispatcher ($router);
  23. }
  24. return self::$instance;
  25. }
  26. /**
  27. * Constructeur
  28. */
  29. private function __construct ($router) {
  30. $this->router = $router;
  31. }
  32. /**
  33. * Lance le controller indiqué dans Request
  34. * Remplit le body de Response à partir de la Vue
  35. * @exception Minz_Exception
  36. */
  37. public function run ($ob = true) {
  38. // Le ob_start est dupliqué : sans ça il y a un bug sous Firefox
  39. // ici on l'appelle avec 'ob_gzhandler', après sans.
  40. // Vraisemblablement la compression fonctionne mais c'est sale
  41. // J'ignore les effets de bord :(
  42. if ($ob) {
  43. ob_start ('ob_gzhandler');
  44. }
  45. $text = ''; //TODO: Clean this code
  46. while (Minz_Request::$reseted) {
  47. Minz_Request::$reseted = false;
  48. try {
  49. $this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
  50. $this->controller->init ();
  51. $this->controller->firstAction ();
  52. $this->launchAction (
  53. Minz_Request::actionName ()
  54. . 'Action'
  55. );
  56. $this->controller->lastAction ();
  57. if (!Minz_Request::$reseted) {
  58. if ($ob) {
  59. ob_start ();
  60. }
  61. $this->controller->view ()->build ();
  62. if ($ob) {
  63. $text = ob_get_clean();
  64. }
  65. }
  66. } catch (Minz_Exception $e) {
  67. throw $e;
  68. }
  69. }
  70. Minz_Response::setBody ($text);
  71. }
  72. /**
  73. * Instancie le Controller
  74. * @param $controller_name le nom du controller à instancier
  75. * @exception ControllerNotExistException le controller n'existe pas
  76. * @exception ControllerNotActionControllerException controller n'est
  77. * > pas une instance de ActionController
  78. */
  79. private function createController ($controller_name) {
  80. $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
  81. . $controller_name . '.php';
  82. if (!class_exists ($controller_name)) {
  83. throw new Minz_ControllerNotExistException (
  84. $controller_name,
  85. Minz_Exception::ERROR
  86. );
  87. }
  88. $this->controller = new $controller_name ($this->router);
  89. if (! ($this->controller instanceof Minz_ActionController)) {
  90. throw new Minz_ControllerNotActionControllerException (
  91. $controller_name,
  92. Minz_Exception::ERROR
  93. );
  94. }
  95. }
  96. /**
  97. * Lance l'action sur le controller du dispatcher
  98. * @param $action_name le nom de l'action
  99. * @exception ActionException si on ne peut pas exécuter l'action sur
  100. * le controller
  101. */
  102. private function launchAction ($action_name) {
  103. if (!Minz_Request::$reseted) {
  104. if (!is_callable (array (
  105. $this->controller,
  106. $action_name
  107. ))) {
  108. throw new Minz_ActionException (
  109. get_class ($this->controller),
  110. $action_name,
  111. Minz_Exception::ERROR
  112. );
  113. }
  114. call_user_func (array (
  115. $this->controller,
  116. $action_name
  117. ));
  118. }
  119. }
  120. }