Security update for Core, with self-updated composer
[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 Drupal\Console\Command\Shared\ArrayInputTrait;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ThemeRegionTrait;
15 use Drupal\Console\Command\Shared\ThemeBreakpointTrait;
16 use Drupal\Console\Generator\ThemeGenerator;
17 use Drupal\Console\Command\Shared\ConfirmationTrait;
18 use Drupal\Console\Core\Command\Command;
19 use Drupal\Console\Extension\Manager;
20 use Drupal\Console\Utils\Site;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Utils\Validator;
23 use Drupal\Core\Extension\ThemeHandler;
24 use Webmozart\PathUtil\Path;
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 ArrayInputTrait;
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.theme')
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.theme-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 | InputOption::VALUE_IS_ARRAY,
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 | InputOption::VALUE_IS_ARRAY,
166                 $this->trans('commands.generate.theme.options.regions')
167             )
168             ->addOption(
169                 'breakpoints',
170                 null,
171                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
172                 $this->trans('commands.generate.theme.options.breakpoints')
173             )
174             ->setAliases(['gt']);
175     }
176
177     /**
178      * {@inheritdoc}
179      */
180     protected function execute(InputInterface $input, OutputInterface $output)
181     {
182         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
183         if (!$this->confirmOperation()) {
184             return 1;
185         }
186
187         $theme = $this->validator->validateModuleName($input->getOption('theme'));
188         // Get the profile path and define a profile path if it is null
189         // Check that it is an absolute path or otherwise create an absolute path using appRoot
190         $theme_path = $input->getOption('theme-path');
191         $theme_path = $theme_path == null ? 'themes/custom' : $theme_path;
192         $theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
193         $theme_path = $this->validator->validateModulePath($theme_path, true);
194
195         $machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
196         $description = $input->getOption('description');
197         $core = $input->getOption('core');
198         $package = $input->getOption('package');
199         $base_theme = $input->getOption('base-theme');
200         $global_library = $input->getOption('global-library');
201         $libraries = $input->getOption('libraries');
202         $regions = $input->getOption('regions');
203         $breakpoints = $input->getOption('breakpoints');
204         $noInteraction = $input->getOption('no-interaction');
205
206         // Parse nested data.
207         if ($noInteraction) {
208             $libraries = $this->explodeInlineArray($libraries);
209             $regions = $this->explodeInlineArray($regions);
210             $breakpoints = $this->explodeInlineArray($breakpoints);
211         }
212
213         $this->generator->generate([
214             'theme' => $theme,
215             'machine_name' => $machine_name,
216             'dir' => $theme_path,
217             'core' => $core,
218             'description' => $description,
219             'package' => $package,
220             'base_theme' => $base_theme,
221             'global_library' => $global_library,
222             'libraries' => $libraries,
223             'regions' => $regions,
224             'breakpoints' => $breakpoints,
225         ]);
226
227
228         return 0;
229     }
230
231     /**
232      * {@inheritdoc}
233      */
234     protected function interact(InputInterface $input, OutputInterface $output)
235     {
236         try {
237             $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
238         } catch (\Exception $error) {
239             $this->getIo()->error($error->getMessage());
240
241             return 1;
242         }
243
244         if (!$theme) {
245             $validators = $this->validator;
246             $theme = $this->getIo()->ask(
247                 $this->trans('commands.generate.theme.questions.theme'),
248                 '',
249                 function ($theme) use ($validators) {
250                     return $validators->validateModuleName($theme);
251                 }
252             );
253             $input->setOption('theme', $theme);
254         }
255
256         try {
257             $machine_name = $input->getOption('machine-name') ? $this->validator->validateModuleName($input->getOption('machine-name')) : null;
258         } catch (\Exception $error) {
259             $this->getIo()->error($error->getMessage());
260
261             return 1;
262         }
263
264         if (!$machine_name) {
265             $machine_name = $this->getIo()->ask(
266                 $this->trans('commands.generate.theme.questions.machine-name'),
267                 $this->stringConverter->createMachineName($theme),
268                 function ($machine_name) use ($validators) {
269                     return $validators->validateMachineName($machine_name);
270                 }
271             );
272             $input->setOption('machine-name', $machine_name);
273         }
274
275         $theme_path = $input->getOption('theme-path');
276         if (!$theme_path) {
277             $theme_path = $this->getIo()->ask(
278                 $this->trans('commands.generate.theme.questions.theme-path'),
279                 'themes/custom',
280                 function ($theme_path) use ($machine_name) {
281                     $fullPath = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
282                     $fullPath = $fullPath.'/'.$machine_name;
283                     if (file_exists($fullPath)) {
284                         throw new \InvalidArgumentException(
285                             sprintf(
286                                 $this->trans('commands.generate.theme.errors.directory-exists'),
287                                 $fullPath
288                             )
289                         );
290                     } else {
291                         return $theme_path;
292                     }
293                 }
294             );
295             $input->setOption('theme-path', $theme_path);
296         }
297
298         $description = $input->getOption('description');
299         if (!$description) {
300             $description = $this->getIo()->ask(
301                 $this->trans('commands.generate.theme.questions.description'),
302                 $this->trans('commands.generate.theme.suggestions.my-awesome-theme')
303             );
304             $input->setOption('description', $description);
305         }
306
307         $package = $input->getOption('package');
308         if (!$package) {
309             $package = $this->getIo()->ask(
310                 $this->trans('commands.generate.theme.questions.package'),
311                 $this->trans('commands.generate.theme.suggestions.other')
312             );
313             $input->setOption('package', $package);
314         }
315
316         $core = $input->getOption('core');
317         if (!$core) {
318             $core = $this->getIo()->ask(
319                 $this->trans('commands.generate.theme.questions.core'),
320                 '8.x'
321             );
322             $input->setOption('core', $core);
323         }
324
325         $base_theme = $input->getOption('base-theme');
326         if (!$base_theme) {
327             $themes = $this->themeHandler->rebuildThemeData();
328             $themes['false'] ='';
329
330             uasort($themes, 'system_sort_modules_by_info_name');
331
332             $base_theme = $this->getIo()->choiceNoList(
333                 $this->trans('commands.generate.theme.options.base-theme'),
334                 array_keys($themes)
335             );
336             $input->setOption('base-theme', $base_theme);
337         }
338
339         $global_library = $input->getOption('global-library');
340         if (!$global_library) {
341             $global_library = $this->getIo()->ask(
342                 $this->trans('commands.generate.theme.questions.global-library'),
343                 'global-styling'
344             );
345             $input->setOption('global-library', $global_library);
346         }
347
348
349         // --libraries option.
350         $libraries = $input->getOption('libraries');
351         if (!$libraries) {
352             if ($this->getIo()->confirm(
353                 $this->trans('commands.generate.theme.questions.library-add'),
354                 true
355             )
356             ) {
357                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion
358                 $libraries = $this->libraryQuestion();
359             }
360         } else {
361             $libraries = $this->explodeInlineArray($libraries);
362         }
363         $input->setOption('libraries', $libraries);
364
365         // --regions option.
366         $regions = $input->getOption('regions');
367         if (!$regions) {
368             if ($this->getIo()->confirm(
369                 $this->trans('commands.generate.theme.questions.regions'),
370                 true
371             )
372             ) {
373                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
374                 $regions = $this->regionQuestion();
375             }
376         } else {
377             $regions = $this->explodeInlineArray($regions);
378         }
379         $input->setOption('regions', $regions);
380
381         // --breakpoints option.
382         $breakpoints = $input->getOption('breakpoints');
383         if (!$breakpoints) {
384             if ($this->getIo()->confirm(
385                 $this->trans('commands.generate.theme.questions.breakpoints'),
386                 true
387             )
388             ) {
389                 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
390                 $breakpoints = $this->breakpointQuestion();
391             }
392         } else {
393             $breakpoints = $this->explodeInlineArray($breakpoints);
394         }
395         $input->setOption('breakpoints', $breakpoints);
396     }
397 }