Skip to content

Commit

Permalink
Merge forwardport of #11425 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/11425.patch (created by @denisristic) based on commit(s):
  1. 720496c
  2. 0e4dd85
  3. ce12a74
  • Loading branch information
magento-engcom-team authored Jan 23, 2018
2 parents 4c927a0 + 6b0c02c commit 060a8f9
Showing 1 changed file with 166 additions and 5 deletions.
171 changes: 166 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,25 @@ 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()];
}
}
}

if ($inputOptions['interactive']) {
$command = '';
foreach ($configOptionsToValidate as $key => $value) {
$command .= " --{$key}={$value}";
}
$output->writeln("<comment>Try re-running command: php bin/magento setup:install{$command}</comment>");
}

$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 +209,133 @@ 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,
true
);
}

$output->writeln("");

foreach ($this->userConfig->getOptionsList() as $option) {
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
$input,
$output,
$helper,
$option
);
}

$output->writeln("");

foreach ($this->adminUser->getOptionsList() as $option) {
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
$input,
$output,
$helper,
$option
);
}

$output->writeln("");

$returnConfigOptionsToValidate = [];
foreach ($configOptionsToValidate as $key => $value) {
if ($value != '') {
$returnConfigOptionsToValidate[$key] = $value;
}
}

return $returnConfigOptionsToValidate;
}

/**
* Runs interactive questions
*
* It will ask users for interactive questionst regarding setup configuration.
*
* @param InputInterface $input
* @param OutputInterface $output
* @param QuestionHelper $helper
* @param TextConfigOption|FlagConfigOption\SelectConfigOption $option
* @param Boolean $validateInline
* @return string[] Array of inputs
*/
private function askQuestion(
InputInterface $input,
OutputInterface $output,
QuestionHelper $helper,
$option,
$validateInline = false
) {
if ($option instanceof \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()
);
}
} 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, $validateInline) {

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);
}

return $answer;
});

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

return $value;
}
}

0 comments on commit 060a8f9

Please sign in to comment.