Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ProfileCommand.php
index 5dfbe1509205d726d09cd4ec62c748752f164fa3..0d61548c157970207c44355b3791d0254aa1bb9b 100644 (file)
@@ -8,16 +8,15 @@
 namespace Drupal\Console\Command\Generate;
 
 use Drupal\Console\Command\Shared\ConfirmationTrait;
-use Symfony\Component\Console\Command\Command;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Console\Generator\ProfileGenerator;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
 use Drupal\Console\Extension\Manager;
 use Drupal\Console\Core\Utils\StringConverter;
 use Drupal\Console\Utils\Validator;
+use Webmozart\PathUtil\Path;
 
 /**
  * Class ProfileCommand
@@ -28,7 +27,6 @@ use Drupal\Console\Utils\Validator;
 class ProfileCommand extends Command
 {
     use ConfirmationTrait;
-    use CommandTrait;
 
     /**
      * @var Manager
@@ -95,6 +93,12 @@ class ProfileCommand extends Command
                 InputOption::VALUE_REQUIRED,
                 $this->trans('commands.generate.profile.options.machine-name')
             )
+            ->addOption(
+                'profile-path',
+                null,
+                InputOption::VALUE_REQUIRED,
+                $this->trans('commands.generate.profile.options.profile-path')
+            )
             ->addOption(
                 'description',
                 null,
@@ -126,7 +130,7 @@ class ProfileCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL,
                 $this->trans('commands.generate.profile.options.distribution')
-            );
+            )->setAliases(['gpr']);
     }
 
     /**
@@ -134,31 +138,37 @@ class ProfileCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
-        if (!$this->confirmGeneration($io)) {
+        // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
+        if (!$this->confirmOperation()) {
             return 1;
         }
 
+        // Get the profile path and define a profile path if it is null
+        // Check that it is an absolute path or otherwise create an absolute path using appRoot
+        $profile_path = $input->getOption('profile-path');
+        $profile_path = $profile_path == null ? 'profiles' : $profile_path;
+        $profile_path = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
+        $profile_path = $this->validator->validateModulePath($profile_path, true);
+
         $profile = $this->validator->validateModuleName($input->getOption('profile'));
         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
         $description = $input->getOption('description');
         $core = $input->getOption('core');
-        $dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $io);
-        $themes = $this->validator->validateExtensions($input->getOption('themes'), 'theme', $io);
+        $dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $this->getIo());
+        $themes = $this->validator->validateExtensions($input->getOption('themes'), 'theme', $this->getIo());
         $distribution = $input->getOption('distribution');
-        $profile_path = $this->appRoot . '/profiles';
 
-        $this->generator->generate(
-            $profile,
-            $machine_name,
-            $profile_path,
-            $description,
-            $core,
-            $dependencies,
-            $themes,
-            $distribution
-        );
+        $this->generator->generate([
+            'profile' => $profile,
+            'machine_name' => $machine_name,
+            'type' => 'profile',
+            'core' => $core,
+            'description' => $description,
+            'dependencies' => $dependencies,
+            'themes' => $themes,
+            'distribution' => $distribution,
+            'dir' => $profile_path,
+        ]);
     }
 
     /**
@@ -166,8 +176,6 @@ class ProfileCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         //$stringUtils = $this->getStringHelper();
         $validators = $this->validator;
 
@@ -176,13 +184,13 @@ class ProfileCommand extends Command
             // validator to check the name.
             $profile = $input->getOption('profile') ? $validators->validateModuleName($input->getOption('profile')) : null;
         } catch (\Exception $error) {
-            $io->error($error->getMessage());
+            $this->getIo()->error($error->getMessage());
 
             return 1;
         }
 
         if (!$profile) {
-            $profile = $io->ask(
+            $profile = $this->getIo()->ask(
                 $this->trans('commands.generate.profile.questions.profile'),
                 '',
                 function ($profile) use ($validators) {
@@ -195,13 +203,13 @@ class ProfileCommand extends Command
         try {
             $machine_name = $input->getOption('machine-name') ? $validators->validateModuleName($input->getOption('machine-name')) : null;
         } catch (\Exception $error) {
-            $io->error($error->getMessage());
+            $this->getIo()->error($error->getMessage());
 
             return 1;
         }
 
         if (!$machine_name) {
-            $machine_name = $io->ask(
+            $machine_name = $this->getIo()->ask(
                 $this->trans('commands.generate.profile.questions.machine-name'),
                 $this->stringConverter->createMachineName($profile),
                 function ($machine_name) use ($validators) {
@@ -211,18 +219,41 @@ class ProfileCommand extends Command
             $input->setOption('machine-name', $machine_name);
         }
 
+        $profile_path = $input->getOption('profile-path');
+        if (!$profile_path) {
+            $profile_path = $this->getIo()->ask(
+                $this->trans('commands.generate.profile.questions.profile-path'),
+                'profiles',
+                function ($profile_path) use ($machine_name) {
+                    $fullPath = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
+                    $fullPath = $fullPath.'/'.$machine_name;
+                    if (file_exists($fullPath)) {
+                        throw new \InvalidArgumentException(
+                            sprintf(
+                                $this->trans('commands.generate.profile.errors.directory-exists'),
+                                $fullPath
+                            )
+                        );
+                    }
+
+                    return $profile_path;
+                }
+            );
+        }
+        $input->setOption('profile-path', $profile_path);
+
         $description = $input->getOption('description');
         if (!$description) {
-            $description = $io->ask(
+            $description = $this->getIo()->ask(
                 $this->trans('commands.generate.profile.questions.description'),
-                'My Useful Profile'
+                $this->trans('commands.generate.profile.suggestions.my-useful-profile')
             );
             $input->setOption('description', $description);
         }
 
         $core = $input->getOption('core');
         if (!$core) {
-            $core = $io->ask(
+            $core = $this->getIo()->ask(
                 $this->trans('commands.generate.profile.questions.core'),
                 '8.x'
             );
@@ -231,12 +262,12 @@ class ProfileCommand extends Command
 
         $dependencies = $input->getOption('dependencies');
         if (!$dependencies) {
-            if ($io->confirm(
+            if ($this->getIo()->confirm(
                 $this->trans('commands.generate.profile.questions.dependencies'),
                 true
             )
             ) {
-                $dependencies = $io->ask(
+                $dependencies = $this->getIo()->ask(
                     $this->trans('commands.generate.profile.options.dependencies'),
                     ''
                 );
@@ -246,25 +277,17 @@ class ProfileCommand extends Command
 
         $distribution = $input->getOption('distribution');
         if (!$distribution) {
-            if ($io->confirm(
+            if ($this->getIo()->confirm(
                 $this->trans('commands.generate.profile.questions.distribution'),
                 false
             )
             ) {
-                $distribution = $io->ask(
+                $distribution = $this->getIo()->ask(
                     $this->trans('commands.generate.profile.options.distribution'),
-                    'My Kick-ass Distribution'
+                    $this->trans('commands.generate.profile.suggestions.my-kick-ass-distribution')
                 );
                 $input->setOption('distribution', $distribution);
             }
         }
     }
-
-    /**
-     * @return ProfileGenerator
-     */
-    protected function createGenerator()
-    {
-        return new ProfileGenerator();
-    }
 }