Created
February 6, 2022 12:37
-
-
Save osteel/7373c90d14568b5a5d3b1fc71a76ac6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Osteel\PhpCliDemo\Commands; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Style\SymfonyStyle; | |
class Play extends Command | |
{ | |
/** | |
* The name of the command (the part after "bin/demo"). | |
* | |
* @var string | |
*/ | |
protected static $defaultName = 'play'; | |
/** | |
* The command description shown when running "php bin/demo list". | |
* | |
* @var string | |
*/ | |
protected static $defaultDescription = 'Play the game!'; | |
/** | |
* Execute the command | |
* | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* @return int 0 if everything went fine, or an exit code. | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output): int | |
{ | |
$term1 = rand(1, 10); | |
$term2 = rand(1, 10); | |
$result = $term1 + $term2; | |
$io = new SymfonyStyle($input, $output); | |
$answer = (int) $io->ask(sprintf('What is %s + %s?', $term1, $term2)); | |
if ($answer === $result) { | |
$io->success('Well done!'); | |
} else { | |
$io->error(sprintf('Aww, so close. The answer was %s', $result)); | |
} | |
if ($io->confirm('Play again?')) { | |
return $this->execute($input, $output); | |
} | |
return Command::SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment