13
13
use Magento \Framework \Setup \ConsoleLogger ;
14
14
use Symfony \Component \Console \Input \InputOption ;
15
15
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 ;
16
19
17
20
/**
18
21
* Command to install Magento application
@@ -35,6 +38,16 @@ class InstallCommand extends AbstractSetupCommand
35
38
*/
36
39
const INPUT_KEY_USE_SAMPLE_DATA = 'use-sample-data ' ;
37
40
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
+
38
51
/**
39
52
* Regex for sales_order_increment_prefix validation.
40
53
*/
@@ -109,7 +122,13 @@ protected function configure()
109
122
null ,
110
123
InputOption::VALUE_NONE ,
111
124
'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
+ ),
113
132
]);
114
133
$ this ->setName ('setup:install ' )
115
134
->setDescription ('Installs the Magento application ' )
@@ -139,12 +158,17 @@ protected function initialize(InputInterface $input, OutputInterface $output)
139
158
{
140
159
$ inputOptions = $ input ->getOptions ();
141
160
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
+ }
146
169
}
147
170
}
171
+
148
172
$ errors = $ this ->configModel ->validate ($ configOptionsToValidate );
149
173
$ errors = array_merge ($ errors , $ this ->adminUser ->validate ($ input ));
150
174
$ errors = array_merge ($ errors , $ this ->validate ($ input ));
@@ -177,4 +201,81 @@ public function validate(InputInterface $input)
177
201
}
178
202
return $ errors ;
179
203
}
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
+ }
180
281
}
0 commit comments