Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / FormAlterCommand.php
index 71c5d8cfd3716439576826c8febc6a802330d3f1..37073da06bb1d254f77929decd8233554677ee2c 100644 (file)
@@ -7,18 +7,16 @@
 
 namespace Drupal\Console\Command\Generate;
 
+use Drupal\Console\Command\Shared\ArrayInputTrait;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Drupal\Console\Generator\FormAlterGenerator;
 use Drupal\Console\Command\Shared\ServicesTrait;
 use Drupal\Console\Command\Shared\ModuleTrait;
-use Drupal\Console\Command\Shared\MenuTrait;
 use Drupal\Console\Command\Shared\FormTrait;
 use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Console\Core\Utils\StringConverter;
 use Drupal\Console\Extension\Manager;
 use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -30,21 +28,20 @@ use Drupal\webprofiler\Profiler\Profiler;
 
 class FormAlterCommand extends Command
 {
-    use ServicesTrait;
-    use ModuleTrait;
-    use FormTrait;
-    use MenuTrait;
     use ConfirmationTrait;
-    use CommandTrait;
+    use ArrayInputTrait;
+    use FormTrait;
+    use ModuleTrait;
+    use ServicesTrait;
 
     /**
- * @var Manager
-*/
    * @var Manager
+     */
     protected $extensionManager;
 
     /**
- * @var FormAlterGenerator
-*/
    * @var FormAlterGenerator
+     */
     protected $generator;
 
     /**
@@ -63,13 +60,13 @@ class FormAlterCommand extends Command
     protected $elementInfoManager;
 
     /**
- * @var Validator
-*/
    * @var Validator
+     */
     protected $validator;
 
     /**
- * @var RouteProviderInterface
-*/
    * @var RouteProviderInterface
+     */
     protected $routeProvider;
 
     /**
@@ -108,7 +105,8 @@ class FormAlterCommand extends Command
         ElementInfoManager $elementInfoManager,
         Profiler $profiler = null,
         $appRoot,
-        ChainQueue $chainQueue
+        ChainQueue $chainQueue,
+        Validator $validator
     ) {
         $this->extensionManager = $extensionManager;
         $this->generator = $generator;
@@ -118,6 +116,7 @@ class FormAlterCommand extends Command
         $this->profiler = $profiler;
         $this->appRoot = $appRoot;
         $this->chainQueue = $chainQueue;
+        $this->validator = $validator;
         parent::__construct();
     }
 
@@ -132,9 +131,7 @@ class FormAlterCommand extends Command
     {
         $this
             ->setName('generate:form:alter')
-            ->setDescription(
-                $this->trans('commands.generate.form.alter.description')
-            )
+            ->setDescription($this->trans('commands.generate.form.alter.description'))
             ->setHelp($this->trans('commands.generate.form.alter.help'))
             ->addOption(
                 'module',
@@ -153,7 +150,8 @@ class FormAlterCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
                 $this->trans('commands.common.options.inputs')
-            );
+            )
+            ->setAliases(['gfa']);
     }
 
     /**
@@ -161,18 +159,21 @@ class FormAlterCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
-        // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
-        if (!$this->confirmGeneration($io)) {
+        // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+        if (!$this->confirmOperation()) {
             return 1;
         }
 
         $module = $input->getOption('module');
         $formId = $input->getOption('form-id');
         $inputs = $input->getOption('inputs');
+        $noInteraction = $input->getOption('no-interaction');
+        // Parse nested data.
+        if ($noInteraction) {
+          $inputs = $this->explodeInlineArray($inputs);
+        }
 
-        $function = $module . '_form_' .$formId . '_alter';
+        $function = $module . '_form_' . $formId . '_alter';
 
         if ($this->extensionManager->validateModuleFunctionExist($module, $function)) {
             throw new \Exception(
@@ -183,14 +184,12 @@ class FormAlterCommand extends Command
             );
         }
 
-        //validate if input is an array
-        if (!is_array($inputs[0])) {
-            $inputs= $this->explodeInlineArray($inputs);
-        }
-
-        $this
-            ->generator
-            ->generate($module, $formId, $inputs, $this->metadata);
+        $this->generator->generate([
+            'module' => $module,
+            'form_id' => $formId,
+            'inputs' => $inputs,
+            'metadata' => $this->metadata,
+        ]);
 
         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
 
@@ -199,16 +198,8 @@ class FormAlterCommand extends Command
 
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         // --module option
-        $module = $input->getOption('module');
-        if (!$module) {
-            // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
-            $module = $this->moduleQuestion($io);
-        }
-
-        $input->setOption('module', $module);
+        $this->getModuleOption();
 
         // --form-id option
         $formId = $input->getOption('form-id');
@@ -216,15 +207,15 @@ class FormAlterCommand extends Command
             $forms = [];
             // Get form ids from webprofiler
             if ($this->moduleHandler->moduleExists('webprofiler')) {
-                $io->info(
+                $this->getIo()->info(
                     $this->trans('commands.generate.form.alter.messages.loading-forms')
                 );
                 $forms = $this->getWebprofilerForms();
             }
 
             if (!empty($forms)) {
-                $formId = $io->choiceNoList(
-                    $this->trans('commands.generate.form.alter.options.form-id'),
+                $formId = $this->getIo()->choiceNoList(
+                    $this->trans('commands.generate.form.alter.questions.form-id'),
                     array_keys($forms)
                 );
             }
@@ -252,7 +243,7 @@ class FormAlterCommand extends Command
 
             $formItems = array_keys($forms[$formId]['form']);
 
-            $formItemsToHide = $io->choice(
+            $formItemsToHide = $this->getIo()->choice(
                 $this->trans('commands.generate.form.alter.messages.hide-form-elements'),
                 $formItems,
                 null,
@@ -268,8 +259,8 @@ class FormAlterCommand extends Command
         $inputs = $input->getOption('inputs');
 
         if (empty($inputs)) {
-            $io->writeln($this->trans('commands.generate.form.alter.messages.inputs'));
-            $inputs = $this->formQuestion($io);
+            $this->getIo()->writeln($this->trans('commands.generate.form.alter.messages.inputs'));
+            $inputs = $this->formQuestion();
         } else {
             $inputs= $this->explodeInlineArray($inputs);
         }
@@ -277,32 +268,6 @@ class FormAlterCommand extends Command
         $input->setOption('inputs', $inputs);
     }
 
-    /**
-     * @{@inheritdoc}
-     */
-    public function explodeInlineArray($inlineInputs)
-    {
-        $inputs = [];
-        foreach ($inlineInputs as $inlineInput) {
-            $explodeInput = explode(" ", $inlineInput);
-            $parameters = [];
-            foreach ($explodeInput as $inlineParameter) {
-                list($key, $value) = explode(":", $inlineParameter);
-                if (!empty($value)) {
-                    $parameters[$key] = $value;
-                }
-            }
-            $inputs[] = $parameters;
-        }
-
-        return $inputs;
-    }
-
-    protected function createGenerator()
-    {
-        return new FormAlterGenerator();
-    }
-
     public function getWebprofilerForms()
     {
         $tokens = $this->profiler->find(null, null, 1000, null, '', '');