4 * Contains Drupal\metatag\Command\GenerateTagCommand.
7 namespace Drupal\metatag\Command;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Output\OutputInterface;
11 use Symfony\Component\Console\Command\Command;
12 use Drupal\Console\Core\Command\Shared\CommandTrait;
13 use Drupal\Console\Core\Style\DrupalStyle;
14 use Drupal\metatag\MetatagManager;
15 use Drupal\metatag\Generator\MetatagTagGenerator;
16 use Drupal\Console\Command\Shared\ModuleTrait;
17 use Drupal\Console\Command\Shared\FormTrait;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
19 use Symfony\Component\Console\Input\InputOption;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\StringConverter;
22 use Drupal\Console\Core\Utils\ChainQueue;
25 * Class GenerateTagCommand.
27 * Generate a Metatag tag plugin.
29 * @package Drupal\metatag
31 class GenerateTagCommand extends Command {
36 use ConfirmationTrait;
41 protected $metatagManager;
44 * @var MetatagTagGenerator
49 protected $extensionManager;
52 * @var StringConverter
54 protected $stringConverter;
59 protected $chainQueue;
62 * GenerateTagCommand constructor.
64 * @param MetatagManager $metatagManager
65 * @param MetatagTagGenerator $generator
66 * @param Manager $extensionManager
67 * @param StringConverter $stringConverter
68 * @param ChainQueue $chainQueue
70 public function __construct(
71 MetatagManager $metatagManager,
72 MetatagTagGenerator $generator,
73 Manager $extensionManager,
74 StringConverter $stringConverter,
75 ChainQueue $chainQueue
77 $this->metatagManager = $metatagManager;
78 $this->generator = $generator;
79 $this->extensionManager = $extensionManager;
80 $this->stringConverter = $stringConverter;
81 $this->chainQueue = $chainQueue;
83 parent::__construct();
89 protected function configure() {
91 ->setName('generate:plugin:metatag:tag')
92 ->setDescription($this->trans('commands.generate.metatag.tag.description'))
93 ->setHelp($this->trans('commands.generate.metatag.tag.help'))
94 ->addOption('base_class', '', InputOption::VALUE_REQUIRED,
95 $this->trans('commands.common.options.base_class'))
96 ->addOption('module', '', InputOption::VALUE_REQUIRED,
97 $this->trans('commands.common.options.module'))
98 ->addOption('name', '', InputOption::VALUE_REQUIRED,
99 $this->trans('commands.generate.metatag.tag.options.name'))
100 ->addOption('label', '', InputOption::VALUE_REQUIRED,
101 $this->trans('commands.generate.metatag.tag.options.label'))
102 ->addOption('description', '', InputOption::VALUE_OPTIONAL,
103 $this->trans('commands.generate.metatag.tag.options.description'))
104 ->addOption('plugin-id', '', InputOption::VALUE_REQUIRED,
105 $this->trans('commands.generate.metatag.tag.options.plugin_id'))
106 ->addOption('class-name', '', InputOption::VALUE_REQUIRED,
107 $this->trans('commands.generate.metatag.tag.options.class_name'))
108 ->addOption('group', '', InputOption::VALUE_REQUIRED,
109 $this->trans('commands.generate.metatag.tag.options.group'))
110 ->addOption('weight', '', InputOption::VALUE_REQUIRED,
111 $this->trans('commands.generate.metatag.tag.options.weight'))
112 ->addOption('type', '', InputOption::VALUE_REQUIRED,
113 $this->trans('commands.generate.metatag.tag.options.type'))
114 ->addOption('secure', '', InputOption::VALUE_REQUIRED,
115 $this->trans('commands.generate.metatag.tag.options.secure'))
116 ->addOption('multiple', '', InputOption::VALUE_REQUIRED,
117 $this->trans('commands.generate.metatag.tag.options.multiple'))
124 protected function execute(InputInterface $input, OutputInterface $output) {
125 $io = new DrupalStyle($input, $output);
127 // @see use Drupal\Console\Command\ConfirmationTrait::confirmGeneration
128 if (!$this->confirmGeneration($io)) {
132 $base_class = $input->getOption('base_class');
133 $module = $input->getOption('module');
134 $name = $input->getOption('name');
135 $label = $input->getOption('label');
136 $description = $input->getOption('description');
137 $plugin_id = $input->getOption('plugin-id');
138 $class_name = $input->getOption('class-name');
139 $group = $input->getOption('group');
140 $weight = $input->getOption('weight');
141 $type = $input->getOption('type');
142 $secure = $input->getOption('secure');
143 $multiple = $input->getOption('multiple');
146 ->generate($base_class, $module, $name, $label, $description, $plugin_id, $class_name, $group, $weight, $type, $secure, $multiple);
148 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
154 protected function interact(InputInterface $input, OutputInterface $output) {
156 $io = new DrupalStyle($input, $output);
163 // ToDo: Take this from typed data, so it can be extended?
172 // --base_class option.
173 // @todo Turn this into a choice() option.
174 $base_class = $input->getOption('base_class');
175 if (empty($base_class)) {
176 $base_class = $io->ask(
177 $this->trans('commands.generate.metatag.tag.questions.base_class'),
181 $input->setOption('base_class', $base_class);
184 $module = $input->getOption('module');
185 if (empty($module)) {
186 // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
187 $module = $this->moduleQuestion($io);
189 $input->setOption('module', $module);
192 // @todo Add validation.
193 $name = $input->getOption('name');
196 $this->trans('commands.generate.metatag.tag.questions.name')
199 $input->setOption('name', $name);
202 $label = $input->getOption('label');
205 $this->trans('commands.generate.metatag.tag.questions.label'),
209 $input->setOption('label', $label);
211 // --description option.
212 $description = $input->getOption('description');
213 if (empty($description)) {
214 $description = $io->ask(
215 $this->trans('commands.generate.metatag.tag.questions.description')
218 $input->setOption('description', $description);
220 // --plugin-id option.
221 $plugin_id = $input->getOption('plugin-id');
222 if (empty($plugin_id)) {
223 $plugin_id = $this->nameToPluginId($name);
224 $plugin_id = $io->ask(
225 $this->trans('commands.generate.metatag.tag.questions.plugin_id'),
229 $input->setOption('plugin-id', $plugin_id);
231 // --class-name option.
232 $class_name = $input->getOption('class-name');
233 if (empty($class_name)) {
234 $class_name = $this->nameToClassName($name);
235 $class_name = $io->ask(
236 $this->trans('commands.generate.metatag.tag.questions.class_name'),
240 $input->setOption('class-name', $class_name);
243 $group = $input->getOption('group');
245 $groups = $this->getGroups();
246 $group = $io->choice(
247 $this->trans('commands.generate.metatag.tag.questions.group'),
251 $input->setOption('group', $group);
254 // @todo Automatically get the next integer value based upon the current
256 $weight = $input->getOption('weight');
257 if (is_null($weight)) {
259 $this->trans('commands.generate.metatag.tag.questions.weight'),
263 $input->setOption('weight', $weight);
266 // @todo Turn this into an option.
267 $type = $input->getOption('type');
268 if (is_null($type)) {
270 $this->trans('commands.generate.metatag.tag.questions.type'),
275 $input->setOption('type', $type);
278 // @todo Turn this into an option.
279 $secure = $input->getOption('secure');
280 if (is_null($secure)) {
281 $secure = $io->choice(
282 $this->trans('commands.generate.metatag.tag.questions.secure'),
287 $input->setOption('secure', $secure);
289 // --multiple option.
290 $multiple = $input->getOption('multiple');
291 if (is_null($multiple)) {
292 $multiple = $io->choice(
293 $this->trans('commands.generate.metatag.tag.questions.multiple'),
298 $input->setOption('multiple', $multiple);
302 * Convert the meta tag's name to a plugin ID.
304 * @param string $name
305 * The meta tag name to convert.
308 * The original string with all non-alphanumeric characters converted to
311 private function nameToPluginId($name) {
312 return $this->stringConverter->createMachineName($name);
316 * Convert the meta tag's name to a class name.
318 * @param string $name
319 * The meta tag name to convert.
322 * The original string with all non-alphanumeric characters removed and
323 * converted to CamelCase.
325 private function nameToClassName($name) {
326 return $this->stringConverter->humanToCamelCase($name);
330 * All of the meta tag groups.
333 * A list of the available groups.
335 private function getGroups() {
336 return array_keys($this->metatagManager->sortedGroups());
340 * Confirm that a requested group exists.
342 * @param string $group
343 * A group's machine name.
346 * The group's name, if available, otherwise an empty string.
348 private function validateGroupExist($group) {
349 $groups = $this->getGroups();
350 if (isset($groups[$group])) {