Dispatcher.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 (is_null (self::$instance)) {
  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 () {
  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 (Minz_Request::$reseted) {
  50. Minz_Request::$reseted = false;
  51. try {
  52. $this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller');
  53. $this->controller->init ();
  54. $this->controller->firstAction ();
  55. $this->launchAction (
  56. Minz_Request::actionName ()
  57. . 'Action'
  58. );
  59. $this->controller->lastAction ();
  60. if (!Minz_Request::$reseted) {
  61. ob_start ();
  62. $this->controller->view ()->build ();
  63. $text = ob_get_clean();
  64. }
  65. } catch (Minz_Exception $e) {
  66. throw $e;
  67. }
  68. }
  69. if (Minz_Cache::isEnabled ()) {
  70. $cache->cache ($text);
  71. }
  72. }
  73. Minz_Response::setBody ($text);
  74. }
  75. /**
  76. * Instancie le Controller
  77. * @param $controller_name le nom du controller à instancier
  78. * @exception ControllerNotExistException le controller n'existe pas
  79. * @exception ControllerNotActionControllerException controller n'est
  80. * > pas une instance de ActionController
  81. */
  82. private function createController ($controller_name) {
  83. $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/'
  84. . $controller_name . '.php';
  85. if (!class_exists ($controller_name)) {
  86. throw new Minz_ControllerNotExistException (
  87. $controller_name,
  88. Minz_Exception::ERROR
  89. );
  90. }
  91. $this->controller = new $controller_name ($this->router);
  92. if (! ($this->controller instanceof Minz_ActionController)) {
  93. throw new Minz_ControllerNotActionControllerException (
  94. $controller_name,
  95. Minz_Exception::ERROR
  96. );
  97. }
  98. }
  99. /**
  100. * Lance l'action sur le controller du dispatcher
  101. * @param $action_name le nom de l'action
  102. * @exception ActionException si on ne peut pas exécuter l'action sur
  103. * le controller
  104. */
  105. private function launchAction ($action_name) {
  106. if (!Minz_Request::$reseted) {
  107. if (!is_callable (array (
  108. $this->controller,
  109. $action_name
  110. ))) {
  111. throw new Minz_ActionException (
  112. get_class ($this->controller),
  113. $action_name,
  114. Minz_Exception::ERROR
  115. );
  116. }
  117. call_user_func (array (
  118. $this->controller,
  119. $action_name
  120. ));
  121. }
  122. }
  123. }