FrontController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Dispatcher 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 Minz_FrontController {
  25. protected $dispatcher;
  26. protected $router;
  27. /**
  28. * Constructeur
  29. * Initialise le router et le dispatcher
  30. */
  31. public function __construct () {
  32. if (LOG_PATH === false) {
  33. $this->killApp ('Path doesn\'t exist : LOG_PATH');
  34. }
  35. try {
  36. Minz_Configuration::init ();
  37. Minz_Request::init ();
  38. $this->router = new Minz_Router ();
  39. $this->router->init ();
  40. } catch (Minz_RouteNotFoundException $e) {
  41. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  42. Minz_Error::error (
  43. 404,
  44. array ('error' => array ($e->getMessage ()))
  45. );
  46. } catch (Minz_Exception $e) {
  47. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  48. $this->killApp ($e->getMessage ());
  49. }
  50. $this->dispatcher = Minz_Dispatcher::getInstance ($this->router);
  51. }
  52. /**
  53. * Démarre l'application (lance le dispatcher et renvoie la réponse
  54. */
  55. public function run () {
  56. try {
  57. $this->dispatcher->run ();
  58. Minz_Response::send ();
  59. } catch (Minz_Exception $e) {
  60. try {
  61. Minz_Log::record ($e->getMessage (), Minz_Log::ERROR);
  62. } catch (Minz_PermissionDeniedException $e) {
  63. $this->killApp ($e->getMessage ());
  64. }
  65. if ($e instanceof Minz_FileNotExistException ||
  66. $e instanceof Minz_ControllerNotExistException ||
  67. $e instanceof Minz_ControllerNotActionControllerException ||
  68. $e instanceof Minz_ActionException) {
  69. Minz_Error::error (
  70. 404,
  71. array ('error' => array ($e->getMessage ())),
  72. true
  73. );
  74. } else {
  75. $this->killApp ();
  76. }
  77. }
  78. }
  79. /**
  80. * Permet d'arrêter le programme en urgence
  81. */
  82. private function killApp ($txt = '') {
  83. if ($txt == '') {
  84. $txt = 'See logs files';
  85. }
  86. exit ('### Application problem ###<br />'."\n".$txt);
  87. }
  88. }