Dispatcher.php 3.2 KB

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