0d61548c157970207c44355b3791d0254aa1bb9b
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ProfileCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\ProfileCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Command\Shared\ConfirmationTrait;
11 use Drupal\Console\Core\Command\Command;
12 use Drupal\Console\Generator\ProfileGenerator;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Drupal\Console\Extension\Manager;
17 use Drupal\Console\Core\Utils\StringConverter;
18 use Drupal\Console\Utils\Validator;
19 use Webmozart\PathUtil\Path;
20
21 /**
22  * Class ProfileCommand
23  *
24  * @package Drupal\Console\Command\Generate
25  */
26
27 class ProfileCommand extends Command
28 {
29     use ConfirmationTrait;
30
31     /**
32      * @var Manager
33      */
34     protected $extensionManager;
35
36     /**
37      * @var ProfileGenerator
38      */
39     protected $generator;
40
41     /**
42      * @var StringConverter
43      */
44     protected $stringConverter;
45
46     /**
47      * @var Validator
48      */
49     protected $validator;
50
51     /**
52      * ProfileCommand constructor.
53      *
54      * @param Manager          $extensionManager
55      * @param ProfileGenerator $generator
56      * @param StringConverter  $stringConverter
57      * @param Validator        $validator
58      * @param $appRoot
59      */
60     public function __construct(
61         Manager $extensionManager,
62         ProfileGenerator $generator,
63         StringConverter $stringConverter,
64         Validator $validator,
65         $appRoot
66     ) {
67         $this->extensionManager = $extensionManager;
68         $this->generator = $generator;
69         $this->stringConverter = $stringConverter;
70         $this->validator = $validator;
71         $this->appRoot = $appRoot;
72         parent::__construct();
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     protected function configure()
79     {
80         $this
81             ->setName('generate:profile')
82             ->setDescription($this->trans('commands.generate.profile.description'))
83             ->setHelp($this->trans('commands.generate.profile.help'))
84             ->addOption(
85                 'profile',
86                 null,
87                 InputOption::VALUE_REQUIRED,
88                 $this->trans('commands.generate.profile.options.profile')
89             )
90             ->addOption(
91                 'machine-name',
92                 null,
93                 InputOption::VALUE_REQUIRED,
94                 $this->trans('commands.generate.profile.options.machine-name')
95             )
96             ->addOption(
97                 'profile-path',
98                 null,
99                 InputOption::VALUE_REQUIRED,
100                 $this->trans('commands.generate.profile.options.profile-path')
101             )
102             ->addOption(
103                 'description',
104                 null,
105                 InputOption::VALUE_OPTIONAL,
106                 $this->trans('commands.generate.profile.options.description')
107             )
108             ->addOption(
109                 'core',
110                 null,
111                 InputOption::VALUE_OPTIONAL,
112                 $this->trans('commands.generate.profile.options.core')
113             )
114             ->addOption(
115                 'dependencies',
116                 null,
117                 InputOption::VALUE_OPTIONAL,
118                 $this->trans('commands.generate.profile.options.dependencies'),
119                 ''
120             )
121             ->addOption(
122                 'themes',
123                 null,
124                 InputOption::VALUE_OPTIONAL,
125                 $this->trans('commands.generate.profile.options.themes'),
126                 ''
127             )
128             ->addOption(
129                 'distribution',
130                 null,
131                 InputOption::VALUE_OPTIONAL,
132                 $this->trans('commands.generate.profile.options.distribution')
133             )->setAliases(['gpr']);
134     }
135
136     /**
137      * {@inheritdoc}
138      */
139     protected function execute(InputInterface $input, OutputInterface $output)
140     {
141         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
142         if (!$this->confirmOperation()) {
143             return 1;
144         }
145
146         // Get the profile path and define a profile path if it is null
147         // Check that it is an absolute path or otherwise create an absolute path using appRoot
148         $profile_path = $input->getOption('profile-path');
149         $profile_path = $profile_path == null ? 'profiles' : $profile_path;
150         $profile_path = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
151         $profile_path = $this->validator->validateModulePath($profile_path, true);
152
153         $profile = $this->validator->validateModuleName($input->getOption('profile'));
154         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
155         $description = $input->getOption('description');
156         $core = $input->getOption('core');
157         $dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $this->getIo());
158         $themes = $this->validator->validateExtensions($input->getOption('themes'), 'theme', $this->getIo());
159         $distribution = $input->getOption('distribution');
160
161         $this->generator->generate([
162             'profile' => $profile,
163             'machine_name' => $machine_name,
164             'type' => 'profile',
165             'core' => $core,
166             'description' => $description,
167             'dependencies' => $dependencies,
168             'themes' => $themes,
169             'distribution' => $distribution,
170             'dir' => $profile_path,
171         ]);
172     }
173
174     /**
175      * {@inheritdoc}
176      */
177     protected function interact(InputInterface $input, OutputInterface $output)
178     {
179         //$stringUtils = $this->getStringHelper();
180         $validators = $this->validator;
181
182         try {
183             // A profile is technically also a module, so we can use the same
184             // validator to check the name.
185             $profile = $input->getOption('profile') ? $validators->validateModuleName($input->getOption('profile')) : null;
186         } catch (\Exception $error) {
187             $this->getIo()->error($error->getMessage());
188
189             return 1;
190         }
191
192         if (!$profile) {
193             $profile = $this->getIo()->ask(
194                 $this->trans('commands.generate.profile.questions.profile'),
195                 '',
196                 function ($profile) use ($validators) {
197                     return $validators->validateModuleName($profile);
198                 }
199             );
200             $input->setOption('profile', $profile);
201         }
202
203         try {
204             $machine_name = $input->getOption('machine-name') ? $validators->validateModuleName($input->getOption('machine-name')) : null;
205         } catch (\Exception $error) {
206             $this->getIo()->error($error->getMessage());
207
208             return 1;
209         }
210
211         if (!$machine_name) {
212             $machine_name = $this->getIo()->ask(
213                 $this->trans('commands.generate.profile.questions.machine-name'),
214                 $this->stringConverter->createMachineName($profile),
215                 function ($machine_name) use ($validators) {
216                     return $validators->validateMachineName($machine_name);
217                 }
218             );
219             $input->setOption('machine-name', $machine_name);
220         }
221
222         $profile_path = $input->getOption('profile-path');
223         if (!$profile_path) {
224             $profile_path = $this->getIo()->ask(
225                 $this->trans('commands.generate.profile.questions.profile-path'),
226                 'profiles',
227                 function ($profile_path) use ($machine_name) {
228                     $fullPath = Path::isAbsolute($profile_path) ? $profile_path : Path::makeAbsolute($profile_path, $this->appRoot);
229                     $fullPath = $fullPath.'/'.$machine_name;
230                     if (file_exists($fullPath)) {
231                         throw new \InvalidArgumentException(
232                             sprintf(
233                                 $this->trans('commands.generate.profile.errors.directory-exists'),
234                                 $fullPath
235                             )
236                         );
237                     }
238
239                     return $profile_path;
240                 }
241             );
242         }
243         $input->setOption('profile-path', $profile_path);
244
245         $description = $input->getOption('description');
246         if (!$description) {
247             $description = $this->getIo()->ask(
248                 $this->trans('commands.generate.profile.questions.description'),
249                 $this->trans('commands.generate.profile.suggestions.my-useful-profile')
250             );
251             $input->setOption('description', $description);
252         }
253
254         $core = $input->getOption('core');
255         if (!$core) {
256             $core = $this->getIo()->ask(
257                 $this->trans('commands.generate.profile.questions.core'),
258                 '8.x'
259             );
260             $input->setOption('core', $core);
261         }
262
263         $dependencies = $input->getOption('dependencies');
264         if (!$dependencies) {
265             if ($this->getIo()->confirm(
266                 $this->trans('commands.generate.profile.questions.dependencies'),
267                 true
268             )
269             ) {
270                 $dependencies = $this->getIo()->ask(
271                     $this->trans('commands.generate.profile.options.dependencies'),
272                     ''
273                 );
274             }
275             $input->setOption('dependencies', $dependencies);
276         }
277
278         $distribution = $input->getOption('distribution');
279         if (!$distribution) {
280             if ($this->getIo()->confirm(
281                 $this->trans('commands.generate.profile.questions.distribution'),
282                 false
283             )
284             ) {
285                 $distribution = $this->getIo()->ask(
286                     $this->trans('commands.generate.profile.options.distribution'),
287                     $this->trans('commands.generate.profile.suggestions.my-kick-ass-distribution')
288                 );
289                 $input->setOption('distribution', $distribution);
290             }
291         }
292     }
293 }