src/Compatibility/ViewHandler.php line 10

  1. <?php
  2. namespace App\Compatibility;
  3. use Parsedown;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Twig\Environment;
  7. class ViewHandler
  8. {
  9.     public const PAGE_ATTRIBUTE 'page';
  10.     public function __construct(
  11.         private readonly Environment $twig,
  12.         private readonly NavigationRepository $navigationRepository,
  13.     ) {
  14.     }
  15.     public function __invoke(Request $request): Response
  16.     {
  17.         $page $request->attributes->get(self::PAGE_ATTRIBUTE);
  18.         if (!$markdown $this->readMarkdownFile($page)) {
  19.             return new Response(
  20.                 $this->twig->render('404.html.twig'),
  21.                 404
  22.             );
  23.         }
  24.         preg_match('/^# (.+)/'$markdown$matches);
  25.         return new Response(
  26.             $this->twig->render('compatibility.html.twig', [
  27.                 'page' => (new Parsedown())->text($markdown),
  28.                 'title' => $matches[1],
  29.                 'navigation' => $this->navigationRepository->getPageGroups(),
  30.             ])
  31.         );
  32.     }
  33.     private function readMarkdownFile(mixed $page): ?string
  34.     {
  35.         $filename __DIR__ '/markdown/' $page '.md';
  36.         if (!is_readable($filename)) {
  37.             return null;
  38.         }
  39.         return file_get_contents($filename);
  40.     }
  41. }