FrontController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 = Minz_Url::build();
  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. 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() {
  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 (
  64. 404,
  65. array('error' => array ($e->getMessage ())),
  66. true
  67. );
  68. } else {
  69. self::killApp($e->getMessage());
  70. }
  71. }
  72. }
  73. /**
  74. * Kills the programme
  75. */
  76. public static function killApp($txt = '') {
  77. header('HTTP 1.1 500 Internal Server Error', true, 500);
  78. if (function_exists('errorMessageInfo')) {
  79. //If the application has defined a custom error message function
  80. die(errorMessageInfo('Application problem', $txt));
  81. }
  82. die('### Application problem ###<br />' . "\n" . $txt);
  83. }
  84. private function setReporting() {
  85. $envType = getenv('FRESHRSS_ENV');
  86. if ($envType == '') {
  87. $conf = Minz_Configuration::get('system');
  88. $envType = $conf->environment;
  89. }
  90. switch ($envType) {
  91. case 'development':
  92. error_reporting(E_ALL);
  93. ini_set('display_errors', 'On');
  94. ini_set('log_errors', 'On');
  95. break;
  96. case 'silent':
  97. error_reporting(0);
  98. break;
  99. case 'production':
  100. default:
  101. error_reporting(E_ALL);
  102. ini_set('display_errors', 'Off');
  103. ini_set('log_errors', 'On');
  104. break;
  105. }
  106. }
  107. }