Skip to content

Commit ad775a8

Browse files
MAGETWO-82754: Magento setup:install interactive shell #11425
2 parents 266e13f + e7c3334 commit ad775a8

File tree

1 file changed

+166
-5
lines changed

1 file changed

+166
-5
lines changed

setup/src/Magento/Setup/Console/Command/InstallCommand.php

+166-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Framework\Setup\ConsoleLogger;
1414
use Symfony\Component\Console\Input\InputOption;
1515
use Magento\Setup\Model\ConfigModel;
16+
use Symfony\Component\Console\Question\Question;
17+
use Symfony\Component\Console\Question\ChoiceQuestion;
18+
use Symfony\Component\Console\Helper\QuestionHelper;
1619

1720
/**
1821
* Command to install Magento application
@@ -35,6 +38,16 @@ class InstallCommand extends AbstractSetupCommand
3538
*/
3639
const INPUT_KEY_USE_SAMPLE_DATA = 'use-sample-data';
3740

41+
/**
42+
* Parameter indicating command for interactive setup
43+
*/
44+
const INPUT_KEY_INTERACTIVE_SETUP = 'interactive';
45+
46+
/**
47+
* Parameter indicating command shortcut for interactive setup
48+
*/
49+
const INPUT_KEY_INTERACTIVE_SETUP_SHORTCUT = 'i';
50+
3851
/**
3952
* Regex for sales_order_increment_prefix validation.
4053
*/
@@ -109,7 +122,13 @@ protected function configure()
109122
null,
110123
InputOption::VALUE_NONE,
111124
'Use sample data'
112-
)
125+
),
126+
new InputOption(
127+
self::INPUT_KEY_INTERACTIVE_SETUP,
128+
self::INPUT_KEY_INTERACTIVE_SETUP_SHORTCUT,
129+
InputOption::VALUE_NONE,
130+
'Interactive Magento instalation'
131+
),
113132
]);
114133
$this->setName('setup:install')
115134
->setDescription('Installs the Magento application')
@@ -139,12 +158,25 @@ protected function initialize(InputInterface $input, OutputInterface $output)
139158
{
140159
$inputOptions = $input->getOptions();
141160

142-
$configOptionsToValidate = [];
143-
foreach ($this->configModel->getAvailableOptions() as $option) {
144-
if (array_key_exists($option->getName(), $inputOptions)) {
145-
$configOptionsToValidate[$option->getName()] = $inputOptions[$option->getName()];
161+
if ($inputOptions['interactive']) {
162+
$configOptionsToValidate = $this->interactiveQuestions($input, $output);
163+
} else {
164+
$configOptionsToValidate = [];
165+
foreach ($this->configModel->getAvailableOptions() as $option) {
166+
if (array_key_exists($option->getName(), $inputOptions)) {
167+
$configOptionsToValidate[$option->getName()] = $inputOptions[$option->getName()];
168+
}
169+
}
170+
}
171+
172+
if ($inputOptions['interactive']) {
173+
$command = '';
174+
foreach ($configOptionsToValidate as $key => $value) {
175+
$command .= " --{$key}={$value}";
146176
}
177+
$output->writeln("<comment>Try re-running command: php bin/magento setup:install{$command}</comment>");
147178
}
179+
148180
$errors = $this->configModel->validate($configOptionsToValidate);
149181
$errors = array_merge($errors, $this->adminUser->validate($input));
150182
$errors = array_merge($errors, $this->validate($input));
@@ -177,4 +209,133 @@ public function validate(InputInterface $input)
177209
}
178210
return $errors;
179211
}
212+
213+
/**
214+
* Runs interactive questions
215+
*
216+
* It will ask users for interactive questionst regarding setup configuration.
217+
*
218+
* @param InputInterface $input
219+
* @param OutputInterface $output
220+
* @return string[] Array of inputs
221+
*/
222+
private function interactiveQuestions(InputInterface $input, OutputInterface $output)
223+
{
224+
$helper = $this->getHelper('question');
225+
$configOptionsToValidate = [];
226+
227+
foreach ($this->configModel->getAvailableOptions() as $option) {
228+
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
229+
$input,
230+
$output,
231+
$helper,
232+
$option,
233+
true
234+
);
235+
}
236+
237+
$output->writeln("");
238+
239+
foreach ($this->userConfig->getOptionsList() as $option) {
240+
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
241+
$input,
242+
$output,
243+
$helper,
244+
$option
245+
);
246+
}
247+
248+
$output->writeln("");
249+
250+
foreach ($this->adminUser->getOptionsList() as $option) {
251+
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
252+
$input,
253+
$output,
254+
$helper,
255+
$option
256+
);
257+
}
258+
259+
$output->writeln("");
260+
261+
$returnConfigOptionsToValidate = [];
262+
foreach ($configOptionsToValidate as $key => $value) {
263+
if ($value != '') {
264+
$returnConfigOptionsToValidate[$key] = $value;
265+
}
266+
}
267+
268+
return $returnConfigOptionsToValidate;
269+
}
270+
271+
/**
272+
* Runs interactive questions
273+
*
274+
* It will ask users for interactive questionst regarding setup configuration.
275+
*
276+
* @param InputInterface $input
277+
* @param OutputInterface $output
278+
* @param QuestionHelper $helper
279+
* @param TextConfigOption|FlagConfigOption\SelectConfigOption $option
280+
* @param Boolean $validateInline
281+
* @return string[] Array of inputs
282+
*/
283+
private function askQuestion(
284+
InputInterface $input,
285+
OutputInterface $output,
286+
QuestionHelper $helper,
287+
$option,
288+
$validateInline = false
289+
) {
290+
if ($option instanceof \Magento\Framework\Setup\Option\SelectConfigOption) {
291+
if ($option->isValueRequired()) {
292+
$question = new ChoiceQuestion(
293+
$option->getDescription() . '? ',
294+
$option->getSelectOptions(),
295+
$option->getDefault()
296+
);
297+
} else {
298+
$question = new ChoiceQuestion(
299+
$option->getDescription() . ' [optional]? ',
300+
$option->getSelectOptions(),
301+
$option->getDefault()
302+
);
303+
}
304+
} else {
305+
if ($option->isValueRequired()) {
306+
$question = new Question(
307+
$option->getDescription() . '? ',
308+
$option->getDefault()
309+
);
310+
} else {
311+
$question = new Question(
312+
$option->getDescription() . ' [optional]? ',
313+
$option->getDefault()
314+
);
315+
}
316+
}
317+
318+
$question->setValidator(function ($answer) use ($option, $validateInline) {
319+
320+
if ($option instanceof \Magento\Framework\Setup\Option\SelectConfigOption) {
321+
$answer = $option->getSelectOptions()[$answer];
322+
}
323+
324+
if ($answer == null) {
325+
$answer = '';
326+
} else {
327+
$answer = trim($answer);
328+
}
329+
330+
if ($validateInline) {
331+
$option->validate($answer);
332+
}
333+
334+
return $answer;
335+
});
336+
337+
$value = $helper->ask($input, $output, $question);
338+
339+
return $value;
340+
}
180341
}

0 commit comments

Comments
 (0)