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