|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Error Controller (500) |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <[email protected]> |
| 12 | + * @copyright 2026 phpMyFAQ Team |
| 13 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | + * @link https://www.phpmyfaq.de |
| 15 | + * @since 2026-01-03 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Controller\Frontend; |
| 21 | + |
| 22 | +use Exception; |
| 23 | +use phpMyFAQ\Translation; |
| 24 | +use Symfony\Component\HttpFoundation\Request; |
| 25 | +use Symfony\Component\HttpFoundation\Response; |
| 26 | +use Twig\Environment; |
| 27 | +use Twig\Loader\FilesystemLoader; |
| 28 | + |
| 29 | +final class ErrorController extends AbstractFrontController |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Static fallback method for early bootstrap errors |
| 33 | + * Used when the system is not fully initialized yet |
| 34 | + * |
| 35 | + * @param string|null $errorMessage Optional error message to display |
| 36 | + * @throws Exception |
| 37 | + */ |
| 38 | + public static function renderBootstrapError(?string $errorMessage = null): Response |
| 39 | + { |
| 40 | + $loader = new FilesystemLoader(PMF_ROOT_DIR . '/assets/templates/error'); |
| 41 | + $twig = new Environment($loader); |
| 42 | + $html = $twig->render('500.twig', [ |
| 43 | + 'errorMessage' => $errorMessage, |
| 44 | + ]); |
| 45 | + |
| 46 | + $response = new Response(content: $html, status: Response::HTTP_INTERNAL_SERVER_ERROR); |
| 47 | + $response->headers->set('Content-Type', 'text/html; charset=utf-8'); |
| 48 | + |
| 49 | + return $response; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Renders a 500 Internal Server Error page |
| 54 | + * |
| 55 | + * @param string|null $errorMessage Optional error message to display (shown only in debug mode) |
| 56 | + * @throws Exception |
| 57 | + */ |
| 58 | + public function internalServerError(Request $request, ?string $errorMessage = null): Response |
| 59 | + { |
| 60 | + try { |
| 61 | + $response = $this->render('500.twig', [ |
| 62 | + ...$this->getHeader($request), |
| 63 | + 'title' => sprintf('%s - %s', Translation::get(key: 'msgError500'), $this->configuration->getTitle()), |
| 64 | + 'errorMessage' => $errorMessage, |
| 65 | + ]); |
| 66 | + } catch (Exception) { |
| 67 | + $response = $this->renderMinimalError($errorMessage); |
| 68 | + } |
| 69 | + |
| 70 | + $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); |
| 71 | + |
| 72 | + return $response; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Renders a minimal standalone error page without dependencies |
| 77 | + * |
| 78 | + * @param string|null $errorMessage Optional error message to display |
| 79 | + * @throws Exception |
| 80 | + */ |
| 81 | + private function renderMinimalError(?string $errorMessage = null): Response |
| 82 | + { |
| 83 | + $loader = new FilesystemLoader(PMF_ROOT_DIR . '/assets/templates/error'); |
| 84 | + $twig = new Environment($loader); |
| 85 | + $html = $twig->render('500.twig', [ |
| 86 | + 'errorMessage' => $errorMessage, |
| 87 | + ]); |
| 88 | + |
| 89 | + $response = new Response(content: $html, status: Response::HTTP_INTERNAL_SERVER_ERROR); |
| 90 | + $response->headers->set('Content-Type', 'text/html; charset=utf-8'); |
| 91 | + |
| 92 | + return $response; |
| 93 | + } |
| 94 | +} |
0 commit comments