Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / Command / GenerateGroupCommand.php
1 <?php
2
3 namespace Drupal\metatag\Command;
4
5 use Symfony\Component\Console\Input\InputInterface;
6 use Symfony\Component\Console\Input\InputOption;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Symfony\Component\Console\Command\Command;
9 use Drupal\Console\Core\Command\Shared\CommandTrait;
10 use Drupal\Console\Command\Shared\ModuleTrait;
11 use Drupal\Console\Command\Shared\FormTrait;
12 use Drupal\Console\Command\Shared\ConfirmationTrait;
13 use Drupal\Console\Core\Style\DrupalStyle;
14 use Drupal\metatag\Generator\MetatagGroupGenerator;
15 use Drupal\Console\Extension\Manager;
16 use Drupal\Console\Core\Utils\ChainQueue;
17
18 /**
19  * Class GenerateGroupCommand.
20  *
21  * Generate a Metatag group plugin.
22  *
23  * @package Drupal\metatag
24  */
25 class GenerateGroupCommand extends Command {
26
27   use CommandTrait;
28   use ModuleTrait;
29   use FormTrait;
30   use ConfirmationTrait;
31
32   /**
33    * The metatag group generator.
34    *
35    * @var \Drupal\metatag\Generator\MetatagGroupGenerator
36    */
37   protected $generator;
38
39   /**
40    * The console extension manager.
41    *
42    * @var \Drupal\Console\Extension\Manager
43    */
44   protected $extensionManager;
45
46   /**
47    * The console chain queue.
48    *
49    * @var \Drupal\Console\Core\Utils\ChainQueue
50    */
51   protected $chainQueue;
52
53   /**
54    * The GenerateTagCommand constructor.
55    *
56    * @param \Drupal\metatag\Generator\MetatagGroupGenerator $generator
57    *   The generator object.
58    * @param \Drupal\Console\Extension\Manager $extensionManager
59    *   The extension manager object.
60    * @param \Drupal\Console\Core\Utils\ChainQueue $chainQueue
61    *   The chain queue object.
62    */
63   public function __construct(
64       MetatagGroupGenerator $generator,
65       Manager $extensionManager,
66       ChainQueue $chainQueue
67     ) {
68     $this->generator = $generator;
69     $this->extensionManager = $extensionManager;
70     $this->chainQueue = $chainQueue;
71
72     parent::__construct();
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   protected function configure() {
79     $this
80       ->setName('generate:plugin:metatag:group')
81       ->setDescription($this->trans('commands.generate.metatag.group.description'))
82       ->setHelp($this->trans('commands.generate.metatag.group.help'))
83       ->addOption('base_class', '', InputOption::VALUE_REQUIRED,
84         $this->trans('commands.common.options.base_class'))
85       ->addOption('module', '', InputOption::VALUE_REQUIRED,
86         $this->trans('commands.common.options.module'))
87       ->addOption('label', '', InputOption::VALUE_REQUIRED,
88         $this->trans('commands.generate.metatag.group.options.label'))
89       ->addOption('description', '', InputOption::VALUE_OPTIONAL,
90         $this->trans('commands.generate.metatag.group.options.description'))
91       ->addOption('plugin-id', '', InputOption::VALUE_REQUIRED,
92         $this->trans('commands.generate.metatag.group.options.plugin_id'))
93       ->addOption('class-name', '', InputOption::VALUE_REQUIRED,
94         $this->trans('commands.generate.metatag.group.options.class_name'))
95       ->addOption('weight', '', InputOption::VALUE_REQUIRED,
96         $this->trans('commands.generate.metatag.group.options.weight'));
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   protected function execute(InputInterface $input, OutputInterface $output) {
103     $io = new DrupalStyle($input, $output);
104
105     // @see Drupal\Console\Command\ConfirmationTrait::confirmGeneration
106     if (!$this->confirmGeneration($io)) {
107       return 1;
108     }
109
110     $base_class = $input->getOption('base_class');
111     $module = $input->getOption('module');
112     $label = $input->getOption('label');
113     $description = $input->getOption('description');
114     $plugin_id = $input->getOption('plugin-id');
115     $class_name = $input->getOption('class-name');
116     $weight = $input->getOption('weight');
117
118     $this->generator
119       ->generate($base_class, $module, $label, $description, $plugin_id, $class_name, $weight);
120
121     $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   protected function interact(InputInterface $input, OutputInterface $output) {
128     $io = new DrupalStyle($input, $output);
129
130     // --base_class option.
131     // @todo Turn this into a choice() option.
132     $base_class = $input->getOption('base_class');
133     if (empty($base_class)) {
134       $base_class = $io->ask(
135         $this->trans('commands.generate.metatag.group.questions.base_class'),
136         'GroupBase'
137       );
138     }
139     $input->setOption('base_class', $base_class);
140
141     // --module option.
142     $module = $input->getOption('module');
143     if (empty($module)) {
144       // @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
145       $module = $this->moduleQuestion($io);
146     }
147     $input->setOption('module', $module);
148
149     // --label option.
150     $label = $input->getOption('label');
151     if (empty($label)) {
152       $label = $io->ask(
153         $this->trans('commands.generate.metatag.group.questions.label')
154       );
155     }
156     $input->setOption('label', $label);
157
158     // --description option.
159     $description = $input->getOption('description');
160     if (empty($description)) {
161       $description = $io->ask(
162         $this->trans('commands.generate.metatag.group.questions.description')
163       );
164     }
165     $input->setOption('description', $description);
166
167     // --plugin-id option.
168     $plugin_id = $input->getOption('plugin-id');
169     if (empty($plugin_id)) {
170       $plugin_id = $io->ask(
171         $this->trans('commands.generate.metatag.group.questions.plugin_id')
172       );
173     }
174     $input->setOption('plugin-id', $plugin_id);
175
176     // --class-name option.
177     $class_name = $input->getOption('class-name');
178     if (empty($class_name)) {
179       $class_name = $io->ask(
180         $this->trans('commands.generate.metatag.group.questions.class_name')
181       );
182     }
183     $input->setOption('class-name', $class_name);
184
185     // --weight option.
186     // @todo Automatically get the next int value based upon the current group.
187     $weight = $input->getOption('weight');
188     if (is_null($weight)) {
189       $weight = $io->ask(
190         $this->trans('commands.generate.metatag.group.questions.weight'),
191         0
192       );
193     }
194     $input->setOption('weight', $weight);
195   }
196
197 }