From 720496ccfa3baedb8e0435df442bc32de19b2bcc Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Fri, 13 Oct 2017 12:34:15 +0200 Subject: [PATCH] Magento setup:install interactive shell --- .../Setup/Console/Command/InstallCommand.php | 111 +++++++++++++++++- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/InstallCommand.php b/setup/src/Magento/Setup/Console/Command/InstallCommand.php index b0ccbba8c296f..98423c26d39e2 100644 --- a/setup/src/Magento/Setup/Console/Command/InstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InstallCommand.php @@ -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 @@ -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. */ @@ -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') @@ -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)); @@ -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; + } }