Skip to content

Commit

Permalink
Magento setup:install interactive shell
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Ristic committed Oct 13, 2017
1 parent 08ec8ce commit 720496c
Showing 1 changed file with 106 additions and 5 deletions.
111 changes: 106 additions & 5 deletions setup/src/Magento/Setup/Console/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Magento\Framework\Setup\ConsoleLogger;
use Symfony\Component\Console\Input\InputOption;
use Magento\Setup\Model\ConfigModel;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Helper\QuestionHelper;

/**
* Command to install Magento application
Expand All @@ -35,6 +38,16 @@ class InstallCommand extends AbstractSetupCommand
*/
const INPUT_KEY_USE_SAMPLE_DATA = 'use-sample-data';

/**
* Parameter indicating command for interactive setup
*/
const INPUT_KEY_INTERACTIVE_SETUP = 'interactive';

/**
* Parameter indicating command shortcut for interactive setup
*/
const INPUT_KEY_INTERACTIVE_SETUP_SHORTCUT = 'i';

/**
* Regex for sales_order_increment_prefix validation.
*/
Expand Down Expand Up @@ -109,7 +122,13 @@ protected function configure()
null,
InputOption::VALUE_NONE,
'Use sample data'
)
),
new InputOption(
self::INPUT_KEY_INTERACTIVE_SETUP,
self::INPUT_KEY_INTERACTIVE_SETUP_SHORTCUT,
InputOption::VALUE_NONE,
'Interactive Magento instalation'
),
]);
$this->setName('setup:install')
->setDescription('Installs the Magento application')
Expand Down Expand Up @@ -139,12 +158,17 @@ protected function initialize(InputInterface $input, OutputInterface $output)
{
$inputOptions = $input->getOptions();

$configOptionsToValidate = [];
foreach ($this->configModel->getAvailableOptions() as $option) {
if (array_key_exists($option->getName(), $inputOptions)) {
$configOptionsToValidate[$option->getName()] = $inputOptions[$option->getName()];
if ($inputOptions['interactive']) {
$configOptionsToValidate = $this->interactiveQuestions($input, $output);
} else {
$configOptionsToValidate = [];
foreach ($this->configModel->getAvailableOptions() as $option) {
if (array_key_exists($option->getName(), $inputOptions)) {
$configOptionsToValidate[$option->getName()] = $inputOptions[$option->getName()];
}
}
}

$errors = $this->configModel->validate($configOptionsToValidate);
$errors = array_merge($errors, $this->adminUser->validate($input));
$errors = array_merge($errors, $this->validate($input));
Expand Down Expand Up @@ -177,4 +201,81 @@ public function validate(InputInterface $input)
}
return $errors;
}

/**
* Runs interactive questions
*
* It will ask users for interactive questionst regarding setup configuration.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return string[] Array of inputs
*/
private function interactiveQuestions(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$configOptionsToValidate = [];
foreach ($this->configModel->getAvailableOptions() as $option) {

$configOptionsToValidate[$option->getName()] = $this->askQuestion($input, $output, $helper, $option);

/*$question = new Question($option->getDescription() . '? ', $option->getDefault());
$configOptionsToValidate[$option->getName()] = $helper->ask($input, $output, $question);
*/
}
return $configOptionsToValidate;
}

/**
* Runs interactive questions
*
* It will ask users for interactive questionst regarding setup configuration.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param QuestionHelper $helper
* @param Magento\Framework\Setup\Option\TextConfigOption|Magento\Framework\Setup\Option\FlagConfigOption\Magento\Framework\Setup\Option\SelectConfigOption $option
* @return string[] Array of inputs
*/
private function askQuestion(InputInterface $input, OutputInterface $output, QuestionHelper $helper, $option)
{
if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') {
if ($option->isValueRequired()) {
$question = new ChoiceQuestion(
$option->getDescription() . '? ',
$option->getSelectOptions(),
$option->getDefault()
);
} else {
$question = new ChoiceQuestion(
$option->getDescription() . ' [optional]? ',
$option->getSelectOptions(),
$option->getDefault()
);
}
$question->setValidator(function ($answer) use ($option) {
$option->validate($option->getSelectOptions()[$answer]);
return $answer;
});
} else {
if ($option->isValueRequired()) {
$question = new Question(
$option->getDescription() . '? ',
$option->getDefault()
);
} else {
$question = new Question(
$option->getDescription() . ' [optional]? ',
$option->getDefault()
);
}
$question->setValidator(function ($answer) use ($option) {
$option->validate($answer);
return $answer;
});
}

$value = $helper->ask($input, $output, $question);
return $value;
}
}

0 comments on commit 720496c

Please sign in to comment.