Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / FormCommand.php
index 58a2582a18aed66bbd81e43bd47a2f436ea96f70..56e71b9e829694a9fca2dd44da97afbeaab9abf6 100644 (file)
@@ -7,6 +7,9 @@
 
 namespace Drupal\Console\Command\Generate;
 
+use Drupal\Console\Command\Shared\ArrayInputTrait;
+use Drupal\Console\Utils\TranslatorManager;
+use Drupal\Console\Utils\Validator;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -14,9 +17,7 @@ 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 Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
+use Drupal\Console\Core\Command\ContainerAwareCommand;
 use Drupal\Console\Generator\FormGenerator;
 use Drupal\Console\Extension\Manager;
 use Drupal\Console\Core\Utils\ChainQueue;
@@ -24,30 +25,30 @@ use Drupal\Console\Core\Utils\StringConverter;
 use Drupal\Core\Render\ElementInfoManager;
 use Drupal\Core\Routing\RouteProviderInterface;
 
-abstract class FormCommand extends Command
+abstract class FormCommand extends ContainerAwareCommand
 {
-    use ContainerAwareCommandTrait;
-    use ModuleTrait;
-    use ServicesTrait;
+    use ArrayInputTrait;
     use FormTrait;
     use MenuTrait;
+    use ModuleTrait;
+    use ServicesTrait;
 
     private $formType;
     private $commandName;
 
     /**
- * @var Manager
-*/
    * @var Manager
+     */
     protected $extensionManager;
 
     /**
- * @var FormGenerator
-*/
    * @var FormGenerator
+     */
     protected $generator;
 
     /**
- * @var ChainQueue
-*/
    * @var ChainQueue
+     */
     protected $chainQueue;
 
     /**
@@ -55,6 +56,11 @@ abstract class FormCommand extends Command
      */
     protected $stringConverter;
 
+    /**
+     * @var Validator
+     */
+    protected $validator;
+
     /**
      * @var ElementInfoManager
      */
@@ -69,25 +75,31 @@ abstract class FormCommand extends Command
     /**
      * FormCommand constructor.
      *
+     * @param TranslatorManager      $translator
      * @param Manager                $extensionManager
      * @param FormGenerator          $generator
      * @param ChainQueue             $chainQueue
      * @param StringConverter        $stringConverter
+     * @param Validator              $validator
      * @param ElementInfoManager     $elementInfoManager
      * @param RouteProviderInterface $routeProvider
      */
     public function __construct(
+        TranslatorManager $translator,
         Manager $extensionManager,
         FormGenerator $generator,
         ChainQueue $chainQueue,
         StringConverter $stringConverter,
+        Validator $validator,
         ElementInfoManager $elementInfoManager,
         RouteProviderInterface $routeProvider
     ) {
+        $this->setTranslator($translator);
         $this->extensionManager = $extensionManager;
         $this->generator = $generator;
         $this->chainQueue = $chainQueue;
         $this->stringConverter = $stringConverter;
+        $this->validator = $validator;
         $this->elementInfoManager = $elementInfoManager;
         $this->routeProvider = $routeProvider;
         parent::__construct();
@@ -153,7 +165,7 @@ abstract class FormCommand extends Command
             ->addOption(
                 'inputs',
                 null,
-                InputOption::VALUE_OPTIONAL,
+                InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
                 $this->trans('commands.common.options.inputs')
             )
             ->addOption(
@@ -163,28 +175,28 @@ abstract class FormCommand extends Command
                 $this->trans('commands.generate.form.options.path')
             )
             ->addOption(
-                'menu_link_gen',
+                'menu-link-gen',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.form.options.menu_link_gen')
+                $this->trans('commands.generate.form.options.menu-link-gen')
             )
             ->addOption(
-                'menu_link_title',
+                'menu-link-title',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.form.options.menu_link_title')
+                $this->trans('commands.generate.form.options.menu-link-title')
             )
             ->addOption(
-                'menu_parent',
+                'menu-parent',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.form.options.menu_parent')
+                $this->trans('commands.generate.form.options.menu-parent')
             )
             ->addOption(
-                'menu_link_desc',
+                'menu-link-desc',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.form.options.menu_link_desc')
+                $this->trans('commands.generate.form.options.menu-link-desc')
             );
     }
 
@@ -197,21 +209,38 @@ abstract class FormCommand extends Command
         $services = $input->getOption('services');
         $path = $input->getOption('path');
         $config_file = $input->getOption('config-file');
-        $class_name = $input->getOption('class');
+        $class_name = $this->validator->validateClassName($input->getOption('class'));
         $form_id = $input->getOption('form-id');
         $form_type = $this->formType;
-        $menu_link_gen = $input->getOption('menu_link_gen');
-        $menu_parent = $input->getOption('menu_parent');
-        $menu_link_title = $input->getOption('menu_link_title');
-        $menu_link_desc = $input->getOption('menu_link_desc');
+        $menu_link_gen = $input->getOption('menu-link-gen');
+        $menu_parent = $input->getOption('menu-parent');
+        $menu_link_title = $input->getOption('menu-link-title');
+        $menu_link_desc = $input->getOption('menu-link-desc');
+        $inputs = $input->getOption('inputs');
+        $noInteraction = $input->getOption('no-interaction');
+        // Parse nested data.
+        if ($noInteraction) {
+            $inputs = $this->explodeInlineArray($inputs);
+        }
 
         // if exist form generate config file
-        $inputs = $input->getOption('inputs');
         $build_services = $this->buildServices($services);
 
-        $this
-            ->generator
-            ->generate($module, $class_name, $form_id, $form_type, $build_services, $config_file, $inputs, $path, $menu_link_gen, $menu_link_title, $menu_parent, $menu_link_desc);
+        $this->generator->generate([
+          'class_name' => $class_name,
+          'services' => $build_services,
+          'config_file' => $config_file,
+          'inputs' => $inputs,
+          'module_name' => $module,
+          'form_id' => $form_id,
+          'form_type' => $form_type,
+          'path' => $path,
+          'route_name' => $class_name,
+          'menu_link_title' => $menu_link_title,
+          'menu_parent' => $menu_parent,
+          'menu_link_desc' => $menu_link_desc,
+          'menu_link_gen' => $menu_link_gen,
+        ]);
 
         $this->chainQueue->addCommand('router:rebuild', []);
     }
