/** * Initializes the command just after the input has been validated. * * This is mainly useful when a lot of commands extends one main command * where some things need to be initialized based on the input arguments and options. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance */ public function initialize(InputInterface $input, OutputInterface $output) { $this->startTime = microtime(true); $this->input = $input; $this->output = $output; if ($this->loadConfig) { $this->config = OTPGen::loadConfig(); } }
/** * Runs the command. * * @param InputInterface $input * @param OutputInterface $output * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); //Ask te user for a name which will be used as key $question = new Question('Enter a name: '); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Please enter a name.'); } if (strrpos($answer, " ") === true) { throw new \RuntimeException('No spaces allowed in your name.'); } return $answer; }); $name = $helper->ask($input, $output, $question); $question = new Question('Enter your token: '); $question->setValidator(function ($answer) { if (empty($answer)) { throw new \RuntimeException('Please enter your token.'); } }); $token = $helper->ask($input, $output, $question); $question = new Question('Enter the digest algorithm (default sha1): ', 'sha1'); $digest = $helper->ask($input, $output, $question); $question = new Question('Enter the lenght of the generated token (default 6): ', 6); $size = $helper->ask($input, $output, $question); $question = new Question('Enter the interval of the token (default 30): ', 30); $interval = $helper->ask($input, $output, $question); $data = array($name => array('token' => $token, 'digest' => $digest, 'size' => $size, 'interval' => $interval)); if (file_exists(OTPGEN_WORKING_DIR . '.otp')) { $config = OTPGen::loadConfig(); $data = array_merge($config, $data); } $otpFile = fopen(OTPGEN_WORKING_DIR . '.otp', 'w') or die('Wasn\'t able to open or create library are you sure I have enough permissions?'); fwrite($otpFile, OTPGen::toYAML($data)); fclose($otpFile); $output->writeln('Added ' . $name . ' to your library.'); return 0; }