Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / ThemeCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\ThemeCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ThemeRegionTrait;
14 use Drupal\Console\Command\Shared\ThemeBreakpointTrait;
15 use Drupal\Console\Generator\ThemeGenerator;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Utils\Site;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Core\Command\Shared\CommandTrait;
23 use Drupal\Console\Utils\Validator;
24 use Drupal\Core\Extension\ThemeHandler;
25
26 /**
27  * Class ThemeCommand
28  *
29  * @package Drupal\Console\Command\Generate
30  */
31 class ThemeCommand extends Command
32 {
33     use ConfirmationTrait;
34     use ThemeRegionTrait;
35     use ThemeBreakpointTrait;
36     use CommandTrait;
37
38     /**
39  * @var Manager
40 */
41     protected $extensionManager;
42
43     /**
44  * @var ThemeGenerator
45 */
46     protected $generator;
47
48     /**
49  * @var Validator
50 */
51     protected $validator;
52
53     /**
54      * @var string
55      */
56     protected $appRoot;
57
58     /**
59      * @var ThemeHandler
60      */
61     protected $themeHandler;
62
63     /**
64      * @var Site
65      */
66     protected $site;
67
68     /**
69      * @var StringConverter
70      */
71     protected $stringConverter;
72
73     /**
74      * ThemeCommand constructor.
75      *
76      * @param Manager         $extensionManager
77      * @param ThemeGenerator  $generator
78      * @param Validator       $validator
79      * @param $appRoot
80      * @param ThemeHandler    $themeHandler
81      * @param Site            $site
82      * @param StringConverter $stringConverter
83      */
84     public function __construct(
85         Manager $extensionManager,
86         ThemeGenerator $generator,
87         Validator $validator,
88         $appRoot,
89         ThemeHandler $themeHandler,
90         Site $site,
91         StringConverter $stringConverter
92     ) {
93         $this->extensionManager = $extensionManager;
94         $this->generator = $generator;
95         $this->validator = $validator;
96         $this->appRoot = $appRoot;
97         $this->themeHandler = $themeHandler;
98         $this->site = $site;
99         $this->stringConverter = $stringConverter;
100         parent::__construct();
101     }
102
103
104     /**
105      * {@inheritdoc}
106      */
107     protected function configure()
108     {
109         $this
110             ->setName('generate:theme')
111             ->setDescription($this->trans('commands.generate.theme.description'))
112             ->setHelp($this->trans('commands.generate.theme.help'))
113             ->addOption(
114                 'theme',
115                 null,
116                 InputOption::VALUE_REQUIRED,
117                 $this->trans('commands.generate.theme.options.module')
118             )
119             ->addOption(
120                 'machine-name',
121                 null,
122                 InputOption::VALUE_REQUIRED,
123                 $this->trans('commands.generate.theme.options.machine-name')
124             )
125             ->addOption(
126                 'theme-path',
127                 null,
128                 InputOption::VALUE_REQUIRED,
129                 $this->trans('commands.generate.theme.options.module-path')
130             )
131             ->addOption(
132                 'description',
133                 null,
134                 InputOption::VALUE_OPTIONAL,
135                 $this->trans('commands.generate.theme.options.description')
136             )
137             ->addOption('core', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.theme.options.core'))
138             ->addOption(
139                 'package',
140                 null,
141                 InputOption::VALUE_OPTIONAL,
142                 $this->trans('commands.generate.theme.options.package')
143             )
144             ->addOption(
145                 'global-library',
146                 null,
147                 InputOption::VALUE_OPTIONAL,
148                 $this->trans('commands.generate.theme.options.global-library')
149             )
150             ->addOption(
151                 'libraries',
152                 null,
153                 InputOption::VALUE_OPTIONAL,
154                 $this->trans('commands.generate.theme.options.libraries')
155             )
156             ->addOption(
157                 'base-theme',
158                 null,
159                 InputOption::VALUE_OPTIONAL,
160                 $this->trans('commands.generate.theme.options.base-theme')
161             )
162             ->addOption(
163                 'regions',
164                 null,
165                 InputOption::VALUE_OPTIONAL,
166                 $this->trans('commands.generate.theme.options.regions')
167             )
168             ->addOption(
169                 'breakpoints',
170                 null,
171                 InputOption::VALUE_OPTIONAL,
172                 $this->trans('commands.generate.theme.options.breakpoints')
173             );
174     }
175
176     /**
177      * {@inheritdoc}
178      */
179     protected function execute(InputInterface $input, OutputInterface $output)
180     {
181         $io = new DrupalStyle($input, $output);
182
183         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
184         if (!$this->confirmGeneration($io)) {
185             return 1;
186         }
187
188         $theme = $this->validator->validateModuleName($input->getOption('theme'));
189         $theme_path = $this->appRoot . $input->getOption('theme-path');
190         $theme_path = $this->validator->validateModulePath($theme_path, true);
191
192         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
193         $description = $input->getOption('description');
194         $core = $input->getOption('core');
195         $package = $input->getOption('package');
196         $base_theme = $input->getOption('base-theme');
197         $global_library = $input->getOption('global-library');
198         $libraries = $input->getOption('libraries');
199         $regions = $input->getOption('regions');
200         $breakpoints = $input->getOption('breakpoints');
201
202         $this->generator->generate(
203             $theme,
204             $machine_name,
205             $theme_path,
206             $description,
207             $core,
208             $package,
209             $base_theme,
210             $global_library,
211             $libraries,
212             $regions,
213             $breakpoints
214         );
215
216         return 0;
217     }
218
219     /**
220      * {@inheritdoc}
221      */
222     protected function interact(InputInterface $input, OutputInterface $output)
223     {
224         $io = new DrupalStyle($input, $output);
225
226         try {
227             $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
228         } catch (\Exception $error) {
229             $io->error($error->getMessage());
230
231             return 1;
232         }
233
234         if (!$theme) {
235             $validators = $this->validator;
236             $theme = $io->ask(
237                 $this->trans('commands.generate.theme.questions.theme'),
238                 '',
239                 function ($theme) use ($validators) {
240                     return $validators->validateModuleName($theme);
241                 }
242             );
243             $input->setOption('theme', $theme);
244         }
245
246         try {
247             $machine_name = $input->getOption('machine-name') ? $this->validator->validateModule($input->getOption('machine-name')) : null;
248         } catch (\Exception $error) {
249             $io->error($error->getMessage());
250
251             return 1;
252         }
253
254         if (!$machine_name) {
255             $machine_name = $io->ask(
256                 $this->trans('commands.generate.module.questions.machine-name'),
257                 $this->stringConverter->createMachineName($theme),
258                 function ($machine_name) use ($validators) {
259                     return $validators->validateMachineName($machine_name);
260                 }
261             );
262             $input->setOption('machine-name', $machine_name);
263         }
264
265         $theme_path = $input->getOption('theme-path');
266         if (!$theme_path) {
267             $drupalRoot = $this->appRoot;
268             $theme_path = $io->ask(
269                 $this->trans('commands.generate.theme.questions.theme-path'),
270                 '/themes/custom',
271                 function ($theme_path) use ($drupalRoot, $machine_name) {
272                     $theme_path = ($theme_path[0] != '/' ? '/' : '') . $theme_path;
273                     $full_path = $drupalRoot . $theme_path . '/' . $machine_name;
274                     if (file_exists($full_path)) {
275                         throw new \InvalidArgumentException(
276                             sprintf(
277                                 $this->trans('commands.generate.theme.errors.directory-exists'),
278                                 $full_path
279                             )
280                         );
281                     } else {
282                         return $theme_path;
283                     }
284                 }
285             );
286             $input->setOption('theme-path', $theme_path);
287         }
288
289         $description = $input->getOption('description');
290         if (!$description) {
291             $description = $io->ask(
292                 $this->trans('commands.generate.theme.questions.description'),
293                 'My Awesome theme'
294             );
295             $input->setOption('description', $description);
296         }
297
298         $package = $input->getOption('package');
299         if (!$package) {
300             $package = $io->ask(
301                 $this->trans('commands.generate.theme.questions.package'),
302                 'Other'
303             );
304             $input->setOption('package', $package);
305         }
306
307         $core = $input->getOption('core');
308         if (!$core) {
309             $core = $io->ask(
310                 $this->trans('commands.generate.theme.questions.core'),
311                 '8.x'
312             );
313             $input->setOption('core', $core);
314         }
315
316         $base_theme = $input->getOption('base-theme');
317         if (!$base_theme) {
318             $themes = $this->themeHandler->rebuildThemeData();
319             $themes['false'] ='';
320
321             uasort($themes, 'system_sort_modules_by_info_name');
322
323             $base_theme = $io->choiceNoList(
324                 $this->trans('commands.generate.theme.options.base-theme'),
325                 array_keys($themes)
326             );
327             $input->setOption('base-theme', $base_theme);
328         }
329
330         $global_library = $input->getOption('global-library');
331         if (!$global_library) {
332             $global_library = $io->ask(
333                 $this->trans('commands.generate.theme.questions.global-library'),
334                 'global-styling'
335             );
336             $input->setOption('global-library', $global_library);
337         }
338
339
340         // --libraries option.
341         $libraries = $input->getOption('libraries');
342         if (!$libraries) {
343             if ($io->confirm(
344                 $this->trans('commands.generate.theme.questions.library-add'),
345                 true
346             )
347             ) {
348                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion
349                 $libraries = $this->libraryQuestion($io);
350                 $input->setOption('libraries', $libraries);
351             }
352         }
353
354         // --regions option.
355         $regions = $input->getOption('regions');
356         if (!$regions) {
357             if ($io->confirm(
358                 $this->trans('commands.generate.theme.questions.regions'),
359                 true
360             )
361             ) {
362                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
363                 $regions = $this->regionQuestion($io);
364                 $input->setOption('regions', $regions);
365             }
366         }
367
368         // --breakpoints option.
369         $breakpoints = $input->getOption('breakpoints');
370         if (!$breakpoints) {
371             if ($io->confirm(
372                 $this->trans('commands.generate.theme.questions.breakpoints'),
373                 true
374             )
375             ) {
376                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
377                 $breakpoints = $this->breakpointQuestion($io);
378                 $input->setOption('breakpoints', $breakpoints);
379             }
380         }
381     }
382 }