4163e51ad1934fce8aa2b6c767720a2e7f6e9f17
[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', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
87             ->addOption(
88                 'class',
89                 '',
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.generate.plugin.rulesaction.options.class')
92             )
93             ->addOption(
94                 'label',
95                 '',
96                 InputOption::VALUE_OPTIONAL,
97                 $this->trans('commands.generate.plugin.rulesaction.options.label')
98             )
99             ->addOption(
100                 'plugin-id',
101                 '',
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.plugin.rulesaction.options.plugin-id')
104             )
105             ->addOption('type', '', InputOption::VALUE_REQUIRED, $this->trans('commands.generate.plugin.rulesaction.options.type'))
106             ->addOption(
107                 'category',
108                 '',
109                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
110                 $this->trans('commands.generate.plugin.rulesaction.options.category')
111             )
112             ->addOption(
113                 'context',
114                 '',
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;
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
145     protected function interact(InputInterface $input, OutputInterface $output)
146     {
147         $io = new DrupalStyle($input, $output);
148
149         // --module option
150         $module = $input->getOption('module');
151         if (!$module) {
152             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
153             $module = $this->moduleQuestion($io);
154             $input->setOption('module', $module);
155         }
156
157         // --class option
158         $class_name = $input->getOption('class');
159         if (!$class_name) {
160             $class_name = $io->ask(
161                 $this->trans('commands.generate.plugin.rulesaction.options.class'),
162                 'DefaultAction'
163             );
164             $input->setOption('class', $class_name);
165         }
166
167         // --label option
168         $label = $input->getOption('label');
169         if (!$label) {
170             $label = $io->ask(
171                 $this->trans('commands.generate.plugin.rulesaction.options.label'),
172                 $this->stringConverter->camelCaseToHuman($class_name)
173             );
174             $input->setOption('label', $label);
175         }
176
177         // --plugin-id option
178         $plugin_id = $input->getOption('plugin-id');
179         if (!$plugin_id) {
180             $plugin_id = $io->ask(
181                 $this->trans('commands.generate.plugin.rulesaction.options.plugin-id'),
182                 $this->stringConverter->camelCaseToUnderscore($class_name)
183             );
184             $input->setOption('plugin-id', $plugin_id);
185         }
186
187         // --type option
188         $type = $input->getOption('type');
189         if (!$type) {
190             $type = $io->ask(
191                 $this->trans('commands.generate.plugin.rulesaction.options.type'),
192                 'user'
193             );
194             $input->setOption('type', $type);
195         }
196
197         // --category option
198         $category = $input->getOption('category');
199         if (!$category) {
200             $category = $io->ask(
201                 $this->trans('commands.generate.plugin.rulesaction.options.category'),
202                 $this->stringConverter->camelCaseToUnderscore($class_name)
203             );
204             $input->setOption('category', $category);
205         }
206
207         // --context option
208         $context = $input->getOption('context');
209         if (!$context) {
210             $context = $io->ask(
211                 $this->trans('commands.generate.plugin.rulesaction.options.context'),
212                 $this->stringConverter->camelCaseToUnderscore($class_name)
213             );
214             $input->setOption('context', $context);
215         }
216     }
217 }