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 * @var \Drupal\metatag\MetatagManager
37 protected $metatagManager;
40 * @var \Drupal\metatag\Generator\MetatagTagGenerator
45 * @var \Drupal\Console\Extension\Manager
47 protected $extensionManager;
50 * @var \Drupal\Console\Core\Utils\StringConverter
52 protected $stringConverter;
55 * @var \Drupal\Console\Core\Utils\ChainQueue
57 protected $chainQueue;
60 * GenerateTagCommand constructor.
62 * @param Drupal\metatag\MetatagManager $metatagManager
63 * The metatag manager object.
64 * @param Drupal\metatag\Generator\MetatagTagGenerator $generator
65 * The tag generator object.
66 * @param Drupal\Console\Extension\Manager $extensionManager
67 * The extension manager object.
68 * @param Drupal\Console\Core\Utils\StringConverter $stringConverter
69 * The string converter object.
70 * @param Drupal\Console\Core\Utils\ChainQueue $chainQueue
71 * The chain queue object.
73 public function __construct(
74 MetatagManager $metatagManager,
75 MetatagTagGenerator $generator,
76 Manager $extensionManager,
77 StringConverter $stringConverter,
78 ChainQueue $chainQueue
80 $this->metatagManager = $metatagManager;
81 $this->generator = $generator;
82 $this->extensionManager = $extensionManager;
83 $this->stringConverter = $stringConverter;
84 $this->chainQueue = $chainQueue;
86 parent::__construct();
92 protected function configure() {
94 ->setName('generate:plugin:metatag:tag')
95 ->setDescription($this->trans('commands.generate.metatag.tag.description'))
96 ->setHelp($this->trans('commands.generate.metatag.tag.help'))
97 ->addOption('base_class', '', InputOption::VALUE_REQUIRED,
98 $this->trans('commands.common.options.base_class'))
99 ->addOption('module', '', InputOption::VALUE_REQUIRED,
100 $this->trans('commands.common.options.module'))
101 ->addOption('name', '', InputOption::VALUE_REQUIRED,
102 $this->trans('commands.generate.metatag.tag.options.name'))
103 ->addOption('label', '', InputOption::VALUE_REQUIRED,
104 $this->trans('commands.generate.metatag.tag.options.label'))
105 ->addOption('description', '', InputOption::VALUE_OPTIONAL,
106 $this->trans('commands.generate.metatag.tag.options.description'))
107 ->addOption('plugin-id', '', InputOption::VALUE_REQUIRED,
108 $this->trans('commands.generate.metatag.tag.options.plugin_id'))
109 ->addOption('class-name', '', InputOption::VALUE_REQUIRED,
110 $this->trans('commands.generate.metatag.tag.options.class_name'))
111 ->addOption('group', '', InputOption::VALUE_REQUIRED,
112 $this->trans('commands.generate.metatag.tag.options.group'))
113 ->addOption('weight', '', InputOption::VALUE_REQUIRED,
114 $this->trans('commands.generate.metatag.tag.options.weight'))
115 ->addOption('type', '', InputOption::VALUE_REQUIRED,
116 $this->trans('commands.generate.metatag.tag.options.type'))
117 ->addOption('secure', '', InputOption::VALUE_REQUIRED,
118 $this->trans('commands.generate.metatag.tag.options.secure'))
119 ->addOption('multiple', '', InputOption::VALUE_REQUIRED,
120 $this->trans('commands.generate.metatag.tag.options.multiple'));
126 protected function execute(InputInterface $input, OutputInterface $output) {
127 $io = new DrupalStyle($input, $output);
129 // @see Drupal\Console\Command\ConfirmationTrait::confirmGeneration
130 if (!$this->confirmGeneration($io)) {
134 $base_class = $input->getOption('base_class');
135 $module = $input->getOption('module');
136 $name = $input->getOption('name');
137 $label = $input->getOption('label');
138 $description = $input->getOption('description');
139 $plugin_id = $input->getOption('plugin-id');
140 $class_name = $input->getOption('class-name');
141 $group = $input->getOption('group');
142 $weight = $input->getOption('weight');
143 $type = $input->getOption('type');
144 $secure = $input->getOption('secure');
145 $multiple = $input->getOption('multiple');
148 ->generate($base_class, $module, $name, $label, $description, $plugin_id, $class_name, $group, $weight, $type, $secure, $multiple);
150 $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
156 protected function interact(InputInterface $input, OutputInterface $output) {
158 $io = new DrupalStyle($input, $output);
165 // @todo Take this from typed data, so it can be extended?
174 // --base_class option.
175 // @todo Turn this into a choice() option.
176 $base_class = $input->getOption('base_class');
177 if (empty($base_class)) {
178 $base_class = $io->ask(
179 $this->trans('commands.generate.metatag.tag.questions.base_class'),
183 $input->setOption('base_class', $base_class);
186 $module = $input->getOption('module');
187 if (empty($module)) {
188 // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
189 $module = $this->moduleQuestion($io);
191 $input->setOption('module', $module);
194 // @todo Add validation.
195 $name = $input->getOption('name');
198 $this->trans('commands.generate.metatag.tag.questions.name')
201 $input->setOption('name', $name);
204 $label = $input->getOption('label');
207 $this->trans('commands.generate.metatag.tag.questions.label'),
211 $input->setOption('label', $label);
213 // --description option.
214 $description = $input->getOption('description');
215 if (empty($description)) {
216 $description = $io->ask(
217 $this->trans('commands.generate.metatag.tag.questions.description')
220 $input->setOption('description', $description);
222 // --plugin-id option.
223 $plugin_id = $input->getOption('plugin-id');
224 if (empty($plugin_id)) {
225 $plugin_id = $this->nameToPluginId($name);
226 $plugin_id = $io->ask(
227 $this->trans('commands.generate.metatag.tag.questions.plugin_id'),
231 $input->setOption('plugin-id', $plugin_id);
233 // --class-name option.
234 $class_name = $input->getOption('class-name');
235 if (empty($class_name)) {
236 $class_name = $this->nameToClassName($name);
237 $class_name = $io->ask(
238 $this->trans('commands.generate.metatag.tag.questions.class_name'),
242 $input->setOption('class-name', $class_name);
245 $group = $input->getOption('group');
247 $groups = $this->getGroups();
248 $group = $io->choice(
249 $this->trans('commands.generate.metatag.tag.questions.group'),
253 $input->setOption('group', $group);
256 // @todo Automatically get the next int value based upon the current group.
257 $weight = $input->getOption('weight');
258 if (is_null($weight)) {
260 $this->trans('commands.generate.metatag.tag.questions.weight'),
264 $input->setOption('weight', $weight);
267 // @todo Turn this into an option.
268 $type = $input->getOption('type');
269 if (is_null($type)) {
271 $this->trans('commands.generate.metatag.tag.questions.type'),
276 $input->setOption('type', $type);
279 // @todo Turn this into an option.
280 $secure = $input->getOption('secure');
281 if (is_null($secure)) {
282 $secure = $io->choice(
283 $this->trans('commands.generate.metatag.tag.questions.secure'),
288 $input->setOption('secure', $secure);
290 // --multiple option.
291 $multiple = $input->getOption('multiple');
292 if (is_null($multiple)) {
293 $multiple = $io->choice(
294 $this->trans('commands.generate.metatag.tag.questions.multiple'),
299 $input->setOption('multiple', $multiple);
303 * Convert the meta tag's name to a plugin ID.
305 * @param string $name
306 * The meta tag name to convert.
309 * The original string with all non-alphanumeric characters converted to
312 private function nameToPluginId($name) {
313 return $this->stringConverter->createMachineName($name);
317 * Convert the meta tag's name to a class name.
319 * @param string $name
320 * The meta tag name to convert.
323 * The original string with all non-alphanumeric characters removed and
324 * converted to CamelCase.
326 private function nameToClassName($name) {
327 return $this->stringConverter->humanToCamelCase($name);
331 * All of the meta tag groups.
334 * A list of the available groups.
336 private function getGroups() {
337 return array_keys($this->metatagManager->sortedGroups());
341 * Confirm that a requested group exists.
343 * @param string $group
344 * A group's machine name.
347 * The group's name, if available, otherwise an empty string.
349 private function validateGroupExist($group) {
350 $groups = $this->getGroups();
351 if (isset($groups[$group])) {