More tidying.
[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 Symfony\Component\Console\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\Core\Style\DrupalStyle;
17 use Drupal\Console\Core\Command\Shared\CommandTrait;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Utils\Validator;
21
22 /**
23  * Class ProfileCommand
24  *
25  * @package Drupal\Console\Command\Generate
26  */
27
28 class ProfileCommand extends Command
29 {
30     use ConfirmationTrait;
31     use CommandTrait;
32
33     /**
34      * @var Manager
35      */
36     protected $extensionManager;
37
38     /**
39      * @var ProfileGenerator
40      */
41     protected $generator;
42
43     /**
44      * @var StringConverter
45      */
46     protected $stringConverter;
47
48     /**
49      * @var Validator
50      */
51     protected $validator;
52
53     /**
54      * ProfileCommand constructor.
55      *
56      * @param Manager          $extensionManager
57      * @param ProfileGenerator $generator
58      * @param StringConverter  $stringConverter
59      * @param Validator        $validator
60      * @param $appRoot
61      */
62     public function __construct(
63         Manager $extensionManager,
64         ProfileGenerator $generator,
65         StringConverter $stringConverter,
66         Validator $validator,
67         $appRoot
68     ) {
69         $this->extensionManager = $extensionManager;
70         $this->generator = $generator;
71         $this->stringConverter = $stringConverter;
72         $this->validator = $validator;
73         $this->appRoot = $appRoot;
74         parent::__construct();
75     }
76
77     /**
78      * {@inheritdoc}
79      */
80     protected function configure()
81     {
82         $this
83             ->setName('generate:profile')
84             ->setDescription($this->trans('commands.generate.profile.description'))
85             ->setHelp($this->trans('commands.generate.profile.help'))
86             ->addOption(
87                 'profile',
88                 null,
89                 InputOption::VALUE_REQUIRED,
90                 $this->trans('commands.generate.profile.options.profile')
91             )
92             ->addOption(
93                 'machine-name',
94                 null,
95                 InputOption::VALUE_REQUIRED,
96                 $this->trans('commands.generate.profile.options.machine-name')
97             )
98             ->addOption(
99                 'description',
100                 null,
101                 InputOption::VALUE_OPTIONAL,
102                 $this->trans('commands.generate.profile.options.description')
103             )
104             ->addOption(
105                 'core',
106                 null,
107                 InputOption::VALUE_OPTIONAL,
108                 $this->trans('commands.generate.profile.options.core')
109             )
110             ->addOption(
111                 'dependencies',
112                 null,
113                 InputOption::VALUE_OPTIONAL,
114                 $this->trans('commands.generate.profile.options.dependencies'),
115                 ''
116             )
117             ->addOption(
118                 'themes',
119                 null,
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.generate.profile.options.themes'),
122                 ''
123             )
124             ->addOption(
125                 'distribution',
126                 null,
127                 InputOption::VALUE_OPTIONAL,
128                 $this->trans('commands.generate.profile.options.distribution')
129             );
130     }
131
132     /**
133      * {@inheritdoc}
134      */
135     protected function execute(InputInterface $input, OutputInterface $output)
136     {
137         $io = new DrupalStyle($input, $output);
138
139         if (!$this->confirmGeneration($io)) {
140             return 1;
141         }
142
143         $profile = $this->validator->validateModuleName($input->getOption('profile'));
144         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
145         $description = $input->getOption('description');
146         $core = $input->getOption('core');
147         $dependencies = $this->validator->validateExtensions($input->getOption('dependencies'), 'module', $io);
148         $themes = $this->validator->validateExtensions($input->getOption('themes'), 'theme', $io);
149         $distribution = $input->getOption('distribution');
150         $profile_path = $this->appRoot . '/profiles';
151
152         $this->generator->generate(
153             $profile,
154             $machine_name,
155             $profile_path,
156             $description,
157             $core,
158             $dependencies,
159             $themes,
160             $distribution
161         );
162     }
163
164     /**
165      * {@inheritdoc}
166      */
167     protected function interact(InputInterface $input, OutputInterface $output)
168     {
169         $io = new DrupalStyle($input, $output);
170
171         //$stringUtils = $this->getStringHelper();
172         $validators = $this->validator;
173
174         try {
175             // A profile is technically also a module, so we can use the same
176             // validator to check the name.
177             $profile = $input->getOption('profile') ? $validators->validateModuleName($input->getOption('profile')) : null;
178         } catch (\Exception $error) {
179             $io->error($error->getMessage());
180
181             return 1;
182         }
183
184         if (!$profile) {
185             $profile = $io->ask(
186                 $this->trans('commands.generate.profile.questions.profile'),
187                 '',
188                 function ($profile) use ($validators) {
189                     return $validators->validateModuleName($profile);
190                 }
191             );
192             $input->setOption('profile', $profile);
193         }
194
195         try {
196             $machine_name = $input->getOption('machine-name') ? $validators->validateModuleName($input->getOption('machine-name')) : null;
197         } catch (\Exception $error) {
198             $io->error($error->getMessage());
199
200             return 1;
201         }
202
203         if (!$machine_name) {
204             $machine_name = $io->ask(
205                 $this->trans('commands.generate.profile.questions.machine-name'),
206                 $this->stringConverter->createMachineName($profile),
207                 function ($machine_name) use ($validators) {
208                     return $validators->validateMachineName($machine_name);
209                 }
210             );
211             $input->setOption('machine-name', $machine_name);
212         }
213
214         $description = $input->getOption('description');
215         if (!$description) {
216             $description = $io->ask(
217                 $this->trans('commands.generate.profile.questions.description'),
218                 'My Useful Profile'
219             );
220             $input->setOption('description', $description);
221         }
222
223         $core = $input->getOption('core');
224         if (!$core) {
225             $core = $io->ask(
226                 $this->trans('commands.generate.profile.questions.core'),
227                 '8.x'
228             );
229             $input->setOption('core', $core);
230         }
231
232         $dependencies = $input->getOption('dependencies');
233         if (!$dependencies) {
234             if ($io->confirm(
235                 $this->trans('commands.generate.profile.questions.dependencies'),
236                 true
237             )
238             ) {
239                 $dependencies = $io->ask(
240                     $this->trans('commands.generate.profile.options.dependencies'),
241                     ''
242                 );
243             }
244             $input->setOption('dependencies', $dependencies);
245         }
246
247         $distribution = $input->getOption('distribution');
248         if (!$distribution) {
249             if ($io->confirm(
250                 $this->trans('commands.generate.profile.questions.distribution'),
251                 false
252             )
253             ) {
254                 $distribution = $io->ask(
255                     $this->trans('commands.generate.profile.options.distribution'),
256                     'My Kick-ass Distribution'
257                 );
258                 $input->setOption('distribution', $distribution);
259             }
260         }
261     }
262
263     /**
264      * @return ProfileGenerator
265      */
266     protected function createGenerator()
267     {
268         return new ProfileGenerator();
269     }
270 }