43785e3d5b0f480c23c1c953b8edb2c393e97358
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginRulesActionCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginRulesActionCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Drupal\Console\Generator\PluginRulesActionGenerator;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\ServicesTrait;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\FormTrait;
17 use Drupal\Console\Command\Shared\ConfirmationTrait;
18 use Symfony\Component\Console\Command\Command;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Command\Shared\CommandTrait;
22 use Drupal\Console\Core\Utils\StringConverter;
23 use Drupal\Console\Core\Utils\ChainQueue;
24
25 /**
26  * Class PluginRulesActionCommand
27  *
28  * @package Drupal\Console\Command\Generate
29  */
30 class PluginRulesActionCommand extends Command
31 {
32     use ServicesTrait;
33     use ModuleTrait;
34     use FormTrait;
35     use ConfirmationTrait;
36     use CommandTrait;
37
38     /**
39  * @var Manager
40 */
41     protected $extensionManager;
42
43     /**
44  * @var PluginRulesActionGenerator
45 */
46     protected $generator;
47
48     /**
49      * @var StringConverter
50      */
51     protected $stringConverter;
52
53     /**
54      * @var ChainQueue
55      */
56     protected $chainQueue;
57
58
59     /**
60      * PluginRulesActionCommand constructor.
61      *
62      * @param Manager                    $extensionManager
63      * @param PluginRulesActionGenerator $generator
64      * @param StringConverter            $stringConverter
65      * @param ChainQueue                 $chainQueue
66      */
67     public function __construct(
68         Manager $extensionManager,
69         PluginRulesActionGenerator $generator,
70         StringConverter $stringConverter,
71         ChainQueue $chainQueue
72     ) {
73         $this->extensionManager = $extensionManager;
74         $this->generator = $generator;
75         $this->stringConverter = $stringConverter;
76         $this->chainQueue = $chainQueue;
77         parent::__construct();
78     }
79
80     protected function configure()
81     {
82         $this
83             ->setName('generate:plugin:rulesaction')
84             ->setDescription($this->trans('commands.generate.plugin.rulesaction.description'))
85             ->setHelp($this->trans('commands.generate.plugin.rulesaction.help'))
86             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
87             ->addOption(
88                 'class',
89                 null,
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.generate.plugin.rulesaction.options.class')
92             )
93             ->addOption(
94                 'label',
95                 null,
96                 InputOption::VALUE_OPTIONAL,
97                 $this->trans('commands.generate.plugin.rulesaction.options.label')
98             )
99             ->addOption(
100                 'plugin-id',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.plugin.rulesaction.options.plugin-id')
104             )
105             ->addOption('type', null, InputOption::VALUE_REQUIRED, $this->trans('commands.generate.plugin.rulesaction.options.type'))
106             ->addOption(
107                 'category',
108                 null,
109                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
110                 $this->trans('commands.generate.plugin.rulesaction.options.category')
111             )
112             ->addOption(
113                 'context',
114                 null,
115                 InputOption::VALUE_OPTIONAL,
116                 $this->trans('commands.generate.plugin.rulesaction.options.context')
117             );
118     }
119
120     /**
121      * {@inheritdoc}
122      */
123     protected function execute(InputInterface $input, OutputInterface $output)
124     {
125         $io = new DrupalStyle($input, $output);
126
127         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
128         if (!$this->confirmGeneration($io)) {
129             return 1;
130         }
131
132         $module = $input->getOption('module');
133         $class_name = $input->getOption('class');
134         $label = $input->getOption('label');
135         $plugin_id = $input->getOption('plugin-id');
136         $type = $input->getOption('type');
137         $category = $input->getOption('category');
138         $context = $input->getOption('context');
139
140         $this->generator->generate($module, $class_name, $label, $plugin_id, $category, $context, $type);
141
142         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
143
144         return 0;
145     }
146
147     protected function interact(InputInterface $input, OutputInterface $output)
148     {
149         $io = new DrupalStyle($input, $output);
150
151         // --module option
152         $module = $input->getOption('module');
153         if (!$module) {
154             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
155             $module = $this->moduleQuestion($io);
156             $input->setOption('module', $module);
157         }
158
159         // --class option
160         $class_name = $input->getOption('class');
161         if (!$class_name) {
162             $class_name = $io->ask(
163                 $this->trans('commands.generate.plugin.rulesaction.options.class'),
164                 'DefaultAction'
165             );
166             $input->setOption('class', $class_name);
167         }
168
169         // --label option
170         $label = $input->getOption('label');
171         if (!$label) {
172             $label = $io->ask(
173                 $this->trans('commands.generate.plugin.rulesaction.options.label'),
174                 $this->stringConverter->camelCaseToHuman($class_name)
175             );
176             $input->setOption('label', $label);
177         }
178
179         // --plugin-id option
180         $plugin_id = $input->getOption('plugin-id');
181         if (!$plugin_id) {
182             $plugin_id = $io->ask(
183                 $this->trans('commands.generate.plugin.rulesaction.options.plugin-id'),
184                 $this->stringConverter->camelCaseToUnderscore($class_name)
185             );
186             $input->setOption('plugin-id', $plugin_id);
187         }
188
189         // --type option
190         $type = $input->getOption('type');
191         if (!$type) {
192             $type = $io->ask(
193                 $this->trans('commands.generate.plugin.rulesaction.options.type'),
194                 'user'
195             );
196             $input->setOption('type', $type);
197         }
198
199         // --category option
200         $category = $input->getOption('category');
201         if (!$category) {
202             $category = $io->ask(
203                 $this->trans('commands.generate.plugin.rulesaction.options.category'),
204                 $this->stringConverter->camelCaseToUnderscore($class_name)
205             );
206             $input->setOption('category', $category);
207         }
208
209         // --context option
210         $context = $input->getOption('context');
211         if (!$context) {
212             $context = $io->ask(
213                 $this->trans('commands.generate.plugin.rulesaction.options.context'),
214                 $this->stringConverter->camelCaseToUnderscore($class_name)
215             );
216             $input->setOption('context', $context);
217         }
218     }
219 }