Skip to content

Commit 720496c

Browse files
author
Denis Ristic
committed
Magento setup:install interactive shell
1 parent 08ec8ce commit 720496c

File tree

1 file changed

+106
-5
lines changed

1 file changed

+106
-5
lines changed

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

Lines changed: 106 additions & 5 deletions
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,17 @@ 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+
}
146169
}
147170
}
171+
148172
$errors = $this->configModel->validate($configOptionsToValidate);
149173
$errors = array_merge($errors, $this->adminUser->validate($input));
150174
$errors = array_merge($errors, $this->validate($input));
@@ -177,4 +201,81 @@ public function validate(InputInterface $input)
177201
}
178202
return $errors;
179203
}
204+
205+
/**
206+
* Runs interactive questions
207+
*
208+
* It will ask users for interactive questionst regarding setup configuration.
209+
*
210+
* @param InputInterface $input
211+
* @param OutputInterface $output
212+
* @return string[] Array of inputs
213+
*/
214+
private function interactiveQuestions(InputInterface $input, OutputInterface $output)
215+
{
216+
$helper = $this->getHelper('question');
217+
$configOptionsToValidate = [];
218+
foreach ($this->configModel->getAvailableOptions() as $option) {
219+
220+
$configOptionsToValidate[$option->getName()] = $this->askQuestion($input, $output, $helper, $option);
221+
222+
/*$question = new Question($option->getDescription() . '? ', $option->getDefault());
223+
$configOptionsToValidate[$option->getName()] = $helper->ask($input, $output, $question);
224+
*/
225+
}
226+
return $configOptionsToValidate;
227+
}
228+
229+
/**
230+
* Runs interactive questions
231+
*
232+
* It will ask users for interactive questionst regarding setup configuration.
233+
*
234+
* @param InputInterface $input
235+
* @param OutputInterface $output
236+
* @param QuestionHelper $helper
237+
* @param Magento\Framework\Setup\Option\TextConfigOption|Magento\Framework\Setup\Option\FlagConfigOption\Magento\Framework\Setup\Option\SelectConfigOption $option
238+
* @return string[] Array of inputs
239+
*/
240+
private function askQuestion(InputInterface $input, OutputInterface $output, QuestionHelper $helper, $option)
241+
{
242+
if (get_class($option) === 'Magento\Framework\Setup\Option\SelectConfigOption') {
243+
if ($option->isValueRequired()) {
244+
$question = new ChoiceQuestion(
245+
$option->getDescription() . '? ',
246+
$option->getSelectOptions(),
247+
$option->getDefault()
248+
);
249+
} else {
250+
$question = new ChoiceQuestion(
251+
$option->getDescription() . ' [optional]? ',
252+
$option->getSelectOptions(),
253+
$option->getDefault()
254+
);
255+
}
256+
$question->setValidator(function ($answer) use ($option) {
257+
$option->validate($option->getSelectOptions()[$answer]);
258+
return $answer;
259+
});
260+
} else {
261+
if ($option->isValueRequired()) {
262+
$question = new Question(
263+
$option->getDescription() . '? ',
264+
$option->getDefault()
265+
);
266+
} else {
267+
$question = new Question(
268+
$option->getDescription() . ' [optional]? ',
269+
$option->getDefault()
270+
);
271+
}
272+
$question->setValidator(function ($answer) use ($option) {
273+
$option->validate($answer);
274+
return $answer;
275+
});
276+
}
277+
278+
$value = $helper->ask($input, $output, $question);
279+
return $value;
280+
}
180281
}

0 commit comments

Comments
 (0)