Version 1
[yaffs-website] / web / modules / contrib / metatag / src / Command / GenerateTagCommand.php
1 <?php
2 /**
3  * @file
4  * Contains Drupal\metatag\Command\GenerateTagCommand.
5  */
6
7 namespace Drupal\metatag\Command;
8
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;
23
24 /**
25  * Class GenerateTagCommand.
26  *
27  * Generate a Metatag tag plugin.
28  *
29  * @package Drupal\metatag
30  */
31 class GenerateTagCommand extends Command {
32
33   use CommandTrait;
34   use ModuleTrait;
35   use FormTrait;
36   use ConfirmationTrait;
37
38   /**
39    * @var MetatagManager
40    */
41   protected $metatagManager;
42
43   /**
44    * @var MetatagTagGenerator
45    */
46   protected $generator;
47
48   /** @var Manager  */
49   protected $extensionManager;
50
51   /**
52    * @var StringConverter
53    */
54   protected $stringConverter;
55
56   /**
57    * @var ChainQueue
58    */
59   protected $chainQueue;
60
61   /**
62    * GenerateTagCommand constructor.
63    *
64    * @param MetatagManager $metatagManager
65    * @param MetatagTagGenerator $generator
66    * @param Manager $extensionManager
67    * @param StringConverter $stringConverter
68    * @param ChainQueue $chainQueue
69    */
70   public function __construct(
71       MetatagManager $metatagManager,
72       MetatagTagGenerator $generator,
73       Manager $extensionManager,
74       StringConverter $stringConverter,
75       ChainQueue $chainQueue
76     ) {
77     $this->metatagManager = $metatagManager;
78     $this->generator = $generator;
79     $this->extensionManager = $extensionManager;
80     $this->stringConverter = $stringConverter;
81     $this->chainQueue = $chainQueue;
82
83     parent::__construct();
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   protected function configure() {
90     $this
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'))
118       ;
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   protected function execute(InputInterface $input, OutputInterface $output) {
125     $io = new DrupalStyle($input, $output);
126
127     // @see use Drupal\Console\Command\ConfirmationTrait::confirmGeneration
128     if (!$this->confirmGeneration($io)) {
129       return 1;
130     }
131
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');
144
145     $this->generator
146       ->generate($base_class, $module, $name, $label, $description, $plugin_id, $class_name, $group, $weight, $type, $secure, $multiple);
147
148     $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   protected function interact(InputInterface $input, OutputInterface $output) {
155
156     $io = new DrupalStyle($input, $output);
157
158     $boolean_options = [
159       'FALSE',
160       'TRUE',
161     ];
162
163     // ToDo: Take this from typed data, so it can be extended?
164     $type_options = [
165       'integer',
166       'string',
167       'label',
168       'uri',
169       'image',
170     ];
171
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'),
178         'MetaNameBase'
179       );
180     }
181     $input->setOption('base_class', $base_class);
182
183     // --module option.
184     $module = $input->getOption('module');
185     if (empty($module)) {
186       // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
187       $module = $this->moduleQuestion($io);
188     }
189     $input->setOption('module', $module);
190
191     // --name option.
192     // @todo Add validation.
193     $name = $input->getOption('name');
194     if (empty($name)) {
195       $name = $io->ask(
196         $this->trans('commands.generate.metatag.tag.questions.name')
197       );
198     }
199     $input->setOption('name', $name);
200
201     // --label option.
202     $label = $input->getOption('label');
203     if (empty($label)) {
204       $label = $io->ask(
205         $this->trans('commands.generate.metatag.tag.questions.label'),
206         $name
207       );
208     }
209     $input->setOption('label', $label);
210
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')
216       );
217     }
218     $input->setOption('description', $description);
219
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'),
226         $plugin_id
227       );
228     }
229     $input->setOption('plugin-id', $plugin_id);
230
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'),
237         $class_name
238       );
239     }
240     $input->setOption('class-name', $class_name);
241
242     // --group option.
243     $group = $input->getOption('group');
244     if (empty($group)) {
245       $groups = $this->getGroups();
246       $group = $io->choice(
247         $this->trans('commands.generate.metatag.tag.questions.group'),
248         $groups
249       );
250     }
251     $input->setOption('group', $group);
252
253     // --weight option.
254     // @todo Automatically get the next integer value based upon the current
255     //   group.
256     $weight = $input->getOption('weight');
257     if (is_null($weight)) {
258       $weight = $io->ask(
259         $this->trans('commands.generate.metatag.tag.questions.weight'),
260         0
261       );
262     }
263     $input->setOption('weight', $weight);
264
265     // --type option.
266     // @todo Turn this into an option.
267     $type = $input->getOption('type');
268     if (is_null($type)) {
269       $type = $io->choice(
270         $this->trans('commands.generate.metatag.tag.questions.type'),
271         $type_options,
272         0
273       );
274     }
275     $input->setOption('type', $type);
276
277     // --secure option.
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'),
283         $boolean_options,
284         0
285       );
286     }
287     $input->setOption('secure', $secure);
288
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'),
294         $boolean_options,
295         0
296       );
297     }
298     $input->setOption('multiple', $multiple);
299   }
300
301   /**
302    * Convert the meta tag's name to a plugin ID.
303    *
304    * @param string $name
305    *   The meta tag name to convert.
306    *
307    * @return string
308    *   The original string with all non-alphanumeric characters converted to
309    *   underline chars.
310    */
311   private function nameToPluginId($name) {
312     return $this->stringConverter->createMachineName($name);
313   }
314
315   /**
316    * Convert the meta tag's name to a class name.
317    *
318    * @param string $name
319    *   The meta tag name to convert.
320    *
321    * @return string
322    *   The original string with all non-alphanumeric characters removed and
323    *   converted to CamelCase.
324    */
325   private function nameToClassName($name) {
326     return $this->stringConverter->humanToCamelCase($name);
327   }
328
329   /**
330    * All of the meta tag groups.
331    *
332    * @return array
333    *   A list of the available groups.
334    */
335   private function getGroups() {
336     return array_keys($this->metatagManager->sortedGroups());
337   }
338
339   /**
340    * Confirm that a requested group exists.
341    *
342    * @param string $group
343    *   A group's machine name.
344    *
345    * @return string
346    *   The group's name, if available, otherwise an empty string.
347    */
348   private function validateGroupExist($group) {
349     $groups = $this->getGroups();
350     if (isset($groups[$group])) {
351       return $group;
352     }
353     return '';
354   }
355
356 }