/** * @param CliMenu $menu */ public function __invoke(CliMenu $menu) { $menu->close(); $item = $menu->getSelectedItem(); $exercise = $this->exerciseRepository->findByName($item->getText()); $exercises = $this->exerciseRepository->findAll(); $this->userState->setCurrentExercise($item->getText()); $this->userStateSerializer->serialize($this->userState); $numExercises = count($exercises); $exerciseIndex = array_search($exercise, $exercises) + 1; $output = "\n"; $output .= $this->color->__invoke(' LEARN YOU THE PHP FOR MUCH WIN! ')->magenta()->bold() . "\n"; $output .= $this->color->__invoke('*********************************')->magenta()->bold() . "\n"; $output .= "\n"; $output .= $this->color->__invoke(" " . $exercise->getName())->yellow()->bold() . "\n"; $output .= $this->color->__invoke(sprintf(" Exercise %d of %d\n\n", $exerciseIndex, $numExercises))->yellow(); $content = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($content); $doc = str_replace('{appname}', $this->appName, $doc); $output .= $doc; $output .= "\n"; $output .= $this->helpLine('To print these instructions again, run', 'print'); $output .= $this->helpLine('To execute your program in a test environment, run', 'run program.php'); $output .= $this->helpLine('To verify your program, run', 'verify program.php'); $output .= $this->helpLine('For help run', 'help'); $output .= "\n\n"; $this->output->write($output); }
public function testGetAllNames() { $exercise1 = $this->createMock(ExerciseInterface::class); $exercise2 = $this->createMock(ExerciseInterface::class); $exercise1->expects($this->any())->method('getName')->will($this->returnValue('exercise1')); $exercise2->expects($this->any())->method('getName')->will($this->returnValue('exercise2')); $repo = new ExerciseRepository([$exercise1, $exercise2]); $this->assertSame(['exercise1', 'exercise2'], $repo->getAllNames()); }
/** * @return int|void */ public function __invoke() { $currentExercise = $this->userState->getCurrentExercise(); $exercise = $this->exerciseRepository->findByName($currentExercise); $markDown = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($markDown); $doc = str_replace('{appname}', $this->appName, $doc); $this->output->write($doc); }
/** * @param Event $event */ public function __invoke(Event $event) { /** @var CommandDefinition $command */ $command = $event->getParameter('command'); if (!in_array($command->getName(), ['verify', 'run'])) { return; } $currentExercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->runnerManager->configureInput($currentExercise, $command); }
/** * @param Input $input The command line arguments passed to the command. * * @return int|void */ public function __invoke(Input $input) { $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $results = $this->exerciseDispatcher->verify($exercise, $input); if ($results->isSuccessful()) { $this->userState->addCompletedExercise($exercise->getName()); $this->userStateSerializer->serialize($this->userState); } $this->resultsRenderer->render($results, $exercise, $this->userState, $this->output); return $results->isSuccessful() ? 0 : 1; }
/** * @return int|void */ public function __invoke() { if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $currentExercise = $this->userState->getCurrentExercise(); $exercise = $this->exerciseRepository->findByName($currentExercise); $markDown = file_get_contents($exercise->getProblem()); $doc = $this->markdownRenderer->render($markDown); $doc = str_replace('{appname}', $this->appName, $doc); $this->output->write($doc); }
/** * @param string $appName * @param string $program * * @return int|void */ public function __invoke($appName, $program) { if (!file_exists($program)) { $this->output->printError(sprintf('Could not run. File: "%s" does not exist', $program)); return 1; } $program = realpath($program); if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->exerciseDispatcher->run($exercise, $program, $this->output); }
/** * @param ExerciseInterface $exercise * @param UserState $userState * @param OutputInterface $output */ private function renderSuccessInformation(ExerciseInterface $exercise, UserState $userState, OutputInterface $output) { $output->emptyLine(); $output->writeLine($this->style(" PASS!", ['green', 'bg_default', 'bold'])); $output->emptyLine(); $output->writeLine("Here's the official solution in case you want to compare notes:"); $output->writeLine($this->lineBreak()); foreach ($exercise->getSolution()->getFiles() as $file) { $output->writeLine($this->style($file->getRelativePath(), ['bold', 'cyan', 'underline'])); $output->emptyLine(); $code = $file->getContents(); if (pathinfo($file->getRelativePath(), PATHINFO_EXTENSION) === 'php') { $code = $this->syntaxHighlighter->highlight($code); } //make sure there is a new line at the end $code = preg_replace('/\\n$/', '', $code) . "\n"; $output->write($code); $output->writeLine($this->lineBreak()); } $completedCount = count($userState->getCompletedExercises()); $numExercises = $this->exerciseRepository->count(); $output->writeLine(sprintf('You have %d challenges left.', $numExercises - $completedCount)); $output->writeLine(sprintf('Type "%s" to show the menu.', $this->appName)); $output->emptyLine(); }
/** * @param string $appName * @param string $program * * @return int|void */ public function __invoke($appName, $program) { if (!file_exists($program)) { $this->output->printError(sprintf('Could not verify. File: "%s" does not exist', $program)); return 1; } $program = realpath($program); if (!$this->userState->isAssignedExercise()) { $this->output->printError("No active exercises. Select one from the menu"); return 1; } $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $results = $this->exerciseDispatcher->verify($exercise, $program); if ($results->isSuccessful()) { $this->userState->addCompletedExercise($exercise->getName()); $this->userStateSerializer->serialize($this->userState); } $this->resultsRenderer->render($results, $exercise, $this->userState, $this->output); return $results->isSuccessful() ? 0 : 1; }
/** * @param Input $input The command line arguments passed to the command. * * @return int|void */ public function __invoke(Input $input) { $exercise = $this->exerciseRepository->findByName($this->userState->getCurrentExercise()); $this->exerciseDispatcher->run($exercise, $input, $this->output); }
public function testGetAllNames() { $exercises = [new CliExerciseImpl('Exercise 1'), new CliExerciseImpl('Exercise 2')]; $repo = new ExerciseRepository($exercises); $this->assertSame(['Exercise 1', 'Exercise 2'], $repo->getAllNames()); }