Skip to content

Commit

Permalink
Making dialog helper backward compatible + introducing new question h…
Browse files Browse the repository at this point in the history
…elper
  • Loading branch information
András Rutkai committed Feb 11, 2016
1 parent 2c72cc5 commit 378ead2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
52 changes: 48 additions & 4 deletions Command/DriverLockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$driver = $this->getDriver();

if ($input->isInteractive()) {
if (!$dialog->askConfirmation($output, '<question>WARNING! Are you sure you wish to continue? (y/n)</question>', 'y')) {
if (!$this->askConfirmation('WARNING! Are you sure you wish to continue? (y/n)', $input, $output)) {
$output->writeln('<error>Maintenance cancelled!</error>');
return;
}
Expand All @@ -81,7 +80,6 @@ protected function interact(InputInterface $input, OutputInterface $output)
$driver = $this->getDriver();
$default = $driver->getOptions();

$dialog = $this->getHelperSet()->get('dialog');
$formatter = $this->getHelperSet()->get('formatter');

if (null !== $input->getArgument('ttl') && !is_numeric($input->getArgument('ttl'))) {
Expand All @@ -104,7 +102,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
'',
));

$ttl = $dialog->askAndValidate(
$ttl = $this->askAndValidate(
$input,
$output,
sprintf('<info>%s</info> [<comment>Default value in your configuration: %s</comment>]%s ', 'Set time', $driver->hasTtl() ? $driver->getTtl() : 'unlimited', ':'),
function($value) use ($default) {
Expand Down Expand Up @@ -140,4 +139,49 @@ private function getDriver()
{
return $this->getContainer()->get('lexik_maintenance.driver.factory')->getDriver();
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param $question
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed
*/
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askConfirmation($output, '<question>' . $question . '</question>', 'y');
}

return $this->getHelper('question')
->ask($input, $output, new \Symfony\Component\Console\Question\ConfirmationQuestion($question));
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param $question
* @param $validator
* @param bool $attempts
* @param null $default
* @return mixed
*/
protected function askAndValidate(InputInterface $input, OutputInterface $output, $question, $validator, $attempts = false, $default = null) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askAndValidate($output, $question, $validator, $attempts, $default);
}

$question = new \Symfony\Component\Console\Question\Question($question, $default);
$question->setValidator($validator);
$question->setMaxAttempts($attempts);

return $this->getHelper('question')
->ask($input, $output, $question);
}
}
28 changes: 23 additions & 5 deletions Command/DriverUnlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function confirmUnlock(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getHelperSet()->get('dialog');
$formatter = $this->getHelperSet()->get('formatter');

if ($input->getOption('no-interaction', false)) {
Expand All @@ -66,10 +65,10 @@ protected function confirmUnlock(InputInterface $input, OutputInterface $output)
'',
));

$confirmation = $dialog->askConfirmation(
$output,
'<question>WARNING! Are you sure you wish to continue? (y/n) </question>',
'y'
$confirmation = $this->askConfirmation(
'WARNING! Are you sure you wish to continue? (y/n) ',
$input,
$output
);
}

Expand All @@ -79,4 +78,23 @@ protected function confirmUnlock(InputInterface $input, OutputInterface $output)

return $confirmation;
}

/**
* This method ensure that we stay compatible with symfony console 2.3 by using the deprecated dialog helper
* but use the ConfirmationQuestion when available.
*
* @param $question
* @param InputInterface $input
* @param OutputInterface $output
* @return mixed
*/
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) {
if (!$this->getHelperSet()->has('question')) {
return $this->getHelper('dialog')
->askConfirmation($output, '<question>' . $question . '</question>', 'y');
}

return $this->getHelper('question')
->ask($input, $output, new \Symfony\Component\Console\Question\ConfirmationQuestion($question));
}
}

0 comments on commit 378ead2

Please sign in to comment.