52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
final class JsonResponse
|
|
{
|
|
public static function bootstrap(): void
|
|
{
|
|
ini_set('display_errors', '0');
|
|
ini_set('html_errors', '0');
|
|
error_reporting(E_ALL);
|
|
|
|
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
|
|
if (!(error_reporting() & $severity)) {
|
|
return false;
|
|
}
|
|
|
|
throw new \ErrorException($message, 0, $severity, $file, $line);
|
|
});
|
|
|
|
set_exception_handler(static function (\Throwable $exception): void {
|
|
error_log(sprintf(
|
|
'[PTA API] %s: %s in %s:%d',
|
|
get_class($exception),
|
|
$exception->getMessage(),
|
|
$exception->getFile(),
|
|
$exception->getLine()
|
|
));
|
|
|
|
$payload = ['error' => 'Une erreur serveur est survenue.'];
|
|
if ((getenv('PTA_DEBUG') ?: '1') !== '0') {
|
|
$payload['details'] = $exception->getMessage();
|
|
}
|
|
|
|
self::send(500, $payload);
|
|
});
|
|
}
|
|
|
|
public static function send(int $status, array $payload): void
|
|
{
|
|
if (!headers_sent()) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
}
|
|
|
|
http_response_code($status);
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
exit;
|
|
}
|
|
}
|