Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ControllerCommand.php
index 02f09391b09eed52e26937d2072af69492498732..dd982279d4bebe8ff29b18ff77218e317890b900 100644 (file)
@@ -14,32 +14,29 @@ use Drupal\Console\Command\Shared\ServicesTrait;
 use Drupal\Console\Command\Shared\ConfirmationTrait;
 use Drupal\Console\Command\Shared\ModuleTrait;
 use Drupal\Console\Generator\ControllerGenerator;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\ContainerAwareCommand;
 use Drupal\Core\Routing\RouteProviderInterface;
-use Drupal\Console\Core\Style\DrupalStyle;
 use Drupal\Console\Core\Utils\StringConverter;
-use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
 use Drupal\Console\Core\Utils\ChainQueue;
 use Drupal\Console\Core\Command\Shared\InputTrait;
 use Drupal\Console\Extension\Manager;
 use Drupal\Console\Utils\Validator;
 
-class ControllerCommand extends Command
+class ControllerCommand extends ContainerAwareCommand
 {
     use ModuleTrait;
     use ServicesTrait;
     use ConfirmationTrait;
     use InputTrait;
-    use ContainerAwareCommandTrait;
 
     /**
- * @var Manager
-*/
    * @var Manager
+     */
     protected $extensionManager;
 
     /**
- * @var ControllerGenerator
-*/
    * @var ControllerGenerator
+     */
     protected $generator;
 
     /**
@@ -48,13 +45,13 @@ class ControllerCommand extends Command
     protected $stringConverter;
 
     /**
- * @var Validator
-*/
    * @var Validator
+     */
     protected $validator;
 
     /**
- * @var RouteProviderInterface
-*/
    * @var RouteProviderInterface
+     */
     protected $routeProvider;
 
     /**
@@ -124,7 +121,8 @@ class ControllerCommand extends Command
                 null,
                 InputOption::VALUE_NONE,
                 $this->trans('commands.generate.controller.options.test')
-            );
+            )
+            ->setAliases(['gcon']);
     }
 
     /**
@@ -132,16 +130,13 @@ class ControllerCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-        $yes = $input->hasOption('yes')?$input->getOption('yes'):false;
-
-        // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
-        if (!$this->confirmGeneration($io, $yes)) {
+        // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+        if (!$this->confirmOperation()) {
             return 1;
         }
 
         $module = $input->getOption('module');
-        $class = $input->getOption('class');
+        $class = $this->validator->validateControllerName($input->getOption('class'));
         $routes = $input->getOption('routes');
         $test = $input->getOption('test');
         $services = $input->getOption('services');
@@ -153,13 +148,13 @@ class ControllerCommand extends Command
         $build_services = $this->buildServices($services);
 
         //$this->generator->setLearning($learning);
-        $this->generator->generate(
-            $module,
-            $class,
-            $routes,
-            $test,
-            $build_services
-        );
+        $this->generator->generate([
+            'module' => $module,
+            'class_name' => $class,
+            'routes' => $routes,
+            'test' => $test,
+            'services' => $build_services
+        ]);
 
         // Run cache rebuild to see changes in Web UI
         $this->chainQueue->addCommand('router:rebuild', []);
@@ -172,24 +167,17 @@ class ControllerCommand 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
         $class = $input->getOption('class');
         if (!$class) {
-            $class = $io->ask(
+            $class = $this->getIo()->ask(
                 $this->trans('commands.generate.controller.questions.class'),
                 'DefaultController',
                 function ($class) {
-                    return $this->validator->validateClassName($class);
+                    return $this->validator->validateControllerName($class);
                 }
             );
             $input->setOption('class', $class);
@@ -198,8 +186,9 @@ class ControllerCommand extends Command
         $routes = $input->getOption('routes');
         if (!$routes) {
             while (true) {
-                $title = $io->askEmpty(
+                $title = $this->getIo()->askEmpty(
                     $this->trans('commands.generate.controller.questions.title'),
+                    '',
                     function ($title) use ($routes) {
                         if ($routes && empty(trim($title))) {
                             return false;
@@ -232,7 +221,7 @@ class ControllerCommand extends Command
                     break;
                 }
 
-                $method = $io->ask(
+                $method = $this->getIo()->ask(
                     $this->trans('commands.generate.controller.questions.method'),
                     'hello',
                     function ($method) use ($routes) {
@@ -251,9 +240,12 @@ class ControllerCommand extends Command
                     }
                 );
 
-                $path = $io->ask(
+                $path = $this->getIo()->ask(
                     $this->trans('commands.generate.controller.questions.path'),
-                    sprintf('/%s/hello/{name}', $module),
+                    sprintf(
+                        '/%s/'.($method!='hello'?$method:'hello/{name}'),
+                        $module
+                    ),
                     function ($path) use ($routes) {
                         if (count($this->routeProvider->getRoutesByPattern($path)) > 0
                             || in_array($path, array_column($routes, 'path'))
@@ -292,7 +284,7 @@ class ControllerCommand extends Command
         // --test option
         $test = $input->getOption('test');
         if (!$test) {
-            $test = $io->confirm(
+            $test = $this->getIo()->confirm(
                 $this->trans('commands.generate.controller.questions.test'),
                 true
             );
@@ -302,15 +294,7 @@ class ControllerCommand extends Command
 
         // --services option
         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
-        $services = $this->servicesQuestion($io);
+        $services = $this->servicesQuestion();
         $input->setOption('services', $services);
     }
-
-    /**
-     * @return \Drupal\Console\Generator\ControllerGenerator
-     */
-    protected function createGenerator()
-    {
-        return new ControllerGenerator();
-    }
 }