5 * Contains \Drupal\Console\Command\Generate\ThemeCommand.
8 namespace Drupal\Console\Command\Generate;
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;
29 * @package Drupal\Console\Command\Generate
31 class ThemeCommand extends Command
33 use ConfirmationTrait;
35 use ThemeBreakpointTrait;
41 protected $extensionManager;
61 protected $themeHandler;
69 * @var StringConverter
71 protected $stringConverter;
74 * ThemeCommand constructor.
76 * @param Manager $extensionManager
77 * @param ThemeGenerator $generator
78 * @param Validator $validator
80 * @param ThemeHandler $themeHandler
82 * @param StringConverter $stringConverter
84 public function __construct(
85 Manager $extensionManager,
86 ThemeGenerator $generator,
89 ThemeHandler $themeHandler,
91 StringConverter $stringConverter
93 $this->extensionManager = $extensionManager;
94 $this->generator = $generator;
95 $this->validator = $validator;
96 $this->appRoot = $appRoot;
97 $this->themeHandler = $themeHandler;
99 $this->stringConverter = $stringConverter;
100 parent::__construct();
107 protected function configure()
110 ->setName('generate:theme')
111 ->setDescription($this->trans('commands.generate.theme.description'))
112 ->setHelp($this->trans('commands.generate.theme.help'))
116 InputOption::VALUE_REQUIRED,
117 $this->trans('commands.generate.theme.options.module')
122 InputOption::VALUE_REQUIRED,
123 $this->trans('commands.generate.theme.options.machine-name')
128 InputOption::VALUE_REQUIRED,
129 $this->trans('commands.generate.theme.options.module-path')
134 InputOption::VALUE_OPTIONAL,
135 $this->trans('commands.generate.theme.options.description')
137 ->addOption('core', null, InputOption::VALUE_OPTIONAL, $this->trans('commands.generate.theme.options.core'))
141 InputOption::VALUE_OPTIONAL,
142 $this->trans('commands.generate.theme.options.package')
147 InputOption::VALUE_OPTIONAL,
148 $this->trans('commands.generate.theme.options.global-library')
153 InputOption::VALUE_OPTIONAL,
154 $this->trans('commands.generate.theme.options.libraries')
159 InputOption::VALUE_OPTIONAL,
160 $this->trans('commands.generate.theme.options.base-theme')
165 InputOption::VALUE_OPTIONAL,
166 $this->trans('commands.generate.theme.options.regions')
171 InputOption::VALUE_OPTIONAL,
172 $this->trans('commands.generate.theme.options.breakpoints')
179 protected function execute(InputInterface $input, OutputInterface $output)
181 $io = new DrupalStyle($input, $output);
183 // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
184 if (!$this->confirmGeneration($io)) {
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);
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');
202 $this->generator->generate(
222 protected function interact(InputInterface $input, OutputInterface $output)
224 $io = new DrupalStyle($input, $output);
227 $theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
228 } catch (\Exception $error) {
229 $io->error($error->getMessage());
235 $validators = $this->validator;
237 $this->trans('commands.generate.theme.questions.theme'),
239 function ($theme) use ($validators) {
240 return $validators->validateModuleName($theme);
243 $input->setOption('theme', $theme);
247 $machine_name = $input->getOption('machine-name') ? $this->validator->validateModule($input->getOption('machine-name')) : null;
248 } catch (\Exception $error) {
249 $io->error($error->getMessage());
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);
262 $input->setOption('machine-name', $machine_name);
265 $theme_path = $input->getOption('theme-path');
267 $drupalRoot = $this->appRoot;
268 $theme_path = $io->ask(
269 $this->trans('commands.generate.theme.questions.theme-path'),
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(
277 $this->trans('commands.generate.theme.errors.directory-exists'),
286 $input->setOption('theme-path', $theme_path);
289 $description = $input->getOption('description');
291 $description = $io->ask(
292 $this->trans('commands.generate.theme.questions.description'),
295 $input->setOption('description', $description);
298 $package = $input->getOption('package');
301 $this->trans('commands.generate.theme.questions.package'),
304 $input->setOption('package', $package);
307 $core = $input->getOption('core');
310 $this->trans('commands.generate.theme.questions.core'),
313 $input->setOption('core', $core);
316 $base_theme = $input->getOption('base-theme');
318 $themes = $this->themeHandler->rebuildThemeData();
319 $themes['false'] ='';
321 uasort($themes, 'system_sort_modules_by_info_name');
323 $base_theme = $io->choiceNoList(
324 $this->trans('commands.generate.theme.options.base-theme'),
327 $input->setOption('base-theme', $base_theme);
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'),
336 $input->setOption('global-library', $global_library);
340 // --libraries option.
341 $libraries = $input->getOption('libraries');
344 $this->trans('commands.generate.theme.questions.library-add'),
348 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::libraryQuestion
349 $libraries = $this->libraryQuestion($io);
350 $input->setOption('libraries', $libraries);
355 $regions = $input->getOption('regions');
358 $this->trans('commands.generate.theme.questions.regions'),
362 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
363 $regions = $this->regionQuestion($io);
364 $input->setOption('regions', $regions);
368 // --breakpoints option.
369 $breakpoints = $input->getOption('breakpoints');
372 $this->trans('commands.generate.theme.questions.breakpoints'),
376 // @see \Drupal\Console\Command\Shared\ThemeRegionTrait::regionQuestion
377 $breakpoints = $this->breakpointQuestion($io);
378 $input->setOption('breakpoints', $breakpoints);