e701435747a59099fa918d6dbb2240f0b467d1e6
[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                 '',
116                 InputOption::VALUE_REQUIRED,
117                 $this->trans('commands.generate.theme.options.module')
118             )
119             ->addOption(
120                 'machine-name',
121                 '',
122                 InputOption::VALUE_REQUIRED,
123                 $this->trans('commands.generate.theme.options.machine-name')
124             )
125             ->addOption(
126                 'theme-path',
127                 '',
128                 InputOption::VALUE_REQUIRED,
129                 $this->trans('commands.generate.theme.options.module-path')
130             )
131             ->addOption(
132                 'description',
133                 '',
134                 InputOption::VALUE_OPTIONAL,
135                 $this->trans('commands.generate.theme.options.description')
136             )
137             ->addOption('core', '', InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.theme.options.core'))
138             ->addOption(
139                 'package',
140                 '',
141                 InputOption::VALUE_OPTIONAL,
142                 $this->trans('commands.generate.theme.options.package')
143             )
144             ->addOption(
145                 'global-library',
146                 '',
147                 InputOption::VALUE_OPTIONAL,
148                 $this->trans('commands.generate.theme.options.global-library')
149             )
150             ->addOption(
151                 'base-theme',
152                 '',
153                 InputOption::VALUE_OPTIONAL,
154                 $this->trans('commands.generate.theme.options.base-theme')
155             )
156             ->addOption(
157                 'regions',
158                 '',
159                 InputOption::VALUE_OPTIONAL,
160                 $this->trans('commands.generate.theme.options.regions')
161             )
162             ->addOption(
163                 'breakpoints',
164                 '',
165                 InputOption::VALUE_OPTIONAL,
166                 $this->trans('commands.generate.theme.options.breakpoints')
167             );
168     }
169
170     /**
171      * {@inheritdoc}
172      */
173     protected function execute(InputInterface $input, OutputInterface $output)
174     {
175         $io = new DrupalStyle($input, $output);
176
177         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
178         if (!$this->confirmGeneration($io)) {
179             return;
180         }
181
182         $theme = $this->validator->validateModuleName($input->getOption('theme'));
183         $theme_path = $this->appRoot . $input->getOption('theme-path');
184         $theme_path = $this->validator->validateModulePath($theme_path, true);
185
186         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
187         $description = $input->getOption('description');
188         $core = $input->getOption('core');
189         $package = $input->getOption('package');
190         $base_theme = $input->getOption('base-theme');
191         $global_library = $input->getOption('global-library');
192         $regions = $input->getOption('regions');
193         $breakpoints = $input->getOption('breakpoints');
194
195         $this->generator->generate(
196             $theme,
197             $machine_name,
198             $theme_path,
199             $description,
200             $core,
201             $package,
202             $base_theme,
203             $global_library,
204             $regions,
205             $breakpoints
206         );
207     }
208
209     /**
210      * {@inheritdoc}
211      */
212     protected function interact(InputInterface $input, OutputInterface $output)
213     {
214         $io = new DrupalStyle($input, $output);
215
216         try {
217             $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
218         } catch (\Exception $error) {
219             $io->error($error->getMessage());
220
221             return;
222         }
223
224         if (!$theme) {
225             $validators = $this->validator;
226             $theme = $io->ask(
227                 $this->trans('commands.generate.theme.questions.theme'),
228                 '',
229                 function ($theme) use ($validators) {
230                     return $validators->validateModuleName($theme);
231                 }
232             );
233             $input->setOption('theme', $theme);
234         }
235
236         try {
237             $machine_name = $input->getOption('machine-name') ? $this->validator->validateModule($input->getOption('machine-name')) : null;
238         } catch (\Exception $error) {
239             $io->error($error->getMessage());
240
241             return;
242         }
243
244         if (!$machine_name) {
245             $machine_name = $io->ask(
246                 $this->trans('commands.generate.module.questions.machine-name'),
247                 $this->stringConverter->createMachineName($theme),
248                 function ($machine_name) use ($validators) {
249                     return $validators->validateMachineName($machine_name);
250                 }
251             );
252             $input->setOption('machine-name', $machine_name);
253         }
254
255         $theme_path = $input->getOption('theme-path');
256         if (!$theme_path) {
257             $drupalRoot = $this->appRoot;
258             $theme_path = $io->ask(
259                 $this->trans('commands.generate.theme.questions.theme-path'),
260                 '/themes/custom',
261                 function ($theme_path) use ($drupalRoot, $machine_name) {
262                     $theme_path = ($theme_path[0] != '/' ? '/' : '') . $theme_path;
263                     $full_path = $drupalRoot . $theme_path . '/' . $machine_name;
264                     if (file_exists($full_path)) {
265                         throw new \InvalidArgumentException(
266                             sprintf(
267                                 $this->trans('commands.generate.theme.errors.directory-exists'),
268                                 $full_path
269                             )
270                         );
271                     } else {
272                         return $theme_path;
273                     }
274                 }
275             );
276             $input->setOption('theme-path', $theme_path);
277         }
278
279         $description = $input->getOption('description');
280         if (!$description) {
281             $description = $io->ask(
282                 $this->trans('commands.generate.theme.questions.description'),
283                 'My Awesome theme'
284             );
285             $input->setOption('description', $description);
286         }
287
288         $package = $input->getOption('package');
289         if (!$package) {
290             $package = $io->ask(
291                 $this->trans('commands.generate.theme.questions.package'),
292                 'Other'
293             );
294             $input->setOption('package', $package);
295         }
296
297         $core = $input->getOption('core');
298         if (!$core) {
299             $core = $io->ask(
300                 $this->trans('commands.generate.theme.questions.core'),
301                 '8.x'
302             );
303             $input->setOption('core', $core);
304         }
305
306         $base_theme = $input->getOption('base-theme');
307         if (!$base_theme) {
308             $themes = $this->themeHandler->rebuildThemeData();
309             $themes['false'] ='';
310
311             uasort($themes, 'system_sort_modules_by_info_name');
312
313             $base_theme = $io->choiceNoList(
314                 $this->trans('commands.generate.theme.options.base-theme'),
315                 array_keys($themes)
316             );
317             $input->setOption('base-theme', $base_theme);
318         }
319
320         $global_library = $input->getOption('global-library');
321         if (!$global_library) {
322             $global_library = $io->ask(
323                 $this->trans('commands.generate.theme.questions.global-library'),
324                 'global-styling'
325             );
326             $input->setOption('global-library', $global_library);
327         }
328
329         // --regions option.
330         $regions = $input->getOption('regions');
331         if (!$regions) {
332             if ($io->confirm(
333                 $this->trans('commands.generate.theme.questions.regions'),
334                 true
335             )
336             ) {
337                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
338                 $regions = $this->regionQuestion($io);
339                 $input->setOption('regions', $regions);
340             }
341         }
342
343         // --breakpoints option.
344         $breakpoints = $input->getOption('breakpoints');
345         if (!$breakpoints) {
346             if ($io->confirm(
347                 $this->trans('commands.generate.theme.questions.breakpoints'),
348                 true
349             )
350             ) {
351                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
352                 $breakpoints = $this->breakpointQuestion($io);
353                 $input->setOption('breakpoints', $breakpoints);
354             }
355         }
356     }
357 }