Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / chi-teck / drupal-code-generator / src / Helper / InputHandler.php
index b1007e42b467007e991c6e2cded16bf14f448f24..51fb0ced2e7fb4aced7589f30f52641101baafef 100644 (file)
@@ -42,16 +42,17 @@ class InputHandler extends Helper {
    */
   public function collectVars(InputInterface $input, OutputInterface $output, array $questions, array $vars = []) {
 
-    // A user can pass answers through command line option.
+    // A user can pass answers through the command line option.
+    $answers = NULL;
     if ($answers_raw = $input->getOption('answers')) {
       $answers = json_decode($answers_raw, TRUE);
       if (!is_array($answers)) {
         throw new InvalidOptionException('Answers should be encoded in JSON format.');
       }
     }
-    else {
-      $answers = [];
-    }
+
+    /** @var \Symfony\Component\Console\Helper\QuestionHelper $question_helper */
+    $question_helper = $this->getHelperSet()->get('question');
 
     /** @var \DrupalCodeGenerator\Command\GeneratorInterface $command */
     $command = $this->getHelperSet()->getCommand();
@@ -85,27 +86,28 @@ class InputHandler extends Helper {
           $default_value = call_user_func($default_value, $vars);
         }
       }
-
       // Default value may have tokens.
       $default_value = Utils::tokenReplace($default_value, $vars);
-
       $this->setQuestionDefault($question, $default_value);
 
-      if (array_key_exists($name, $answers)) {
-        $answer = $answers[$name];
-        // Null stands for default value.
-        if ($answer === NULL) {
-          $answer = $default_value;
+      if ($answers) {
+        if (array_key_exists($name, $answers)) {
+          $answer = $answers[$name];
+          // Validate provided answer.
+          if ($validator = $question->getValidator()) {
+            $validator($answer);
+          }
+          // Turn 'yes/no' string into boolean.
+          if ($question instanceof ConfirmationQuestion && !is_bool($answer)) {
+            $answer = strcasecmp($answer, 'yes') == 0;
+          }
         }
-        // Turn 'yes/no' string into boolean.
-        elseif ($question instanceof ConfirmationQuestion && !is_bool($answer)) {
-          $answer = strcasecmp($answer, 'yes') == 0;
+        else {
+          $answer = $default_value;
         }
       }
       else {
         $this->formatQuestionText($question);
-        /** @var \Symfony\Component\Console\Helper\QuestionHelper $question_helper */
-        $question_helper = $this->getHelperSet()->get('question');
         $answer = $question_helper->ask($input, $output, $question);
       }