Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginBlockCommand.php
index ff35cb3c26a66885120bb7cd1de458cc2d0904bf..a1f6dd6a43b59ea144c15ad1ea28b82d954b2c5d 100644 (file)
@@ -7,32 +7,31 @@
 
 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 Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\ContainerAwareCommand;
 use Drupal\Console\Generator\PluginBlockGenerator;
 use Drupal\Console\Command\Shared\ServicesTrait;
 use Drupal\Console\Command\Shared\ModuleTrait;
 use Drupal\Console\Command\Shared\FormTrait;
 use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
 use Drupal\Console\Extension\Manager;
 use Drupal\Console\Utils\Validator;
 use Drupal\Console\Core\Utils\StringConverter;
-use Drupal\Console\Core\Style\DrupalStyle;
 use Drupal\Console\Core\Utils\ChainQueue;
 use Drupal\Core\Config\ConfigFactory;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Render\ElementInfoManagerInterface;
 
-class PluginBlockCommand extends Command
+class PluginBlockCommand extends ContainerAwareCommand
 {
+    use ArrayInputTrait;
     use ServicesTrait;
     use ModuleTrait;
     use FormTrait;
     use ConfirmationTrait;
-    use ContainerAwareCommandTrait;
 
     /**
      * @var ConfigFactory
@@ -113,7 +112,12 @@ class PluginBlockCommand extends Command
             ->setName('generate:plugin:block')
             ->setDescription($this->trans('commands.generate.plugin.block.description'))
             ->setHelp($this->trans('commands.generate.plugin.block.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(
                 'class',
                 null,
@@ -149,7 +153,8 @@ class PluginBlockCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
                 $this->trans('commands.common.options.services')
-            );
+            )
+            ->setAliases(['gpb']);
     }
 
     /**
@@ -157,26 +162,29 @@ class PluginBlockCommand 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');
-        $class_name = $input->getOption('class');
+        $class_name = $this->validator->validateClassName($input->getOption('class'));
         $label = $input->getOption('label');
         $plugin_id = $input->getOption('plugin-id');
         $services = $input->getOption('services');
         $theme_region = $input->getOption('theme-region');
         $inputs = $input->getOption('inputs');
+        $noInteraction = $input->getOption('no-interaction');
+        // Parse nested data.
+        if ($noInteraction) {
+            $inputs = $this->explodeInlineArray($inputs);
+        }
 
         $theme = $this->configFactory->get('system.theme')->get('default');
         $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
 
         if (!empty($theme_region) && !isset($themeRegions[$theme_region])) {
-            $io->error(
+            $this->getIo()->error(
                 sprintf(
                     $this->trans('commands.generate.plugin.block.messages.invalid-theme-region'),
                     $theme_region
@@ -189,28 +197,26 @@ class PluginBlockCommand extends Command
         // @see use Drupal\Console\Command\Shared\ServicesTrait::buildServices
         $build_services = $this->buildServices($services);
 
-        $this->generator
-            ->generate(
-                $module,
-                $class_name,
-                $label,
-                $plugin_id,
-                $build_services,
-                $inputs
-            );
+        $this->generator->generate([
+          'module' => $module,
+          'class_name' => $class_name,
+          'label' => $label,
+          'plugin_id' => $plugin_id,
+          'services' => $build_services,
+          'inputs' => $inputs,
+        ]);
+
 
         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
 
         if ($theme_region) {
             $block = $this->entityTypeManager
                 ->getStorage('block')
-                ->create(
-                    [
-                        'id'=> $plugin_id,
-                        'plugin' => $plugin_id,
-                        'theme' => $theme
-                    ]
-                );
+                ->create([
+                    'id'=> $plugin_id,
+                    'plugin' => $plugin_id,
+                    'theme' => $theme,
+                ]);
             $block->setRegion($theme_region);
             $block->save();
         }
@@ -218,23 +224,16 @@ class PluginBlockCommand extends Command
 
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $theme = $this->configFactory->get('system.theme')->get('default');
         $themeRegions = \system_region_list($theme, REGIONS_VISIBLE);
 
         // --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();
 
         // --class option
         $class = $input->getOption('class');
         if (!$class) {
-            $class = $io->ask(
+            $class = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.block.questions.class'),
                 'DefaultBlock',
                 function ($class) {
@@ -247,7 +246,7 @@ class PluginBlockCommand extends Command
         // --label option
         $label = $input->getOption('label');
         if (!$label) {
-            $label = $io->ask(
+            $label = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.block.questions.label'),
                 $this->stringConverter->camelCaseToHuman($class)
             );
@@ -257,7 +256,7 @@ class PluginBlockCommand extends Command
         // --plugin-id option
         $pluginId = $input->getOption('plugin-id');
         if (!$pluginId) {
-            $pluginId = $io->ask(
+            $pluginId = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.block.questions.plugin-id'),
                 $this->stringConverter->camelCaseToUnderscore($class)
             );
@@ -267,10 +266,10 @@ class PluginBlockCommand extends Command
         // --theme-region option
         $themeRegion = $input->getOption('theme-region');
         if (!$themeRegion) {
-            $themeRegion =  $io->choiceNoList(
+            $themeRegion = $this->getIo()->choiceNoList(
                 $this->trans('commands.generate.plugin.block.questions.theme-region'),
                 array_values($themeRegions),
-                null,
+                '',
                 true
             );
             $themeRegion = array_search($themeRegion, $themeRegions);
@@ -279,13 +278,20 @@ class PluginBlockCommand extends Command
 
         // --services option
         // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
-        $services = $this->servicesQuestion($io);
+        $services = $this->servicesQuestion();
         $input->setOption('services', $services);
 
         $output->writeln($this->trans('commands.generate.plugin.block.messages.inputs'));
 
-        // @see Drupal\Console\Command\Shared\FormTrait::formQuestion
-        $inputs = $this->formQuestion($io);
+        // --inputs option
+        $inputs = $input->getOption('inputs');
+        if (!$inputs) {
+            // @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
+            $inputs = $this->formQuestion();
+            $input->setOption('inputs', $inputs);
+        } else {
+            $inputs = $this->explodeInlineArray($inputs);
+        }
         $input->setOption('inputs', $inputs);
     }
 }