Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginFieldCommand.php
index 664123899e715eff1dca4d17d9b14018dd154311..e6979177b85b76b639f2180e75f049bfd1b2467a 100644 (file)
@@ -7,15 +7,14 @@
 
 namespace Drupal\Console\Command\Generate;
 
+use Drupal\Console\Utils\Validator;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Drupal\Console\Command\Shared\ModuleTrait;
 use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Style\DrupalStyle;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Console\Extension\Manager;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
 use Drupal\Console\Core\Utils\StringConverter;
 use Drupal\Console\Core\Utils\ChainQueue;
 
@@ -23,7 +22,6 @@ class PluginFieldCommand extends Command
 {
     use ModuleTrait;
     use ConfirmationTrait;
-    use CommandTrait;
 
     /**
  * @var Manager
@@ -35,6 +33,11 @@ class PluginFieldCommand extends Command
      */
     protected $stringConverter;
 
+    /**
+     * @var Validator
+     */
+    protected $validator;
+
     /**
      * @var ChainQueue
      */
@@ -46,15 +49,18 @@ class PluginFieldCommand extends Command
      *
      * @param Manager         $extensionManager
      * @param StringConverter $stringConverter
+     * @param Validator       $validator
      * @param ChainQueue      $chainQueue
      */
     public function __construct(
         Manager $extensionManager,
         StringConverter $stringConverter,
+        Validator $validator,
         ChainQueue $chainQueue
     ) {
         $this->extensionManager = $extensionManager;
         $this->stringConverter = $stringConverter;
+        $this->validator = $validator;
         $this->chainQueue = $chainQueue;
         parent::__construct();
     }
@@ -65,7 +71,12 @@ class PluginFieldCommand extends Command
             ->setName('generate:plugin:field')
             ->setDescription($this->trans('commands.generate.plugin.field.description'))
             ->setHelp($this->trans('commands.generate.plugin.field.help'))
-            ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
+            ->addOption(
+                'module',
+                null,
+                InputOption::VALUE_REQUIRED,
+                $this->trans('commands.common.options.module')
+            )
             ->addOption(
                 'type-class',
                 null,
@@ -88,13 +99,13 @@ class PluginFieldCommand extends Command
                 'type-description',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.plugin.field.options.type-type-description')
+                $this->trans('commands.generate.plugin.field.options.type-description')
             )
             ->addOption(
                 'formatter-class',
                 null,
                 InputOption::VALUE_REQUIRED,
-                $this->trans('commands.generate.plugin.field.options.class')
+                $this->trans('commands.generate.plugin.field.options.formatter-class')
             )
             ->addOption(
                 'formatter-label',
@@ -143,7 +154,8 @@ class PluginFieldCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL,
                 $this->trans('commands.generate.plugin.field.options.default-formatter')
-            );
+            )
+            ->setAliases(['gpf']);
     }
 
     /**
@@ -151,10 +163,8 @@ class PluginFieldCommand 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;
         }
 
@@ -162,7 +172,7 @@ class PluginFieldCommand extends Command
             ->addCommand(
                 'generate:plugin:fieldtype', [
                 '--module' => $input->getOption('module'),
-                '--class' => $input->getOption('type-class'),
+                '--class' => $this->validator->validateClassName($input->getOption('type-class')),
                 '--label' => $input->getOption('type-label'),
                 '--plugin-id' => $input->getOption('type-plugin-id'),
                 '--description' => $input->getOption('type-description'),
@@ -176,7 +186,7 @@ class PluginFieldCommand extends Command
             ->addCommand(
                 'generate:plugin:fieldwidget', [
                 '--module' => $input->getOption('module'),
-                '--class' => $input->getOption('widget-class'),
+                '--class' => $this->validator->validateClassName($input->getOption('widget-class')),
                 '--label' => $input->getOption('widget-label'),
                 '--plugin-id' => $input->getOption('widget-plugin-id'),
                 '--field-type' => $input->getOption('field-type'),
@@ -187,7 +197,7 @@ class PluginFieldCommand extends Command
             ->addCommand(
                 'generate:plugin:fieldformatter', [
                 '--module' => $input->getOption('module'),
-                '--class' => $input->getOption('formatter-class'),
+                '--class' => $this->validator->validateClassName($input->getOption('formatter-class')),
                 '--label' => $input->getOption('formatter-label'),
                 '--plugin-id' => $input->getOption('formatter-plugin-id'),
                 '--field-type' => $input->getOption('field-type'),
@@ -202,22 +212,18 @@ class PluginFieldCommand 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();
 
         // --type-class option
         $typeClass = $input->getOption('type-class');
         if (!$typeClass) {
-            $typeClass = $io->ask(
+            $typeClass = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.type-class'),
-                'ExampleFieldType'
+                'ExampleFieldType',
+                function ($typeClass) {
+                    return $this->validator->validateClassName($typeClass);
+                }
             );
             $input->setOption('type-class', $typeClass);
         }
@@ -225,7 +231,7 @@ class PluginFieldCommand extends Command
         // --type-label option
         $label = $input->getOption('type-label');
         if (!$label) {
-            $label = $io->ask(
+            $label = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.type-label'),
                 $this->stringConverter->camelCaseToHuman($typeClass)
             );
@@ -235,7 +241,7 @@ class PluginFieldCommand extends Command
         // --type-plugin-id option
         $plugin_id = $input->getOption('type-plugin-id');
         if (!$plugin_id) {
-            $plugin_id = $io->ask(
+            $plugin_id = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.type-plugin-id'),
                 $this->stringConverter->camelCaseToUnderscore($typeClass)
             );
@@ -245,9 +251,9 @@ class PluginFieldCommand extends Command
         // --type-description option
         $description = $input->getOption('type-description');
         if (!$description) {
-            $description = $io->ask(
+            $description = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.type-description'),
-                'My Field Type'
+                $this->trans('commands.generate.plugin.field.suggestions.my-field-type')
             );
             $input->setOption('type-description', $description);
         }
@@ -255,9 +261,12 @@ class PluginFieldCommand extends Command
         // --widget-class option
         $widgetClass = $input->getOption('widget-class');
         if (!$widgetClass) {
-            $widgetClass = $io->ask(
+            $widgetClass = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.widget-class'),
-                'ExampleWidgetType'
+                'ExampleWidgetType',
+                function ($widgetClass) {
+                    return $this->validator->validateClassName($widgetClass);
+                }
             );
             $input->setOption('widget-class', $widgetClass);
         }
@@ -265,7 +274,7 @@ class PluginFieldCommand extends Command
         // --widget-label option
         $widgetLabel = $input->getOption('widget-label');
         if (!$widgetLabel) {
-            $widgetLabel = $io->ask(
+            $widgetLabel = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.widget-label'),
                 $this->stringConverter->camelCaseToHuman($widgetClass)
             );
@@ -275,7 +284,7 @@ class PluginFieldCommand extends Command
         // --widget-plugin-id option
         $widget_plugin_id = $input->getOption('widget-plugin-id');
         if (!$widget_plugin_id) {
-            $widget_plugin_id = $io->ask(
+            $widget_plugin_id = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.widget-plugin-id'),
                 $this->stringConverter->camelCaseToUnderscore($widgetClass)
             );
@@ -285,9 +294,12 @@ class PluginFieldCommand extends Command
         // --formatter-class option
         $formatterClass = $input->getOption('formatter-class');
         if (!$formatterClass) {
-            $formatterClass = $io->ask(
+            $formatterClass = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.formatter-class'),
-                'ExampleFormatterType'
+                'ExampleFormatterType',
+                function ($formatterClass) {
+                    return $this->validator->validateClassName($formatterClass);
+                }
             );
             $input->setOption('formatter-class', $formatterClass);
         }
@@ -295,7 +307,7 @@ class PluginFieldCommand extends Command
         // --formatter-label option
         $formatterLabel = $input->getOption('formatter-label');
         if (!$formatterLabel) {
-            $formatterLabel = $io->ask(
+            $formatterLabel = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.formatter-label'),
                 $this->stringConverter->camelCaseToHuman($formatterClass)
             );
@@ -305,7 +317,7 @@ class PluginFieldCommand extends Command
         // --formatter-plugin-id option
         $formatter_plugin_id = $input->getOption('formatter-plugin-id');
         if (!$formatter_plugin_id) {
-            $formatter_plugin_id = $io->ask(
+            $formatter_plugin_id = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.formatter-plugin-id'),
                 $this->stringConverter->camelCaseToUnderscore($formatterClass)
             );
@@ -315,7 +327,7 @@ class PluginFieldCommand extends Command
         // --field-type option
         $field_type = $input->getOption('field-type');
         if (!$field_type) {
-            $field_type = $io->ask(
+            $field_type = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.field-type'),
                 $plugin_id
             );
@@ -325,7 +337,7 @@ class PluginFieldCommand extends Command
         // --default-widget option
         $default_widget = $input->getOption('default-widget');
         if (!$default_widget) {
-            $default_widget = $io->ask(
+            $default_widget = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.default-widget'),
                 $widget_plugin_id
             );
@@ -335,7 +347,7 @@ class PluginFieldCommand extends Command
         // --default-formatter option
         $default_formatter = $input->getOption('default-formatter');
         if (!$default_formatter) {
-            $default_formatter = $io->ask(
+            $default_formatter = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.field.questions.default-formatter'),
                 $formatter_plugin_id
             );