Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ServiceCommand.php
index 8106f152328eb10e6d88544eb0e47bc1a3debf97..3e387da35c74aeb637daf7beef9670480eccf7c3 100644 (file)
@@ -7,6 +7,7 @@
 
 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;
@@ -14,9 +15,7 @@ use Drupal\Console\Command\Shared\ServicesTrait;
 use Drupal\Console\Command\Shared\ModuleTrait;
 use Drupal\Console\Generator\ServiceGenerator;
 use Drupal\Console\Command\Shared\ConfirmationTrait;
-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\Extension\Manager;
 use Drupal\Console\Core\Utils\ChainQueue;
 use Drupal\Console\Core\Utils\StringConverter;
@@ -26,21 +25,20 @@ use Drupal\Console\Core\Utils\StringConverter;
  *
  * @package Drupal\Console\Command\Generate
  */
-class ServiceCommand extends Command
+class ServiceCommand extends ContainerAwareCommand
 {
     use ServicesTrait;
     use ModuleTrait;
     use ConfirmationTrait;
-    use ContainerAwareCommandTrait;
 
     /**
- * @var Manager
-*/
    * @var Manager
+     */
     protected $extensionManager;
 
     /**
- * @var ServiceGenerator
-*/
    * @var ServiceGenerator
+     */
     protected $generator;
 
     /**
@@ -48,6 +46,11 @@ class ServiceCommand extends Command
      */
     protected $stringConverter;
 
+    /**
+     * @var Validator
+     */
+    protected $validator;
+
     /**
      * @var ChainQueue
      */
@@ -59,17 +62,20 @@ class ServiceCommand extends Command
      * @param Manager          $extensionManager
      * @param ServiceGenerator $generator
      * @param StringConverter  $stringConverter
+     * @param Validator        $validator
      * @param ChainQueue       $chainQueue
      */
     public function __construct(
         Manager $extensionManager,
         ServiceGenerator $generator,
         StringConverter $stringConverter,
+        Validator $validator,
         ChainQueue $chainQueue
     ) {
         $this->extensionManager = $extensionManager;
         $this->generator = $generator;
         $this->stringConverter = $stringConverter;
+        $this->validator = $validator;
         $this->chainQueue = $chainQueue;
         parent::__construct();
     }
@@ -94,7 +100,7 @@ class ServiceCommand extends Command
                 'name',
                 null,
                 InputOption::VALUE_REQUIRED,
-                $this->trans('commands.generate.service.options.name')
+                $this->trans('commands.generate.service.options.service-name')
             )
             ->addOption(
                 'class',
@@ -106,13 +112,13 @@ class ServiceCommand extends Command
                 'interface',
                 null,
                 InputOption::VALUE_NONE,
-                $this->trans('commands.common.service.options.interface')
+                $this->trans('commands.generate.service.options.interface')
             )
             ->addOption(
-                'interface_name',
+                'interface-name',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.common.service.options.interface_name')
+                $this->trans('commands.generate.service.options.interface-name')
             )
             ->addOption(
                 'services',
@@ -121,11 +127,12 @@ class ServiceCommand extends Command
                 $this->trans('commands.common.options.services')
             )
             ->addOption(
-                'path_service',
+                'path-service',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.service.options.path')
-            );
+                $this->trans('commands.generate.service.options.path-service')
+            )
+            ->setAliases(['gs']);
     }
 
     /**
@@ -133,20 +140,18 @@ class ServiceCommand 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');
         $name = $input->getOption('name');
-        $class = $input->getOption('class');
+        $class = $this->validator->validateClassName($input->getOption('class'));
         $interface = $input->getOption('interface');
-        $interface_name = $input->getOption('interface_name');
+        $interface_name = $input->getOption('interface-name');
         $services = $input->getOption('services');
-        $path_service = $input->getOption('path_service');
+        $path_service = $input->getOption('path-service');
 
         $available_services = $this->container->getServiceIds();
 
@@ -161,7 +166,14 @@ class ServiceCommand extends Command
 
         // @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
         $build_services = $this->buildServices($services);
-        $this->generator->generate($module, $name, $class, $interface, $interface_name, $build_services, $path_service);
+        $this->generator->generate([
+            'module' => $module,
+            'name' => $name,
+            'class' => $class,
+            'interface' => $interface,
+            'services' => $build_services,
+            'path_service' => $path_service,
+        ]);
 
         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
 
@@ -173,20 +185,13 @@ class ServiceCommand 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();
 
         //--name option
         $name = $input->getOption('name');
         if (!$name) {
-            $name = $io->ask(
+            $name = $this->getIo()->ask(
                 $this->trans('commands.generate.service.questions.service-name'),
                 $module.'.default'
             );
@@ -196,9 +201,12 @@ class ServiceCommand extends Command
         // --class option
         $class = $input->getOption('class');
         if (!$class) {
-            $class = $io->ask(
+            $class = $this->getIo()->ask(
                 $this->trans('commands.generate.service.questions.class'),
-                'DefaultService'
+                'DefaultService',
+                function ($class) {
+                    return $this->validator->validateClassName($class);
+                }
             );
             $input->setOption('class', $class);
         }
@@ -206,7 +214,7 @@ class ServiceCommand extends Command
         // --interface option
         $interface = $input->getOption('interface');
         if (!$interface) {
-            $interface = $io->confirm(
+            $interface = $this->getIo()->confirm(
                 $this->trans('commands.generate.service.questions.interface'),
                 true
             );
@@ -214,30 +222,30 @@ class ServiceCommand extends Command
         }
 
         // --interface_name option
-        $interface_name = $input->getOption('interface_name');
+        $interface_name = $input->getOption('interface-name');
         if ($interface && !$interface_name) {
-            $interface_name = $io->askEmpty(
-                $this->trans('commands.generate.service.questions.interface_name')
+            $interface_name = $this->getIo()->askEmpty(
+                $this->trans('commands.generate.service.questions.interface-name')
             );
-            $input->setOption('interface_name', $interface_name);
+            $input->setOption('interface-name', $interface_name);
         }
 
         // --services option
         $services = $input->getOption('services');
         if (!$services) {
             // @see Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
-            $services = $this->servicesQuestion($io);
+            $services = $this->servicesQuestion();
             $input->setOption('services', $services);
         }
 
         // --path_service option
-        $path_service = $input->getOption('path_service');
+        $path_service = $input->getOption('path-service');
         if (!$path_service) {
-            $path_service = $io->ask(
-                $this->trans('commands.generate.service.questions.path'),
+            $path_service = $this->getIo()->ask(
+                $this->trans('commands.generate.service.questions.path-service'),
                 '/modules/custom/' . $module . '/src/'
             );
-            $input->setOption('path_service', $path_service);
+            $input->setOption('path-service', $path_service);
         }
     }
 }