FrontController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /**
  27. * Constructeur
  28. * Initialise le dispatcher, met à jour la Request
  29. */
  30. public function __construct () {
  31. try {
  32. Minz_Configuration::register('system',
  33. DATA_PATH . '/config.php',
  34. FRESHRSS_PATH . '/config.default.php');
  35. $this->setReporting();
  36. Minz_Request::init();
  37. $url = $this->buildUrl();
  38. $url['params'] = array_merge (
  39. $url['params'],
  40. Minz_Request::fetchPOST ()
  41. );
  42. Minz_Request::forward ($url);
  43. } catch (Minz_Exception $e) {
  44. Minz_Log::error($e->getMessage());
  45. $this->killApp ($e->getMessage ());
  46. }
  47. $this->dispatcher = Minz_Dispatcher::getInstance();
  48. }
  49. /**
  50. * Retourne un tableau représentant l'url passée par la barre d'adresses
  51. * @return tableau représentant l'url
  52. */
  53. private function buildUrl() {
  54. $url = array ();
  55. $url['c'] = Minz_Request::fetchGET (
  56. 'c',
  57. Minz_Request::defaultControllerName ()
  58. );
  59. $url['a'] = Minz_Request::fetchGET (
  60. 'a',
  61. Minz_Request::defaultActionName ()
  62. );
  63. $url['params'] = Minz_Request::fetchGET ();
  64. // post-traitement
  65. unset ($url['params']['c']);
  66. unset ($url['params']['a']);
  67. return $url;
  68. }
  69. /**
  70. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  71. */
  72. public function run () {
  73. try {
  74. $this->dispatcher->run();
  75. } catch (Minz_Exception $e) {
  76. try {
  77. Minz_Log::error($e->getMessage());
  78. } catch (Minz_PermissionDeniedException $e) {
  79. $this->killApp ($e->getMessage ());
  80. }
  81. if ($e instanceof Minz_FileNotExistException ||
  82. $e instanceof Minz_ControllerNotExistException ||
  83. $e instanceof Minz_ControllerNotActionControllerException ||
  84. $e instanceof Minz_ActionException) {
  85. Minz_Error::error (
  86. 404,
  87. array ('error' => array ($e->getMessage ())),
  88. true
  89. );
  90. } else {
  91. $this->killApp ();
  92. }
  93. }
  94. }
  95. /**
  96. * Permet d'arrêter le programme en urgence
  97. */
  98. private function killApp ($txt = '') {
  99. if ($txt == '') {
  100. $txt = 'See logs files';
  101. }
  102. exit ('### Application problem ###<br />'."\n".$txt);
  103. }
  104. private function setReporting() {
  105. $envType = getenv('FRESHRSS_ENV');
  106. if ($envType == '') {
  107. $conf = Minz_Configuration::get('system');
  108. $envType = $conf->environment;
  109. }
  110. switch ($envType) {
  111. case 'development':
  112. error_reporting(E_ALL);
  113. ini_set('display_errors', 'On');
  114. ini_set('log_errors', 'On');
  115. break;
  116. case 'silent':
  117. error_reporting(0);
  118. break;
  119. case 'production':
  120. default:
  121. error_reporting(E_ALL);
  122. ini_set('display_errors', 'Off');
  123. ini_set('log_errors', 'On');
  124. break;
  125. }
  126. }
  127. }