Dispatcher.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 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 (is_null (self::$instance)) {
  22. self::$instance = new 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 MinzException
  36. */
  37. public function run () {
  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. ob_start ('ob_gzhandler');
  44. if (Minz_Cache::isEnabled () && !$cache->expired ()) {
  45. ob_start ();
  46. $cache->render ();
  47. $text = ob_get_clean();
  48. } else {
  49. while (Request::$reseted) {
  50. Request::$reseted = false;
  51. try {
  52. $this->createController (
  53. Request::controllerName ()
  54. . 'Controller'
  55. );
  56. $this->controller->init ();
  57. $this->controller->firstAction ();
  58. $this->launchAction (
  59. Request::actionName ()
  60. . 'Action'
  61. );
  62. $this->controller->lastAction ();
  63. if (!Request::$reseted) {
  64. ob_start ();
  65. $this->controller->view ()->build ();
  66. $text = ob_get_clean();
  67. }
  68. } catch (MinzException $e) {
  69. throw $e;
  70. }
  71. }
  72. if (Minz_Cache::isEnabled ()) {
  73. $cache->cache ($text);
  74. }
  75. }
  76. Response::setBody ($text);
  77. }
  78. /**
  79. * Instancie le Controller
  80. * @param $controller_name le nom du controller à instancier
  81. * @exception FileNotExistException le fichier correspondant au
  82. * > controller n'existe pas
  83. * @exception ControllerNotExistException le controller n'existe pas
  84. * @exception ControllerNotActionControllerException controller n'est
  85. * > pas une instance de ActionController
  86. */
  87. private function createController ($controller_name) {
  88. $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
  89. . $controller_name . '.php';
  90. if (!file_exists ($filename)) {
  91. throw new FileNotExistException (
  92. $filename,
  93. MinzException::ERROR
  94. );
  95. }
  96. require_once ($filename);
  97. if (!class_exists ($controller_name)) {
  98. throw new ControllerNotExistException (
  99. $controller_name,
  100. MinzException::ERROR
  101. );
  102. }
  103. $this->controller = new $controller_name ($this->router);
  104. if (! ($this->controller instanceof ActionController)) {
  105. throw new ControllerNotActionControllerException (
  106. $controller_name,
  107. MinzException::ERROR
  108. );
  109. }
  110. }
  111. /**
  112. * Lance l'action sur le controller du dispatcher
  113. * @param $action_name le nom de l'action
  114. * @exception ActionException si on ne peut pas exécuter l'action sur
  115. * > le controller
  116. */
  117. private function launchAction ($action_name) {
  118. if (!Request::$reseted) {
  119. if (!is_callable (array (
  120. $this->controller,
  121. $action_name
  122. ))) {
  123. throw new ActionException (
  124. get_class ($this->controller),
  125. $action_name,
  126. MinzException::ERROR
  127. );
  128. }
  129. call_user_func (array (
  130. $this->controller,
  131. $action_name
  132. ));
  133. }
  134. }
  135. }