FrontController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Minz_Dispatcher $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 = Minz_Url::build();
  36. $url['params'] = array_merge(
  37. empty($url['params']) || !is_array($url['params']) ? [] : $url['params'],
  38. $_POST
  39. );
  40. Minz_Request::forward($url);
  41. } catch (Minz_Exception $e) {
  42. Minz_Log::error($e->getMessage());
  43. self::killApp($e->getMessage());
  44. }
  45. $this->dispatcher = Minz_Dispatcher::getInstance();
  46. }
  47. /**
  48. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  49. */
  50. public function run(): void {
  51. try {
  52. $this->dispatcher->run();
  53. } catch (Minz_Exception $e) {
  54. try {
  55. Minz_Log::error($e->getMessage());
  56. } catch (Minz_PermissionDeniedException $e) {
  57. self::killApp($e->getMessage());
  58. }
  59. if ($e instanceof Minz_FileNotExistException ||
  60. $e instanceof Minz_ControllerNotExistException ||
  61. $e instanceof Minz_ControllerNotActionControllerException ||
  62. $e instanceof Minz_ActionException) {
  63. Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
  64. } else {
  65. self::killApp($e->getMessage());
  66. }
  67. }
  68. }
  69. /**
  70. * Kills the programme
  71. * @return never
  72. */
  73. public static function killApp(string $txt = '') {
  74. header('HTTP/1.1 500 Internal Server Error', true, 500);
  75. if (function_exists('errorMessageInfo')) {
  76. //If the application has defined a custom error message function
  77. die(errorMessageInfo('Application problem', $txt));
  78. }
  79. die('### Application problem ###<br />' . "\n" . $txt);
  80. }
  81. private function setReporting(): void {
  82. $envType = getenv('FRESHRSS_ENV');
  83. if ($envType == '') {
  84. $conf = Minz_Configuration::get('system');
  85. $envType = $conf->environment;
  86. }
  87. switch ($envType) {
  88. case 'development':
  89. error_reporting(E_ALL);
  90. ini_set('display_errors', 'On');
  91. ini_set('log_errors', 'On');
  92. break;
  93. case 'silent':
  94. error_reporting(0);
  95. break;
  96. case 'production':
  97. default:
  98. error_reporting(E_ALL);
  99. ini_set('display_errors', 'Off');
  100. ini_set('log_errors', 'On');
  101. break;
  102. }
  103. }
  104. }