Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console-core / src / EventSubscriber / DefaultValueEventListener.php
index f948962b9bdb12815c768fcd01dbdaf5d7289211..25d427396f4f64f0b458d1b7cb95152fe92711c1 100644 (file)
@@ -9,6 +9,7 @@ namespace Drupal\Console\Core\EventSubscriber;
 
 use Symfony\Component\Console\ConsoleEvents;
 use Symfony\Component\Console\Event\ConsoleCommandEvent;
+use Symfony\Component\Console\Input\ArgvInput;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 use Symfony\Component\Console\Command\Command;
 use Drupal\Console\Core\Utils\ConfigurationManager;
@@ -60,34 +61,81 @@ class DefaultValueEventListener implements EventSubscriberInterface
             return;
         }
 
-        $input = $command->getDefinition();
-        $options = $input->getOptions();
-        foreach ($options as $key => $option) {
-            $defaultOption = sprintf(
-                'application.default.commands.%s.options.%s',
-                str_replace(':', '.', $command->getName()),
-                $key
-            );
-            $defaultValue = $configuration->get($defaultOption);
-            if ($defaultValue) {
-                $option->setDefault($defaultValue);
+        $inputDefinition = $command->getDefinition();
+        $input = $event->getInput();
+        $commandConfigKey = sprintf(
+            'application.commands.defaults.%s',
+            str_replace(':', '.', $command->getName())
+        );
+        $defaults = $configuration->get($commandConfigKey);
+
+        $this->setOptions($defaults, $input, $inputDefinition);
+        $this->setArguments($defaults, $input, $inputDefinition);
+    }
+
+    private function setOptions($defaults, $input, $inputDefinition)
+    {
+        $defaultOptions = $this->extractKey($defaults, 'options');
+        $defaultValues = [];
+        if ($defaultOptions) {
+            $reflection = new \ReflectionObject($input);
+            $prop = $reflection->getProperty('tokens');
+            $prop->setAccessible(true);
+            $tokens = $prop->getValue($input);
+            foreach ($defaultOptions as $key => $defaultValue) {
+                $option = $inputDefinition->getOption($key);
+                if ($input->getOption($key)) {
+                    continue;
+                }
+                if ($option->acceptValue()) {
+                    $defaultValues[] = sprintf(
+                        '--%s=%s',
+                        $key,
+                        $defaultValue
+                    );
+                    continue;
+                }
+                $defaultValues[] = sprintf(
+                    '--%s',
+                    $key
+                );
             }
+            $prop->setValue(
+                $input,
+                array_unique(array_merge($tokens, $defaultValues))
+            );
         }
+    }
 
-        $arguments = $input->getArguments();
-        foreach ($arguments as $key => $argument) {
-            $defaultArgument = sprintf(
-                'application.default.commands.%s.arguments.%s',
-                str_replace(':', '.', $command->getName()),
-                $key
-            );
-            $defaultValue = $configuration->get($defaultArgument);
-            if ($defaultValue) {
+    private function setArguments($defaults, $input, $inputDefinition)
+    {
+        $defaultArguments = $this->extractKey($defaults, 'arguments');
+
+        foreach ($defaultArguments as $key => $defaultValue) {
+            if ($input->getArgument($key)) {
+                continue;
+            }
+
+            if ($argument = $inputDefinition->getArgument($key)) {
                 $argument->setDefault($defaultValue);
             }
         }
     }
 
+    private function extractKey($defaults, $key)
+    {
+        if (!$defaults || !is_array($defaults)) {
+            return [];
+        }
+
+        $defaults = array_key_exists($key, $defaults)?$defaults[$key]:[];
+        if (!is_array($defaults)) {
+            return [];
+        }
+
+        return $defaults;
+    }
+
     /**
      * @{@inheritdoc}
      */