Error.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * MINZ - Copyright 2011 Marien Fressinaud
  5. * Sous licence AGPL3 <https://www.gnu.org/licenses/>
  6. */
  7. /**
  8. * The Minz_Error class logs and raises framework errors
  9. */
  10. class Minz_Error {
  11. public function __construct() {}
  12. /**
  13. * Permet de lancer une erreur
  14. * @param int $code le type de l'erreur, par défaut 404 (page not found)
  15. * @param string|array<'error'|'warning'|'notice',list<string>> $logs logs d'erreurs découpés de la forme
  16. * > $logs['error']
  17. * > $logs['warning']
  18. * > $logs['notice']
  19. * @param bool $redirect indique s'il faut forcer la redirection (les logs ne seront pas transmis)
  20. */
  21. public static function error(int $code = 404, string|array $logs = [], bool $redirect = true): void {
  22. $logs = self::processLogs($logs);
  23. Minz_Session::_params([
  24. 'error_code' => $code,
  25. 'error_logs' => $logs,
  26. ]);
  27. Minz_Request::forward(['c' => 'error'], $redirect);
  28. }
  29. /**
  30. * Returns filtered logs
  31. * @param string|array<'error'|'warning'|'notice',list<string>> $logs logs sorted by category (error, warning, notice)
  32. * @return list<string> list of matching logs, without the category, according to environment preferences (production / development)
  33. */
  34. private static function processLogs(string|array $logs): array {
  35. if (is_string($logs)) {
  36. return [$logs];
  37. }
  38. $error = [];
  39. $warning = [];
  40. $notice = [];
  41. if (is_array($logs['error'] ?? null)) {
  42. $error = $logs['error'];
  43. }
  44. if (is_array($logs['warning'] ?? null)) {
  45. $warning = $logs['warning'];
  46. }
  47. if (is_array($logs['notice'] ?? null)) {
  48. $notice = $logs['notice'];
  49. }
  50. switch (Minz_Configuration::get('system')->environment) {
  51. case 'development':
  52. return array_merge($error, $warning, $notice);
  53. case 'production':
  54. default:
  55. return $error;
  56. }
  57. }
  58. }