From 720496ccfa3baedb8e0435df442bc32de19b2bcc Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Fri, 13 Oct 2017 12:34:15 +0200 Subject: [PATCH 1/3] 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; + } } From 0e4dd85af64b5d9ed035da373e80d6d3fb08e1f8 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Fri, 13 Oct 2017 13:22:45 +0200 Subject: [PATCH 2/3] ADDED user and adminUser questions Code cleanup --- .../Setup/Console/Command/InstallCommand.php | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/InstallCommand.php b/setup/src/Magento/Setup/Console/Command/InstallCommand.php index 98423c26d39e2..467e94febef39 100644 --- a/setup/src/Magento/Setup/Console/Command/InstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InstallCommand.php @@ -215,13 +215,33 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou { $helper = $this->getHelper('question'); $configOptionsToValidate = []; + foreach ($this->configModel->getAvailableOptions() as $option) { + $configOptionsToValidate[$option->getName()] = $this->askQuestion( + $input, + $output, + $helper, + $option, + true + ); + } - $configOptionsToValidate[$option->getName()] = $this->askQuestion($input, $output, $helper, $option); + foreach ($this->userConfig->getOptionsList() 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); - */ + foreach ($this->adminUser->getOptionsList() as $option) { + $configOptionsToValidate[$option->getName()] = $this->askQuestion( + $input, + $output, + $helper, + $option + ); } return $configOptionsToValidate; } @@ -234,11 +254,17 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou * @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 + * @param TextConfigOption|FlagConfigOption\SelectConfigOption $option + * @param Boolean $validateInline * @return string[] Array of inputs */ - private function askQuestion(InputInterface $input, OutputInterface $output, QuestionHelper $helper, $option) - { + private function askQuestion( + InputInterface $input, + OutputInterface $output, + QuestionHelper $helper, + $option, + $validateInline = false + ) { if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') { if ($option->isValueRequired()) { $question = new ChoiceQuestion( @@ -253,10 +279,6 @@ private function askQuestion(InputInterface $input, OutputInterface $output, Que $option->getDefault() ); } - $question->setValidator(function ($answer) use ($option) { - $option->validate($option->getSelectOptions()[$answer]); - return $answer; - }); } else { if ($option->isValueRequired()) { $question = new Question( @@ -269,13 +291,25 @@ private function askQuestion(InputInterface $input, OutputInterface $output, Que $option->getDefault() ); } - $question->setValidator(function ($answer) use ($option) { - $option->validate($answer); - return $answer; - }); + } + $question->setValidator(function ($answer) use ($option, $validateInline) { + $answer = trim($answer); + + if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') { + $answer = $option->getSelectOptions()[$answer]; + } + + if ($validateInline) { + $option->validate($answer); + } + + return $answer; + }); + $value = $helper->ask($input, $output, $question); + return $value; } -} +} \ No newline at end of file From ce12a7439fe03a690a5d5acb76dde333118c3d80 Mon Sep 17 00:00:00 2001 From: Denis Ristic Date: Fri, 13 Oct 2017 14:02:56 +0200 Subject: [PATCH 3/3] ADDED re-run command output FIXED phpcs validation --- .../Setup/Console/Command/InstallCommand.php | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/InstallCommand.php b/setup/src/Magento/Setup/Console/Command/InstallCommand.php index 467e94febef39..a2e8715b02233 100644 --- a/setup/src/Magento/Setup/Console/Command/InstallCommand.php +++ b/setup/src/Magento/Setup/Console/Command/InstallCommand.php @@ -169,6 +169,14 @@ protected function initialize(InputInterface $input, OutputInterface $output) } } + if ($inputOptions['interactive']) { + $command = ''; + foreach ($configOptionsToValidate as $key => $value) { + $command .= " --{$key}={$value}"; + } + $output->writeln("Try re-running command: php bin/magento setup:install{$command}"); + } + $errors = $this->configModel->validate($configOptionsToValidate); $errors = array_merge($errors, $this->adminUser->validate($input)); $errors = array_merge($errors, $this->validate($input)); @@ -226,6 +234,8 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou ); } + $output->writeln(""); + foreach ($this->userConfig->getOptionsList() as $option) { $configOptionsToValidate[$option->getName()] = $this->askQuestion( $input, @@ -235,6 +245,8 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou ); } + $output->writeln(""); + foreach ($this->adminUser->getOptionsList() as $option) { $configOptionsToValidate[$option->getName()] = $this->askQuestion( $input, @@ -243,7 +255,17 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou $option ); } - return $configOptionsToValidate; + + $output->writeln(""); + + $returnConfigOptionsToValidate = []; + foreach ($configOptionsToValidate as $key => $value) { + if ($value != '') { + $returnConfigOptionsToValidate[$key] = $value; + } + } + + return $returnConfigOptionsToValidate; } /** @@ -265,7 +287,7 @@ private function askQuestion( $option, $validateInline = false ) { - if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') { + if ($option instanceof \Magento\Framework\Setup\Option\SelectConfigOption) { if ($option->isValueRequired()) { $question = new ChoiceQuestion( $option->getDescription() . '? ', @@ -291,16 +313,20 @@ private function askQuestion( $option->getDefault() ); } - } $question->setValidator(function ($answer) use ($option, $validateInline) { - $answer = trim($answer); - if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') { + if ($option instanceof \Magento\Framework\Setup\Option\SelectConfigOption) { $answer = $option->getSelectOptions()[$answer]; } + if ($answer == null) { + $answer = ''; + } else { + $answer = trim($answer); + } + if ($validateInline) { $option->validate($answer); } @@ -312,4 +338,4 @@ private function askQuestion( return $value; } -} \ No newline at end of file +}