<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class StoryController extends AbstractController
{
private $entries = [
[
'title' => 'Entry #1 - The beginning',
'file' => 'the-beginning'
],
[
'title' => 'Entry #2 - Safety first',
'file' => 'safety-first'
],
[
'title' => 'Entry #3 - Recon',
'file' => 'recon'
],
[
'title' => 'Entry #4 - First contact',
'file' => 'first-contact'
],
[
'title' => 'Entry #5 - Trouble..',
'file' => 'trouble-in-paradise'
],
[
'title' => 'Entry #6 - Time to strike!',
'file' => 'time-to-strike'
],
[
'title' => 'Entry #7 - First blood',
'file' => 'first-blood'
],
[
'title' => 'Entry #8 - Going in',
'file' => 'going-in'
],
[
'title' => 'Entry #9 - Wrong place..',
'file' => 'wrong-place-wrong-time'
],
[
'title' => 'Entry #10 - Wild Animals',
'file' => 'wild-animals'
],
[
'title' => 'Entry #11 - Jacksons wager',
'file' => 'jacksons-wager'
],
[
'title' => 'Entry #12 - Brigs story',
'file' => 'brigs-story'
],
[
'title' => 'Entry #13 - Dedicated to the people',
'file' => 'dedicated-to-the-people'
]
];
/**
* @Route("/story", name="story_index")
*
* @return Response
*/
public function index(): Response
{
return $this->render('story/index.html.twig', [
'page' => 'story',
]);
}
/**
* @Route("/diaries", name="story_diary_list")
*
* @return Response
*/
public function diaryList(): Response
{
return $this->render('story/diary_list.html.twig', [
'entries' => $this->entries,
'page' => 'story',
]);
}
/**
* @Route("/diaries/{diary}", name="story_diary_entry")
*
* @param string $diary
*
* @return Response
*/
public function diaryEntry(string $diary): Response
{
// Load diary content file
$projectRoot = $this->getParameter('kernel.project_dir');
$filePath = $projectRoot . '/diary/' . $diary . '.txt';
if (!file_exists($filePath)) {
return $this->redirectToRoute('story_diary_list');
}
$previous = $next = null;
foreach ($this->entries as $index => $entry) {
if ($entry['file'] === $diary) {
if (isset($this->entries[$index - 1])) {
$previous = $this->entries[$index - 1];
}
if (isset($this->entries[$index + 1])) {
$next = $this->entries[$index + 1];
}
}
}
return $this->render('story/diary_entry.html.twig', [
'diaryContent' => file_get_contents($filePath),
'previous' => $previous,
'next' => $next,
'page' => 'story',
]);
}
}