FrontController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * The Minz_FrontController class is the framework Dispatcher.
  22. * It runs the application.
  23. * It is generally invoqued by an index.php file at the root.
  24. */
  25. class Minz_FrontController {
  26. protected $dispatcher;
  27. /**
  28. * Constructeur
  29. * Initialise le dispatcher, met à jour la Request
  30. */
  31. public function __construct () {
  32. try {
  33. $this->setReporting();
  34. Minz_Request::init();
  35. $url = $this->buildUrl();
  36. $url['params'] = array_merge (
  37. $url['params'],
  38. $_POST
  39. );
  40. Minz_Request::forward ($url);
  41. } catch (Minz_Exception $e) {
  42. Minz_Log::error($e->getMessage());
  43. $this->killApp ($e->getMessage());
  44. }
  45. $this->dispatcher = Minz_Dispatcher::getInstance();
  46. }
  47. /**
  48. * Returns an array representing the URL as passed in the address bar
  49. * @return array URL representation
  50. */
  51. private function buildUrl() {
  52. $url = array();
  53. $url['c'] = $_GET['c'] ?? Minz_Request::defaultControllerName();
  54. $url['a'] = $_GET['a'] ?? Minz_Request::defaultActionName();
  55. $url['params'] = $_GET;
  56. // post-traitement
  57. unset($url['params']['c']);
  58. unset($url['params']['a']);
  59. return $url;
  60. }
  61. /**
  62. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  63. */
  64. public function run() {
  65. try {
  66. $this->dispatcher->run();
  67. } catch (Minz_Exception $e) {
  68. try {
  69. Minz_Log::error($e->getMessage());
  70. } catch (Minz_PermissionDeniedException $e) {
  71. $this->killApp ($e->getMessage ());
  72. }
  73. if ($e instanceof Minz_FileNotExistException ||
  74. $e instanceof Minz_ControllerNotExistException ||
  75. $e instanceof Minz_ControllerNotActionControllerException ||
  76. $e instanceof Minz_ActionException) {
  77. Minz_Error::error (
  78. 404,
  79. array('error' => array ($e->getMessage ())),
  80. true
  81. );
  82. } else {
  83. $this->killApp($e->getMessage());
  84. }
  85. }
  86. }
  87. /**
  88. * Permet d'arrêter le programme en urgence
  89. */
  90. private function killApp ($txt = '') {
  91. if (function_exists('errorMessage')) {
  92. //If the application has defined a custom error message function
  93. exit(errorMessage('Application problem', $txt));
  94. }
  95. exit('### Application problem ###<br />' . "\n" . $txt);
  96. }
  97. private function setReporting() {
  98. $envType = getenv('FRESHRSS_ENV');
  99. if ($envType == '') {
  100. $conf = Minz_Configuration::get('system');
  101. $envType = $conf->environment;
  102. }
  103. switch ($envType) {
  104. case 'development':
  105. error_reporting(E_ALL);
  106. ini_set('display_errors', 'On');
  107. ini_set('log_errors', 'On');
  108. break;
  109. case 'silent':
  110. error_reporting(0);
  111. break;
  112. case 'production':
  113. default:
  114. error_reporting(E_ALL);
  115. ini_set('display_errors', 'Off');
  116. ini_set('log_errors', 'On');
  117. break;
  118. }
  119. }
  120. }