Commit technique : reproduit à l'identique l'état non commité de la copie de travail (sécurisation V7.8, CSP à nonce, Session, migration des droits) afin que le travail d'ergonomie qui suit soit isolable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
50 lines
1.6 KiB
PHP
50 lines
1.6 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 {
|
|
// Security::reportThrowable journalise systématiquement le détail
|
|
// côté serveur et ne le renvoie au client que si PTA_DEBUG est
|
|
// explicitement activé. Par défaut, rien ne fuit.
|
|
$payload = ['error' => 'Une erreur serveur est survenue.'];
|
|
if (Security::debugEnabled()) {
|
|
$payload['details'] = $exception->getMessage();
|
|
}
|
|
Security::reportThrowable($exception, 'Erreur API.');
|
|
|
|
self::send(500, $payload);
|
|
});
|
|
}
|
|
|
|
public static function send(int $status, array $payload): void
|
|
{
|
|
if (!headers_sent()) {
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
header('X-Content-Type-Options: nosniff');
|
|
header('Cache-Control: no-store');
|
|
}
|
|
|
|
http_response_code($status);
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
exit;
|
|
}
|
|
}
|