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