FrontController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # MINZ - a free PHP Framework like Zend Framework
  4. # Copyright (C) 2011 Marien Fressinaud
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # ***** END LICENSE BLOCK *****
  20. /**
  21. * La classe FrontController est le noyau du framework, elle lance l'application
  22. * Elle est appelée en général dans le fichier index.php à la racine du serveur
  23. */
  24. class FrontController {
  25. protected $dispatcher;
  26. protected $router;
  27. /**
  28. * Constructeur
  29. * Initialise le router et le dispatcher
  30. */
  31. public function __construct () {
  32. $this->loadLib ();
  33. if (LOG_PATH === false) {
  34. $this->killApp ('Path doesn\'t exist : LOG_PATH');
  35. }
  36. try {
  37. Configuration::init ();
  38. Request::init ();
  39. $this->router = new Router ();
  40. $this->router->init ();
  41. } catch (RouteNotFoundException $e) {
  42. Log::record ($e->getMessage (), Log::ERROR);
  43. Error::error (
  44. 404,
  45. array ('error' => array ($e->getMessage ()))
  46. );
  47. } catch (MinzException $e) {
  48. Log::record ($e->getMessage (), Log::ERROR);
  49. $this->killApp ();
  50. }
  51. $this->dispatcher = Dispatcher::getInstance ($this->router);
  52. }
  53. /**
  54. * Inclue les fichiers de la librairie
  55. */
  56. private function loadLib () {
  57. require ('ActionController.php');
  58. require ('Cache.php');
  59. require ('Configuration.php');
  60. require ('Dispatcher.php');
  61. require ('Error.php');
  62. require ('Helper.php');
  63. require ('Log.php');
  64. require ('Model.php');
  65. require ('Paginator.php');
  66. require ('Request.php');
  67. require ('Response.php');
  68. require ('Router.php');
  69. require ('Session.php');
  70. require ('Translate.php');
  71. require ('Url.php');
  72. require ('View.php');
  73. require ('dao/Model_pdo.php');
  74. require ('dao/Model_txt.php');
  75. require ('dao/Model_array.php');
  76. require ('exceptions/MinzException.php');
  77. }
  78. /**
  79. * Démarre l'application (lance le dispatcher et renvoie la réponse
  80. */
  81. public function run () {
  82. try {
  83. $this->dispatcher->run ();
  84. Response::send ();
  85. } catch (MinzException $e) {
  86. Log::record ($e->getMessage (), Log::ERROR);
  87. if ($e instanceof FileNotExistException ||
  88. $e instanceof ControllerNotExistException ||
  89. $e instanceof ControllerNotActionControllerException ||
  90. $e instanceof ActionException) {
  91. Error::error (
  92. 404,
  93. array ('error' => array ($e->getMessage ())),
  94. true
  95. );
  96. } else {
  97. $this->killApp ();
  98. }
  99. }
  100. }
  101. /**
  102. * Permet d'arrêter le programme en urgence
  103. */
  104. private function killApp ($txt = '') {
  105. if ($txt == '') {
  106. $txt = 'See logs files';
  107. }
  108. exit ('### Application problem ###'."\n".$txt);
  109. }
  110. }