f5a829aed71f212b7d63b46cd409be04e5cd5204
[yaffs-website] / web / modules / contrib / metatag / src / Command / GenerateTagCommand.php
1 <?php
2
3 namespace Drupal\metatag\Command;
4
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;
19
20 /**
21  * Class GenerateTagCommand.
22  *
23  * Generate a Metatag tag plugin.
24  *
25  * @package Drupal\metatag
26  */
27 class GenerateTagCommand extends Command {
28
29   use CommandTrait;
30   use ModuleTrait;
31   use FormTrait;
32   use ConfirmationTrait;
33
34   /**
35    * The Metatag manager.
36    *
37    * @var \Drupal\metatag\MetatagManager
38    */
39   protected $metatagManager;
40
41   /**
42    * The Metatag tag generator.
43    *
44    * @var \Drupal\metatag\Generator\MetatagTagGenerator
45    */
46   protected $generator;
47
48   /**
49    * An extension manager.
50    *
51    * @var \Drupal\Console\Extension\Manager
52    */
53   protected $extensionManager;
54
55   /**
56    * The string converter.
57    *
58    * @var \Drupal\Console\Core\Utils\StringConverter
59    */
60   protected $stringConverter;
61
62   /**
63    * The console chain queue.
64    *
65    * @var \Drupal\Console\Core\Utils\ChainQueue
66    */
67   protected $chainQueue;
68
69   /**
70    * The GenerateTagCommand constructor.
71    *
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.
82    */
83   public function __construct(
84       MetatagManager $metatagManager,
85       MetatagTagGenerator $generator,
86       Manager $extensionManager,
87       StringConverter $stringConverter,
88       ChainQueue $chainQueue
89     ) {
90     $this->metatagManager = $metatagManager;
91     $this->generator = $generator;
92     $this->extensionManager = $extensionManager;
93     $this->stringConverter = $stringConverter;
94     $this->chainQueue = $chainQueue;
95
96     parent::__construct();
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function configure() {
103     $this
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'));
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   protected function execute(InputInterface $input, OutputInterface $output) {
137     $io = new DrupalStyle($input, $output);
138
139     // @see Drupal\Console\Command\ConfirmationTrait::confirmGeneration
140     if (!$this->confirmGeneration($io)) {
141       return 1;
142     }
143
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');
156
157     $this->generator
158       ->generate($base_class, $module, $name, $label, $description, $plugin_id, $class_name, $group, $weight, $type, $secure, $multiple);
159
160     $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   protected function interact(InputInterface $input, OutputInterface $output) {
167
168     $io = new DrupalStyle($input, $output);
169
170     $boolean_options = [
171       'FALSE',
172       'TRUE',
173     ];
174
175     // @todo Take this from typed data, so it can be extended?
176     $type_options = [
177       'integer',
178       'string',
179       'label',
180       'uri',
181       'image',
182     ];
183
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'),
190         'MetaNameBase'
191       );
192     }
193     $input->setOption('base_class', $base_class);
194
195     // --module option.
196     $module = $input->getOption('module');
197     if (empty($module)) {
198       // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
199       $module = $this->moduleQuestion($io);
200     }
201     $input->setOption('module', $module);
202
203     // --name option.
204     // @todo Add validation.
205     $name = $input->getOption('name');
206     if (empty($name)) {
207       $name = $io->ask(
208         $this->trans('commands.generate.metatag.tag.questions.name')
209       );
210     }
211     $input->setOption('name', $name);
212
213     // --label option.
214     $label = $input->getOption('label');
215     if (empty($label)) {
216       $label = $io->ask(
217         $this->trans('commands.generate.metatag.tag.questions.label'),
218         $name
219       );
220     }
221     $input->setOption('label', $label);
222
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')
228       );
229     }
230     $input->setOption('description', $description);
231
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'),
238         $plugin_id
239       );
240     }
241     $input->setOption('plugin-id', $plugin_id);
242
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'),
249         $class_name
250       );
251     }
252     $input->setOption('class-name', $class_name);
253
254     // --group option.
255     $group = $input->getOption('group');
256     if (empty($group)) {
257       $groups = $this->getGroups();
258       $group = $io->choice(
259         $this->trans('commands.generate.metatag.tag.questions.group'),
260         $groups
261       );
262     }
263     $input->setOption('group', $group);
264
265     // --weight option.
266     // @todo Automatically get the next int value based upon the current group.
267     $weight = $input->getOption('weight');
268     if (is_null($weight)) {
269       $weight = $io->ask(
270         $this->trans('commands.generate.metatag.tag.questions.weight'),
271         0
272       );
273     }
274     $input->setOption('weight', $weight);
275
276     // --type option.
277     // @todo Turn this into an option.
278     $type = $input->getOption('type');
279     if (is_null($type)) {
280       $type = $io->choice(
281         $this->trans('commands.generate.metatag.tag.questions.type'),
282         $type_options,
283         0
284       );
285     }
286     $input->setOption('type', $type);
287
288     // --secure option.
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'),
294         $boolean_options,
295         0
296       );
297     }
298     $input->setOption('secure', $secure);
299
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'),
305         $boolean_options,
306         0
307       );
308     }
309     $input->setOption('multiple', $multiple);
310   }
311
312   /**
313    * Convert the meta tag's name to a plugin ID.
314    *
315    * @param string $name
316    *   The meta tag name to convert.
317    *
318    * @return string
319    *   The original string with all non-alphanumeric characters converted to
320    *   underline chars.
321    */
322   private function nameToPluginId($name) {
323     return $this->stringConverter->createMachineName($name);
324   }
325
326   /**
327    * Convert the meta tag's name to a class name.
328    *
329    * @param string $name
330    *   The meta tag name to convert.
331    *
332    * @return string
333    *   The original string with all non-alphanumeric characters removed and
334    *   converted to CamelCase.
335    */
336   private function nameToClassName($name) {
337     return $this->stringConverter->humanToCamelCase($name);
338   }
339
340   /**
341    * All of the meta tag groups.
342    *
343    * @return array
344    *   A list of the available groups.
345    */
346   private function getGroups() {
347     return array_keys($this->metatagManager->sortedGroups());
348   }
349
350   /**
351    * Confirm that a requested group exists.
352    *
353    * @param string $group
354    *   A group's machine name.
355    *
356    * @return string
357    *   The group's name, if available, otherwise an empty string.
358    */
359   private function validateGroupExist($group) {
360     $groups = $this->getGroups();
361     if (isset($groups[$group])) {
362       return $group;
363     }
364     return '';
365   }
366
367 }