FrontController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. try {
  34. Configuration::init ();
  35. Request::init ();
  36. $this->router = new Router ();
  37. $this->router->init ();
  38. } catch (RouteNotFoundException $e) {
  39. Log::record ($e->getMessage (), Log::ERROR);
  40. Error::error (
  41. 404,
  42. array ('error' => array ($e->getMessage ()))
  43. );
  44. } catch (MinzException $e) {
  45. Log::record ($e->getMessage (), Log::ERROR);
  46. $this->killApp ();
  47. }
  48. $this->dispatcher = Dispatcher::getInstance ($this->router);
  49. }
  50. /**
  51. * Inclue les fichiers de la librairie
  52. */
  53. private function loadLib () {
  54. require ('ActionController.php');
  55. require ('Cache.php');
  56. require ('Configuration.php');
  57. require ('Dispatcher.php');
  58. require ('Error.php');
  59. require ('Helper.php');
  60. require ('Log.php');
  61. require ('Model.php');
  62. require ('Paginator.php');
  63. require ('Request.php');
  64. require ('Response.php');
  65. require ('Router.php');
  66. require ('Session.php');
  67. require ('Translate.php');
  68. require ('Url.php');
  69. require ('View.php');
  70. require ('dao/Model_pdo.php');
  71. require ('dao/Model_txt.php');
  72. require ('dao/Model_array.php');
  73. require ('exceptions/MinzException.php');
  74. }
  75. /**
  76. * Démarre l'application (lance le dispatcher et renvoie la réponse
  77. */
  78. public function run () {
  79. try {
  80. $this->dispatcher->run ();
  81. Response::send ();
  82. } catch (MinzException $e) {
  83. Log::record ($e->getMessage (), Log::ERROR);
  84. $this->killApp ();
  85. }
  86. }
  87. /**
  88. * Permet d'arrêter le programme en urgence
  89. */
  90. private function killApp () {
  91. exit ('### Application problem ###'."\n".
  92. 'See logs files');
  93. }
  94. }