4
0

FrontController.php 2.9 KB

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