3 namespace Drupal\metatag\Command;
5 use Drupal\Console\Command\Shared\ConfirmationTrait;
6 use Drupal\Console\Command\Shared\FormTrait;
7 use Drupal\Console\Command\Shared\ModuleTrait;
8 use Drupal\Console\Core\Command\Shared\CommandTrait;
9 use Drupal\Console\Core\Style\DrupalStyle;
10 use Drupal\Console\Core\Utils\ChainQueue;
11 use Drupal\Console\Core\Utils\StringConverter;
12 use Drupal\Console\Extension\Manager;
13 use Drupal\metatag\Generator\MetatagTagGenerator;
14 use Drupal\metatag\MetatagManager;
15 use Symfony\Component\Console\Command\Command;
16 use Symfony\Component\Console\Input\InputInterface;
17 use Symfony\Component\Console\Input\InputOption;
18 use Symfony\Component\Console\Output\OutputInterface;
21 * Class GenerateTagCommand.
23 * Generate a Metatag tag plugin.
25 * @package Drupal\metatag
27 class GenerateTagCommand extends Command {
32 use ConfirmationTrait;
35 * The Metatag manager.
37 * @var \Drupal\metatag\MetatagManager
39 protected $metatagManager;
42 * The Metatag tag generator.
44 * @var \Drupal\metatag\Generator\MetatagTagGenerator
49 * An extension manager.
51 * @var \Drupal\Console\Extension\Manager
53 protected $extensionManager;
56 * The string converter.
58 * @var \Drupal\Console\Core\Utils\StringConverter
60 protected $stringConverter;
63 * The console chain queue.
65 * @var \Drupal\Console\Core\Utils\ChainQueue
67 protected $chainQueue;
70 * The GenerateTagCommand constructor.
72 * @param \Drupal\metatag\MetatagManager $metatagManager
73 * The metatag manager object.
74 * @param \Drupal\metatag\Generator\MetatagTagGenerator $generator
75 * The tag generator object.
76 * @param \Drupal\Console\Extension\Manager $extensionManager
77 * The extension manager object.
78 * @param \Drupal\Console\Core\Utils\StringConverter $stringConverter
79 * The string converter object.
80 * @param \Drupal\Console\Core\Utils\ChainQueue $chainQueue
81 * The chain queue object.
83 public function __construct(
84 MetatagManager $metatagManager,
85 MetatagTagGenerator $generator,
86 Manager $extensionManager,
87 StringConverter $stringConverter,
88 ChainQueue $chainQueue
90 $this->metatagManager = $metatagManager;
91 $this->generator = $generator;
92 $this->extensionManager = $extensionManager;
93 $this->stringConverter = $stringConverter;
94 $this->chainQueue = $chainQueue;
96 parent::__construct();
102 protected function configure() {
104 ->setName('generate:plugin:metatag:tag')
105 ->setDescription($this->trans('commands.generate.metatag.tag.description'))
106 ->setHelp($this->trans('commands.generate.metatag.tag.help'))
107 ->addOption('base_class', '', InputOption::VALUE_REQUIRED,
108 $this->trans('commands.common.options.base_class'))
109 ->addOption('module', '', InputOption::VALUE_REQUIRED,
110 $this->trans('commands.common.options.module'))
111 ->addOption('name', '', InputOption::VALUE_REQUIRED,
112 $this->trans('commands.generate.metatag.tag.options.name'))
113 ->addOption('label', '', InputOption::VALUE_REQUIRED,
114 $this->trans('commands.generate.metatag.tag.options.label'))
115 ->addOption('description', '', InputOption::VALUE_OPTIONAL,
116 $this->trans('commands.generate.metatag.tag.options.description'))
117 ->addOption('plugin-id', '', InputOption::VALUE_REQUIRED,
118 $this->trans('commands.generate.metatag.tag.options.plugin_id'))
119 ->addOption('class-name', '', InputOption::VALUE_REQUIRED,
120 $this->trans('commands.generate.metatag.tag.options.class_name'))
121 ->addOption('group', '', InputOption::VALUE_REQUIRED,
122 $this->trans('commands.generate.metatag.tag.options.group'))
123 ->addOption('weight', '', InputOption::VALUE_REQUIRED,
124 $this->trans('commands.generate.metatag.tag.options.weight'))
125 ->addOption('type', '', InputOption::VALUE_REQUIRED,
126 $this->trans('commands.generate.metatag.tag.options.type'))
127 ->addOption('secure', '', InputOption::VALUE_REQUIRED,
128 $this->trans('commands.generate.metatag.tag.options.secure'))
129 ->addOption('multiple', '', InputOption::VALUE_REQUIRED,
130 $this->trans('commands.generate.metatag.tag.options.multiple'));
136 protected function execute(InputInterface $input, OutputInterface $output) {
137 $io = new DrupalStyle($input, $output);
139 // @see Drupal\Console\Command\ConfirmationTrait::confirmGeneration
140 if (!$this->confirmGeneration($io)) {
144 $base_class = $input->getOption('base_class');
145 $module = $input->getOption('module');
146 $name = $input->getOption('name');
147 $label = $input->getOption('label');
148 $description = $input->getOption('description');
149 $plugin_id = $input->getOption('plugin-id');
150 $class_name = $input->getOption('class-name');
151 $group = $input->getOption('group');
152 $weight = $input->getOption('weight');
153 $type = $input->getOption('type');
154 $secure = $input->getOption('secure');
155 $multiple = $input->getOption('multiple');
158 ->generate($base_class, $module, $name, $label, $description, $plugin_id, $class_name, $group, $weight, $type, $secure, $multiple);
160 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
166 protected function interact(InputInterface $input, OutputInterface $output) {
168 $io = new DrupalStyle($input, $output);
175 // @todo Take this from typed data, so it can be extended?
184 // --base_class option.
185 // @todo Turn this into a choice() option.
186 $base_class = $input->getOption('base_class');
187 if (empty($base_class)) {
188 $base_class = $io->ask(
189 $this->trans('commands.generate.metatag.tag.questions.base_class'),
193 $input->setOption('base_class', $base_class);
196 $module = $input->getOption('module');
197 if (empty($module)) {
198 // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
199 $module = $this->moduleQuestion($io);
201 $input->setOption('module', $module);
204 // @todo Add validation.
205 $name = $input->getOption('name');
208 $this->trans('commands.generate.metatag.tag.questions.name')
211 $input->setOption('name', $name);
214 $label = $input->getOption('label');
217 $this->trans('commands.generate.metatag.tag.questions.label'),
221 $input->setOption('label', $label);
223 // --description option.
224 $description = $input->getOption('description');
225 if (empty($description)) {
226 $description = $io->ask(
227 $this->trans('commands.generate.metatag.tag.questions.description')
230 $input->setOption('description', $description);
232 // --plugin-id option.
233 $plugin_id = $input->getOption('plugin-id');
234 if (empty($plugin_id)) {
235 $plugin_id = $this->nameToPluginId($name);
236 $plugin_id = $io->ask(
237 $this->trans('commands.generate.metatag.tag.questions.plugin_id'),
241 $input->setOption('plugin-id', $plugin_id);
243 // --class-name option.
244 $class_name = $input->getOption('class-name');
245 if (empty($class_name)) {
246 $class_name = $this->nameToClassName($name);
247 $class_name = $io->ask(
248 $this->trans('commands.generate.metatag.tag.questions.class_name'),
252 $input->setOption('class-name', $class_name);
255 $group = $input->getOption('group');
257 $groups = $this->getGroups();
258 $group = $io->choice(
259 $this->trans('commands.generate.metatag.tag.questions.group'),
263 $input->setOption('group', $group);
266 // @todo Automatically get the next int value based upon the current group.
267 $weight = $input->getOption('weight');
268 if (is_null($weight)) {
270 $this->trans('commands.generate.metatag.tag.questions.weight'),
274 $input->setOption('weight', $weight);
277 // @todo Turn this into an option.
278 $type = $input->getOption('type');
279 if (is_null($type)) {
281 $this->trans('commands.generate.metatag.tag.questions.type'),
286 $input->setOption('type', $type);
289 // @todo Turn this into an option.
290 $secure = $input->getOption('secure');
291 if (is_null($secure)) {
292 $secure = $io->choice(
293 $this->trans('commands.generate.metatag.tag.questions.secure'),
298 $input->setOption('secure', $secure);
300 // --multiple option.
301 $multiple = $input->getOption('multiple');
302 if (is_null($multiple)) {
303 $multiple = $io->choice(
304 $this->trans('commands.generate.metatag.tag.questions.multiple'),
309 $input->setOption('multiple', $multiple);
313 * Convert the meta tag's name to a plugin ID.
315 * @param string $name
316 * The meta tag name to convert.
319 * The original string with all non-alphanumeric characters converted to
322 private function nameToPluginId($name) {
323 return $this->stringConverter->createMachineName($name);
327 * Convert the meta tag's name to a class name.
329 * @param string $name
330 * The meta tag name to convert.
333 * The original string with all non-alphanumeric characters removed and
334 * converted to CamelCase.
336 private function nameToClassName($name) {
337 return $this->stringConverter->humanToCamelCase($name);
341 * All of the meta tag groups.
344 * A list of the available groups.
346 private function getGroups() {
347 return array_keys($this->metatagManager->sortedGroups());
351 * Confirm that a requested group exists.
353 * @param string $group
354 * A group's machine name.
357 * The group's name, if available, otherwise an empty string.
359 private function validateGroupExist($group) {
360 $groups = $this->getGroups();
361 if (isset($groups[$group])) {