4
0

FrontController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. declare(strict_types=1);
  3. # ***** BEGIN LICENSE BLOCK *****
  4. # MINZ - a free PHP Framework like Zend Framework
  5. # Copyright (C) 2011 Marien Fressinaud
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # ***** END LICENSE BLOCK *****
  21. /**
  22. * The Minz_FrontController class is the framework Dispatcher.
  23. * It runs the application.
  24. * It is generally invoqued by an index.php file at the root.
  25. */
  26. class Minz_FrontController {
  27. protected Minz_Dispatcher $dispatcher;
  28. /**
  29. * Constructeur
  30. * Initialise le dispatcher, met à jour la Request
  31. */
  32. public function __construct() {
  33. try {
  34. $this->setReporting();
  35. Minz_Request::init();
  36. $url = Minz_Url::build();
  37. $url['params'] = array_merge(
  38. empty($url['params']) || !is_array($url['params']) ? [] : $url['params'],
  39. $_POST
  40. );
  41. Minz_Request::forward($url);
  42. } catch (Minz_Exception $e) {
  43. Minz_Log::error($e->getMessage());
  44. self::killApp($e->getMessage());
  45. }
  46. $this->dispatcher = Minz_Dispatcher::getInstance();
  47. }
  48. /**
  49. * Démarre l'application (lance le dispatcher et renvoie la réponse)
  50. */
  51. public function run(): void {
  52. try {
  53. $this->dispatcher->run();
  54. } catch (Minz_Exception $e) {
  55. try {
  56. Minz_Log::error($e->getMessage());
  57. } catch (Minz_PermissionDeniedException $e) {
  58. self::killApp($e->getMessage());
  59. }
  60. if ($e instanceof Minz_FileNotExistException ||
  61. $e instanceof Minz_ControllerNotExistException ||
  62. $e instanceof Minz_ControllerNotActionControllerException ||
  63. $e instanceof Minz_ActionException) {
  64. Minz_Error::error(404, ['error' => [$e->getMessage()]], true);
  65. } else {
  66. self::killApp($e->getMessage());
  67. }
  68. }
  69. }
  70. /**
  71. * Kills the programme
  72. * @return never
  73. */
  74. public static function killApp(string $txt = '') {
  75. header('HTTP/1.1 500 Internal Server Error', true, 500);
  76. if (function_exists('errorMessageInfo')) {
  77. //If the application has defined a custom error message function
  78. die(errorMessageInfo('Application problem', $txt));
  79. }
  80. die('### Application problem ###<br />' . "\n" . $txt);
  81. }
  82. private function setReporting(): void {
  83. $envType = getenv('FRESHRSS_ENV');
  84. if ($envType == '') {
  85. $conf = Minz_Configuration::get('system');
  86. $envType = $conf->environment;
  87. }
  88. switch ($envType) {
  89. case 'development':
  90. error_reporting(E_ALL);
  91. ini_set('display_errors', 'On');
  92. ini_set('log_errors', 'On');
  93. break;
  94. case 'silent':
  95. error_reporting(0);
  96. break;
  97. case 'production':
  98. default:
  99. error_reporting(E_ALL);
  100. ini_set('display_errors', 'Off');
  101. ini_set('log_errors', 'On');
  102. break;
  103. }
  104. }
  105. }