Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magento setup:install interactive shell #11425

Merged
merged 3 commits into from
Oct 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}