@@ -221,22 +250,18 @@ abstract class FormCommand 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);
-        }
+        $module = $this->getModuleOption();
 
         // --class option
         $className = $input->getOption('class');
         if (!$className) {
-            $className = $io->ask(
+            $className = $this->getIo()->ask(
                 $this->trans('commands.generate.form.questions.class'),
-                'DefaultForm'
+                'DefaultForm',
+                function ($className) {
+                    return $this->validator->validateClassName($className);
+                }
             );
             $input->setOption('class', $className);
         }
@@ -244,7 +269,7 @@ abstract class FormCommand extends Command
         // --form-id option
         $formId = $input->getOption('form-id');
         if (!$formId) {
-            $formId = $io->ask(
+            $formId = $this->getIo()->ask(
                 $this->trans('commands.generate.form.questions.form-id'),
                 $this->stringConverter->camelCaseToMachineName($className)
             );
@@ -253,14 +278,14 @@ abstract class FormCommand extends Command
 
         // --services option
         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
-        $services = $this->servicesQuestion($io);
+        $services = $this->servicesQuestion();
         $input->setOption('services', $services);
         
         // --config_file option
         $config_file = $input->getOption('config-file');
 
         if (!$config_file) {
-            $config_file = $io->confirm(
+            $config_file = $this->getIo()->confirm(
                 $this->trans('commands.generate.form.questions.config-file'),
                 true
             );
@@ -271,9 +296,12 @@ abstract class FormCommand extends Command
         $inputs = $input->getOption('inputs');
         if (!$inputs) {
             // @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
-            $inputs = $this->formQuestion($io);
+            $inputs = $this->formQuestion();
             $input->setOption('inputs', $inputs);
+        } else {
+            $inputs= $this->explodeInlineArray($inputs);
         }
+        $input->setOption('inputs', $inputs);
 
         $path = $input->getOption('path');
         if (!$path) {
@@ -291,7 +319,7 @@ abstract class FormCommand extends Command
                     $this->stringConverter->camelCaseToMachineName($this->stringConverter->removeSuffix($className))
                 );
             }
-            $path = $io->ask(
+            $path = $this->getIo()->ask(
                 $this->trans('commands.generate.form.questions.path'),
                 $form_path,
                 function ($path) {
@@ -314,16 +342,16 @@ abstract class FormCommand extends Command
 
         // --link option for links.menu
         if ($this->formType == 'ConfigFormBase') {
-            $menu_options = $this->menuQuestion($io, $className);
-            $menu_link_gen = $input->getOption('menu_link_gen');
-            $menu_link_title = $input->getOption('menu_link_title');
-            $menu_parent = $input->getOption('menu_parent');
-            $menu_link_desc = $input->getOption('menu_link_desc');
+            $menu_options = $this->menuQuestion($className);
+            $menu_link_gen = $input->getOption('menu-link-gen');
+            $menu_link_title = $input->getOption('menu-link-title');
+            $menu_parent = $input->getOption('menu-parent');
+            $menu_link_desc = $input->getOption('menu-link-desc');
             if (!$menu_link_gen || !$menu_link_title || !$menu_parent || !$menu_link_desc) {
-                $input->setOption('menu_link_gen', $menu_options['menu_link_gen']);
-                $input->setOption('menu_link_title', $menu_options['menu_link_title']);
-                $input->setOption('menu_parent', $menu_options['menu_parent']);
-                $input->setOption('menu_link_desc', $menu_options['menu_link_desc']);
+                $input->setOption('menu-link-gen', $menu_options['menu_link_gen']);
+                $input->setOption('menu-link-title', $menu_options['menu_link_title']);
+                $input->setOption('menu-parent', $menu_options['menu_parent']);
+                $input->setOption('menu-link-desc', $menu_options['menu_link_desc']);
             }
         }
     }