Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginRestResourceCommand.php
index dfc7ce1eeadecd4d9b5db80b5afc755a556c1f8c..e275c2e53deb21462036a428bd23cee0489c5223 100644 (file)
@@ -7,18 +7,16 @@
 
 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\ServicesTrait;
 use Drupal\Console\Command\Shared\ModuleTrait;
-use Drupal\Console\Command\Shared\FormTrait;
 use Drupal\Console\Generator\PluginRestResourceGenerator;
 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;
 
@@ -31,18 +29,16 @@ class PluginRestResourceCommand extends Command
 {
     use ServicesTrait;
     use ModuleTrait;
-    use FormTrait;
     use ConfirmationTrait;
-    use CommandTrait;
 
     /**
- * @var Manager
-*/
    * @var Manager
+     */
     protected $extensionManager;
 
     /**
- * @var PluginRestResourceGenerator
-*/
    * @var PluginRestResourceGenerator
+     */
     protected $generator;
 
     /**
@@ -50,6 +46,11 @@ class PluginRestResourceCommand extends Command
      */
     protected $stringConverter;
 
+    /**
+     * @var Validator
+     */
+    protected $validator;
+
     /**
      * @var ChainQueue
      */
@@ -62,17 +63,20 @@ class PluginRestResourceCommand extends Command
      * @param Manager                     $extensionManager
      * @param PluginRestResourceGenerator $generator
      * @param StringConverter             $stringConverter
+     * @param Validator                   $validator
      * @param ChainQueue                  $chainQueue
      */
     public function __construct(
         Manager $extensionManager,
         PluginRestResourceGenerator $generator,
         StringConverter $stringConverter,
+        Validator $validator,
         ChainQueue $chainQueue
     ) {
         $this->extensionManager = $extensionManager;
         $this->generator = $generator;
         $this->stringConverter = $stringConverter;
+        $this->validator = $validator;
         $this->chainQueue = $chainQueue;
         parent::__construct();
     }
@@ -83,18 +87,17 @@ class PluginRestResourceCommand extends Command
             ->setName('generate:plugin:rest:resource')
             ->setDescription($this->trans('commands.generate.plugin.rest.resource.description'))
             ->setHelp($this->trans('commands.generate.plugin.rest.resource.help'))
-            ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
             ->addOption(
-                'class',
+                'module',
                 null,
-                InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.plugin.rest.resource.options.class')
+                InputOption::VALUE_REQUIRED,
+                $this->trans('commands.common.options.module')
             )
             ->addOption(
-                'name',
+                'class',
                 null,
                 InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.generate.service.options.name')
+                $this->trans('commands.generate.plugin.rest.resource.options.class')
             )
             ->addOption(
                 'plugin-id',
@@ -119,7 +122,8 @@ class PluginRestResourceCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
                 $this->trans('commands.generate.plugin.rest.resource.options.plugin-states')
-            );
+            )
+            ->setAliases(['gprr']);
     }
 
     /**
@@ -127,21 +131,32 @@ class PluginRestResourceCommand 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;
         }
 
+        $http_methods = $this->getHttpMethods();
         $module = $input->getOption('module');
-        $class_name = $input->getOption('class');
+        $class_name = $this->validator->validateClassName($input->getOption('class'));
         $plugin_id = $input->getOption('plugin-id');
         $plugin_label = $input->getOption('plugin-label');
         $plugin_url = $input->getOption('plugin-url');
-        $plugin_states = $input->getOption('plugin-states');
+        $plugin_states = $this->validator->validateHttpMethods($input->getOption('plugin-states'), $http_methods);
+
+        $prepared_plugin = [];
+        foreach ($plugin_states as $plugin_state) {
+            $prepared_plugin[$plugin_state] = $http_methods[$plugin_state];
+        }
 
-        $this->generator->generate($module, $class_name, $plugin_label, $plugin_id, $plugin_url, $plugin_states);
+        $this->generator->generate([
+            'module_name' => $module,
+            'class_name' => $class_name,
+            'plugin_label' => $plugin_label,
+            'plugin_id' => $plugin_id,
+            'plugin_url' => $plugin_url,
+            'plugin_states' => $prepared_plugin,
+        ]);
 
         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
 
@@ -150,29 +165,17 @@ class PluginRestResourceCommand 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();
 
         // --class option
         $class_name = $input->getOption('class');
         if (!$class_name) {
-            $stringUtils = $this->stringConverter;
-            $class_name = $io->ask(
+            $class_name = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.rest.resource.questions.class'),
                 'DefaultRestResource',
-                function ($class_name) use ($stringUtils) {
-                    if (!strlen(trim($class_name))) {
-                        throw new \Exception('The Class name can not be empty');
-                    }
-
-                    return $stringUtils->humanToCamelCase($class_name);
+                function ($class) {
+                    return $this->validator->validateClassName($class);
                 }
             );
             $input->setOption('class', $class_name);
@@ -181,7 +184,7 @@ class PluginRestResourceCommand extends Command
         // --plugin-id option
         $plugin_id = $input->getOption('plugin-id');
         if (!$plugin_id) {
-            $plugin_id = $io->ask(
+            $plugin_id = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.rest.resource.questions.plugin-id'),
                 $this->stringConverter->camelCaseToUnderscore($class_name)
             );
@@ -191,7 +194,7 @@ class PluginRestResourceCommand extends Command
         // --plugin-label option
         $plugin_label = $input->getOption('plugin-label');
         if (!$plugin_label) {
-            $plugin_label = $io->ask(
+            $plugin_label = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.rest.resource.questions.plugin-label'),
                 $this->stringConverter->camelCaseToHuman($class_name)
             );
@@ -201,17 +204,18 @@ class PluginRestResourceCommand extends Command
         // --plugin-url option
         $plugin_url = $input->getOption('plugin-url');
         if (!$plugin_url) {
-            $plugin_url = $io->ask(
+            $plugin_url = $this->getIo()->ask(
                 $this->trans('commands.generate.plugin.rest.resource.questions.plugin-url')
             );
             $input->setOption('plugin-url', $plugin_url);
         }
 
+
         // --plugin-states option
         $plugin_states = $input->getOption('plugin-states');
         if (!$plugin_states) {
-            $states = ['GET', 'PUT', 'POST', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'];
-            $plugin_states = $io->choice(
+            $states = array_keys($this->getHttpMethods());
+            $plugin_states = $this->getIo()->choice(
                 $this->trans('commands.generate.plugin.rest.resource.questions.plugin-states'),
                 $states,
                 null,
@@ -221,4 +225,44 @@ class PluginRestResourceCommand extends Command
             $input->setOption('plugin-states', $plugin_states);
         }
     }
+
+    /**
+     * Returns available HTTP methods.
+     *
+     * @return array
+     *   Available HTTP methods.
+     */
+    protected function getHttpMethods()
+    {
+        return [
+            'GET' => [
+              'http_code' => 200,
+              'response_class' => 'ResourceResponse',
+            ],
+            'PUT' => [
+              'http_code' => 201,
+              'response_class' => 'ModifiedResourceResponse',
+            ],
+            'POST' => [
+              'http_code' => 200,
+              'response_class' => 'ModifiedResourceResponse',
+            ],
+            'PATCH' => [
+              'http_code' => 204,
+              'response_class' => 'ModifiedResourceResponse',
+            ],
+            'DELETE' => [
+              'http_code' => 204,
+              'response_class' => 'ModifiedResourceResponse',
+            ],
+            'HEAD' => [
+              'http_code' => 200,
+              'response_class' => 'ResourceResponse',
+            ],
+            'OPTIONS' => [
+              'http_code' => 200,
+              'response_class' => 'ResourceResponse',
+            ],
+        ];
+    }
 }