src/Controller/StoryController.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. class StoryController extends AbstractController
  7. {
  8.     private $entries = [
  9.         [
  10.             'title' => 'Entry #1 - The beginning',
  11.             'file' => 'the-beginning'
  12.         ],
  13.         [
  14.             'title' => 'Entry #2 - Safety first',
  15.             'file' => 'safety-first'
  16.         ],
  17.         [
  18.             'title' => 'Entry #3 - Recon',
  19.             'file' => 'recon'
  20.         ],
  21.         [
  22.             'title' => 'Entry #4 - First contact',
  23.             'file' => 'first-contact'
  24.         ],
  25.         [
  26.             'title' => 'Entry #5 - Trouble..',
  27.             'file' => 'trouble-in-paradise'
  28.         ],
  29.         [
  30.             'title' => 'Entry #6 - Time to strike!',
  31.             'file' => 'time-to-strike'
  32.         ],
  33.         [
  34.             'title' => 'Entry #7 - First blood',
  35.             'file' => 'first-blood'
  36.         ],
  37.         [
  38.             'title' => 'Entry #8 - Going in',
  39.             'file' => 'going-in'
  40.         ],
  41.         [
  42.             'title' => 'Entry #9 - Wrong place..',
  43.             'file' => 'wrong-place-wrong-time'
  44.         ],
  45.         [
  46.             'title' => 'Entry #10 - Wild Animals',
  47.             'file' => 'wild-animals'
  48.         ],
  49.         [
  50.             'title' => 'Entry #11 - Jacksons wager',
  51.             'file' => 'jacksons-wager'
  52.         ],
  53.         [
  54.             'title' => 'Entry #12 - Brigs story',
  55.             'file' => 'brigs-story'
  56.         ],
  57.         [
  58.             'title' => 'Entry #13 - Dedicated to the people',
  59.             'file' => 'dedicated-to-the-people'
  60.         ]
  61.     ];
  62.     /**
  63.      * @Route("/story", name="story_index")
  64.      *
  65.      * @return Response
  66.      */
  67.     public function index(): Response
  68.     {
  69.         return $this->render('story/index.html.twig', [
  70.             'page' => 'story',
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/diaries", name="story_diary_list")
  75.      *
  76.      * @return Response
  77.      */
  78.     public function diaryList(): Response
  79.     {
  80.         return $this->render('story/diary_list.html.twig', [
  81.             'entries' => $this->entries,
  82.             'page' => 'story',
  83.         ]);
  84.     }
  85.     /**
  86.      * @Route("/diaries/{diary}", name="story_diary_entry")
  87.      *
  88.      * @param string $diary
  89.      *
  90.      * @return Response
  91.      */
  92.     public function diaryEntry(string $diary): Response
  93.     {
  94.         // Load diary content file
  95.         $projectRoot $this->getParameter('kernel.project_dir');
  96.         $filePath $projectRoot '/diary/' $diary '.txt';
  97.         if (!file_exists($filePath)) {
  98.             return $this->redirectToRoute('story_diary_list');
  99.         }
  100.         $previous $next null;
  101.         foreach ($this->entries as $index => $entry) {
  102.             if ($entry['file'] === $diary) {
  103.                 if (isset($this->entries[$index 1])) {
  104.                     $previous $this->entries[$index 1];
  105.                 }
  106.                 if (isset($this->entries[$index 1])) {
  107.                     $next $this->entries[$index 1];
  108.                 }
  109.             }
  110.         }
  111.         return $this->render('story/diary_entry.html.twig', [
  112.             'diaryContent' => file_get_contents($filePath),
  113.             'previous' => $previous,
  114.             'next' => $next,
  115.             'page' => 'story',
  116.         ]);
  117.     }
  118. }