Dispatcher.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $cache = new Minz_Cache();
  39. // Le ob_start est dupliqué : sans ça il y a un bug sous Firefox
  40. // ici on l'appelle avec 'ob_gzhandler', après sans.
  41. // Vraisemblablement la compression fonctionne mais c'est sale
  42. // J'ignore les effets de bord :(
  43. if ($ob) {
  44. ob_start ('ob_gzhandler');
  45. }
  46. if (Minz_Cache::isEnabled () && !$cache->expired ()) {
  47. if ($ob) {
  48. ob_start ();
  49. }
  50. $cache->render ();
  51. if ($ob) {
  52. $text = ob_get_clean();
  53. }
  54. } else {
  55. $text = ''; //TODO: Clean this code
  56. while (Minz_Request::$reseted) {
  57. Minz_Request::$reseted = false;
  58. try {
  59. $this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
  60. $this->controller->init ();
  61. $this->controller->firstAction ();
  62. $this->launchAction (
  63. Minz_Request::actionName ()
  64. . 'Action'
  65. );
  66. $this->controller->lastAction ();
  67. if (!Minz_Request::$reseted) {
  68. if ($ob) {
  69. ob_start ();
  70. }
  71. $this->controller->view ()->build ();
  72. if ($ob) {
  73. $text = ob_get_clean();
  74. }
  75. }
  76. } catch (Minz_Exception $e) {
  77. throw $e;
  78. }
  79. }
  80. if (Minz_Cache::isEnabled ()) {
  81. $cache->cache ($text);
  82. }
  83. }
  84. Minz_Response::setBody ($text);
  85. }
  86. /**
  87. * Instancie le Controller
  88. * @param $controller_name le nom du controller à instancier
  89. * @exception ControllerNotExistException le controller n'existe pas
  90. * @exception ControllerNotActionControllerException controller n'est
  91. * > pas une instance de ActionController
  92. */
  93. private function createController ($controller_name) {
  94. $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
  95. . $controller_name . '.php';
  96. if (!class_exists ($controller_name)) {
  97. throw new Minz_ControllerNotExistException (
  98. $controller_name,
  99. Minz_Exception::ERROR
  100. );
  101. }
  102. $this->controller = new $controller_name ($this->router);
  103. if (! ($this->controller instanceof Minz_ActionController)) {
  104. throw new Minz_ControllerNotActionControllerException (
  105. $controller_name,
  106. Minz_Exception::ERROR
  107. );
  108. }
  109. }
  110. /**
  111. * Lance l'action sur le controller du dispatcher
  112. * @param $action_name le nom de l'action
  113. * @exception ActionException si on ne peut pas exécuter l'action sur
  114. * le controller
  115. */
  116. private function launchAction ($action_name) {
  117. if (!Minz_Request::$reseted) {
  118. if (!is_callable (array (
  119. $this->controller,
  120. $action_name
  121. ))) {
  122. throw new Minz_ActionException (
  123. get_class ($this->controller),
  124. $action_name,
  125. Minz_Exception::ERROR
  126. );
  127. }
  128. call_user_func (array (
  129. $this->controller,
  130. $action_name
  131. ));
  132. }
  133. }
  134. }