diff --git a/src/Controller/ChallengeController.php b/src/Controller/ChallengeController.php index 0b2465a..f923a57 100644 --- a/src/Controller/ChallengeController.php +++ b/src/Controller/ChallengeController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Service\QuestionsService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -10,10 +11,22 @@ class ChallengeController extends AbstractController { #[Route('/challenge', name: 'app_challenge')] - public function randomChallenge(): Response { - + public function randomChallenge(): Response + { return $this->render('challenge/start.html.twig'); - } + #[Route('/list', name: 'app_list')] + public function list(QuestionsService $questionsService): Response + { + $listOfChallenges = $questionsService->getList(); + return $this->render('challenge/list.html.twig', ['list' => $listOfChallenges]); + } + + #[Route('/challenge/{challenge<.*>}', name: 'app_challenge')] + public function challenge(QuestionsService $questionsService, string $challenge): Response + { + $questions = $questionsService->getQuestions($challenge); + return $this->render('challenge/list.html.twig', ['list' => $questions, 'challenge' => $challenge]); + } } \ No newline at end of file diff --git a/src/Service/QuestionsService.php b/src/Service/QuestionsService.php index 7366efd..20bfa41 100644 --- a/src/Service/QuestionsService.php +++ b/src/Service/QuestionsService.php @@ -3,6 +3,7 @@ namespace App\Service; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\Yaml\Yaml; class QuestionsService @@ -11,10 +12,10 @@ class QuestionsService { } - public function getQuestions(): array + public function getQuestions(?string $file): array { $questions = []; - foreach ($this->getQuestionFiles() as $file) { + foreach ($this->getQuestionFiles($file) as $file) { $yamlFile = Yaml::parseFile($file->getRealPath()); $questions = array_merge($questions, $yamlFile['questions']); } @@ -22,10 +23,24 @@ class QuestionsService return $questions; } - private function getQuestionFiles(): array + private function getQuestionFiles(?string $file = null): array { $finder = new Finder(); + if ($file) { + $finder->name($file); + } else { + $finder->name('*.yaml'); + } - return iterator_to_array($finder->files()->in($this->paths)->name('*.yaml')); + return iterator_to_array($finder->files()->in($this->paths)); } + + /** + * @return SplFileInfo[] + */ + public function getList(): array + { + return $this->getQuestionFiles(); + } + } \ No newline at end of file diff --git a/templates/challenge/list.html.twig b/templates/challenge/list.html.twig index 567dc06..5be80b4 100644 --- a/templates/challenge/list.html.twig +++ b/templates/challenge/list.html.twig @@ -2,48 +2,37 @@ {% block body %}