Skip to content

Commit 464fb82

Browse files
nequetemeenzolutions
authored andcommitted
[generate:theme:setting] New command (#4159)
* Add command generate:theme:setting * update theme name with choices on interact questions * fix update setting file * selection theme name * revert last changes
1 parent 44c8d8c commit 464fb82

File tree

6 files changed

+578
-0
lines changed

6 files changed

+578
-0
lines changed

config/services/generate.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ services:
182182
arguments: ['@console.extension_manager', '@console.theme_generator', '@console.validator', '@app.root', '@theme_handler', '@console.site', '@console.string_converter']
183183
tags:
184184
- { name: drupal.command }
185+
console.generate_setting_theme:
186+
class: Drupal\Console\Command\Generate\ThemeSettingCommand
187+
arguments: ['@console.extension_manager', '@console.theme_setting_generator', '@console.validator', '@app.root', '@theme_handler', '@console.site', '@console.string_converter']
188+
tags:
189+
- { name: drupal.command }
185190
console.generate_twig_extension:
186191
class: Drupal\Console\Command\Generate\TwigExtensionCommand
187192
arguments: ['@console.extension_manager', '@console.twig_extension_generator', '@console.site', '@console.string_converter', '@console.validator', '@console.chain_queue']

config/services/generator.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ services:
176176
arguments: ['@console.extension_manager']
177177
tags:
178178
- { name: drupal.generator }
179+
console.theme_setting_generator:
180+
class: Drupal\Console\Generator\ThemeSettingGenerator
181+
arguments: ['@console.extension_manager']
182+
tags:
183+
- { name: drupal.generator }
179184
console.twig_extension_generator:
180185
class: Drupal\Console\Generator\TwigExtensionGenerator
181186
arguments: ['@console.extension_manager']
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\Console\Command\Generate\ThemeSettingCommand.
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\Generator\ThemeSettingGenerator;
15+
use Drupal\Console\Command\Shared\ConfirmationTrait;
16+
use Drupal\Console\Core\Command\Command;
17+
use Drupal\Console\Extension\Manager;
18+
use Drupal\Console\Utils\Site;
19+
use Drupal\Console\Core\Utils\StringConverter;
20+
use Drupal\Console\Utils\Validator;
21+
use Drupal\Core\Extension\ThemeHandler;
22+
use Webmozart\PathUtil\Path;
23+
use Drupal\Console\Command\Shared\ThemeTrait;
24+
use Symfony\Component\Filesystem\Filesystem;
25+
use Drupal\Console\Core\Style\DrupalStyle;
26+
27+
28+
/**
29+
* Class ThemeSettingCommand
30+
*
31+
* @package Drupal\Console\Command\Generate
32+
*/
33+
class ThemeSettingCommand extends Command
34+
{
35+
use ConfirmationTrait;
36+
use ArrayInputTrait;
37+
use ThemeTrait;
38+
39+
/**
40+
* @var Manager
41+
*/
42+
protected $extensionManager;
43+
44+
/**
45+
* @var ThemeSettingGenerator
46+
*/
47+
protected $generator;
48+
49+
/**
50+
* @var Validator
51+
*/
52+
protected $validator;
53+
54+
/**
55+
* @var string
56+
*/
57+
protected $appRoot;
58+
59+
/**
60+
* @var ThemeHandler
61+
*/
62+
protected $themeHandler;
63+
64+
/**
65+
* @var Site
66+
*/
67+
protected $site;
68+
69+
/**
70+
* @var StringConverter
71+
*/
72+
protected $stringConverter;
73+
74+
/**
75+
* ThemeSettingCommand constructor.
76+
*
77+
* @param Manager $extensionManager
78+
* @param ThemeSettingGenerator $generator
79+
* @param Validator $validator
80+
* @param $appRoot
81+
* @param ThemeHandler $themeHandler
82+
* @param Site $site
83+
* @param StringConverter $stringConverter
84+
*/
85+
public function __construct(
86+
Manager $extensionManager,
87+
ThemeSettingGenerator $generator,
88+
Validator $validator,
89+
$appRoot,
90+
ThemeHandler $themeHandler,
91+
Site $site,
92+
StringConverter $stringConverter
93+
) {
94+
$this->extensionManager = $extensionManager;
95+
$this->generator = $generator;
96+
$this->validator = $validator;
97+
$this->appRoot = $appRoot;
98+
$this->themeHandler = $themeHandler;
99+
$this->site = $site;
100+
$this->stringConverter = $stringConverter;
101+
parent::__construct();
102+
}
103+
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
protected function configure()
109+
{
110+
$this
111+
->setName('generate:theme:setting')
112+
->setDescription($this->trans('commands.generate.theme.setting.description'))
113+
->setHelp($this->trans('commands.generate.theme.setting.help'))
114+
->addOption(
115+
'theme',
116+
null,
117+
InputOption::VALUE_REQUIRED,
118+
$this->trans('commands.generate.theme.setting.options.theme')
119+
)
120+
->addOption(
121+
'theme-path',
122+
null,
123+
InputOption::VALUE_REQUIRED,
124+
$this->trans('commands.generate.theme.setting.options.theme-path')
125+
)
126+
->addOption(
127+
'favicon',
128+
null,
129+
InputOption::VALUE_OPTIONAL,
130+
$this->trans('commands.generate.theme.setting.options.favicon')
131+
)
132+
->addOption(
133+
'comment-user-picture',
134+
null,
135+
InputOption::VALUE_OPTIONAL,
136+
$this->trans('commands.generate.theme.setting.options.comment-user-picture')
137+
)
138+
->addOption(
139+
'comment-user-verification',
140+
null,
141+
InputOption::VALUE_OPTIONAL,
142+
$this->trans('commands.generate.theme.setting.options.comment-user-verification')
143+
)
144+
->addOption(
145+
'node-user-picture',
146+
null,
147+
InputOption::VALUE_OPTIONAL,
148+
$this->trans('commands.generate.theme.setting.options.node-user-picture')
149+
)
150+
->addOption(
151+
'logo',
152+
null,
153+
InputOption::VALUE_OPTIONAL,
154+
$this->trans('commands.generate.theme.setting.options.logo')
155+
)
156+
->addOption(
157+
'merge-existing-file',
158+
null,
159+
InputOption::VALUE_OPTIONAL,
160+
$this->trans('commands.generate.theme.setting.options.merge-existing-file')
161+
)
162+
->setAliases(['gts']);
163+
}
164+
165+
/**
166+
* {@inheritdoc}
167+
*/
168+
protected function execute(InputInterface $input, OutputInterface $output)
169+
{
170+
$theme = $this->validator->validateModuleName($input->getOption('theme'));
171+
$theme_path = $input->getOption('theme-path');
172+
if (is_null($theme_path)) {
173+
$uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST);
174+
$defaultThemePath = 'themes/custom';
175+
$theme_path = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultThemePath : $defaultThemePath;
176+
}
177+
$theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
178+
$theme_path = $this->validator->validateModulePath($theme_path, true);
179+
180+
$favicon = $input->getOption('favicon');
181+
$commentUserPicture = $input->getOption('comment-user-picture');
182+
$commentUserVerification = $input->getOption('comment-user-verification');
183+
$nodeUserPicture = $input->getOption('node-user-picture');
184+
$logo = $input->getOption('logo');
185+
$mergeExistingFile = $input->getOption('merge-existing-file');
186+
$this->generator->setIo($this->getIo());
187+
return $this->generator->generate(
188+
[
189+
'theme' => $theme,
190+
'theme_path' => $theme_path,
191+
'favicon' => $favicon,
192+
'commentUserPicture' => $commentUserPicture,
193+
'commentUserVerification' => $commentUserVerification,
194+
'nodeUserPicture' => $nodeUserPicture,
195+
'logo' => $logo,
196+
'merge-existing-file' => (bool)$mergeExistingFile
197+
]
198+
);
199+
}
200+
201+
/**
202+
* {@inheritdoc}
203+
*/
204+
protected function interact(InputInterface $input, OutputInterface $output)
205+
{
206+
// --theme option
207+
try {
208+
$theme = $input->getOption('theme') ? $this->validator->validateModuleName($input->getOption('theme')) : null;
209+
} catch (\Exception $error) {
210+
$this->getIo()->error($error->getMessage());
211+
return 1;
212+
}
213+
if (!$theme) {
214+
// @see Drupal\Console\Command\Shared\ThemeTrait::themeQuestion
215+
$theme = $this->themeQuestion();
216+
$theme_list = $this->extensionManager->discoverThemes()
217+
->showInstalled()
218+
->showNoCore()
219+
->getList();
220+
$input->setOption('theme', $theme);
221+
222+
}
223+
224+
// --theme-path option
225+
$theme_path = $input->getOption('theme-path');
226+
if (!$theme_path) {
227+
$theme_path = $this->appRoot.'/'.$theme_list[$theme]->getPath();
228+
$input->setOption('theme-path', $theme_path);
229+
}
230+
231+
// --favicon option
232+
$favicon = $input->getOption('favicon');
233+
if (!$favicon) {
234+
$favicon = $this->getIo()->choice(
235+
$this->trans('commands.generate.theme.setting.questions.favicon'),
236+
['true', 'false'],
237+
'true'
238+
);
239+
$input->setOption('favicon', $favicon);
240+
}
241+
242+
// --comment-user-picture option
243+
$commentUserPicture = $input->getOption('comment-user-picture');
244+
if (!$commentUserPicture) {
245+
$commentUserPicture = $this->getIo()->choice(
246+
$this->trans('commands.generate.theme.setting.questions.comment-user-picture'),
247+
['true', 'false'],
248+
'true'
249+
);
250+
$input->setOption('comment-user-picture', $commentUserPicture);
251+
}
252+
253+
// --comment-user-verification option
254+
$commentUserVerification = $input->getOption('comment-user-verification');
255+
if (!$commentUserVerification) {
256+
$commentUserVerification = $this->getIo()->choice(
257+
$this->trans('commands.generate.theme.setting.questions.comment-user-verification'),
258+
['true', 'false'],
259+
'true'
260+
);
261+
$input->setOption('comment-user-verification', $commentUserVerification);
262+
}
263+
264+
// --node-user-picture option
265+
$nodeUserPicture = $input->getOption('node-user-picture');
266+
if (!$nodeUserPicture) {
267+
$nodeUserPicture = $this->getIo()->choice(
268+
$this->trans('commands.generate.theme.setting.questions.node-user-picture'),
269+
['true', 'false'],
270+
'true'
271+
);
272+
$input->setOption('node-user-picture', $nodeUserPicture);
273+
}
274+
275+
// --logo option
276+
$logo = $input->getOption('logo');
277+
if (!$logo) {
278+
$logo = $this->getIo()->choice(
279+
$this->trans('commands.generate.theme.setting.questions.logo'),
280+
['true', 'false'],
281+
'true'
282+
);
283+
$input->setOption('logo', $logo);
284+
}
285+
286+
// --merge-existing-file
287+
$mergeExistingFile = $input->getOption('merge-existing-file');
288+
if (!$mergeExistingFile) {
289+
$file_path = $theme_path.'/config/install/'.$theme.'.settings.yml';
290+
$filesystem = new Filesystem();
291+
if ($filesystem->exists($file_path)) {
292+
$data_cont = file_get_contents($file_path);
293+
if (strlen($data_cont)>0) {
294+
$mergeExistingFile = $this->getIo()->choice(
295+
$this->trans('commands.generate.theme.setting.questions.merge-existing-file'),
296+
['true', 'false'],
297+
'true'
298+
);
299+
$input->setOption('merge-existing-file', $mergeExistingFile);
300+
} else {
301+
$input->setOption('merge-existing-file', 'false');
302+
}
303+
} else {
304+
$input->setOption('merge-existing-file', 'false');
305+
}
306+
} else {
307+
$input->setOption('merge-existing-file', 'false');
308+
}
309+
$io = new DrupalStyle($input, $output);
310+
311+
}
312+
}

0 commit comments

Comments
 (0)