Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginFieldCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginFieldCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ModuleTrait;
14 use Drupal\Console\Command\Shared\ConfirmationTrait;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Extension\Manager;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\Console\Core\Utils\StringConverter;
20 use Drupal\Console\Core\Utils\ChainQueue;
21
22 class PluginFieldCommand extends Command
23 {
24     use ModuleTrait;
25     use ConfirmationTrait;
26     use CommandTrait;
27
28     /**
29  * @var Manager
30 */
31     protected $extensionManager;
32
33     /**
34      * @var StringConverter
35      */
36     protected $stringConverter;
37
38     /**
39      * @var ChainQueue
40      */
41     protected $chainQueue;
42
43
44     /**
45      * PluginFieldCommand constructor.
46      *
47      * @param Manager         $extensionManager
48      * @param StringConverter $stringConverter
49      * @param ChainQueue      $chainQueue
50      */
51     public function __construct(
52         Manager $extensionManager,
53         StringConverter $stringConverter,
54         ChainQueue $chainQueue
55     ) {
56         $this->extensionManager = $extensionManager;
57         $this->stringConverter = $stringConverter;
58         $this->chainQueue = $chainQueue;
59         parent::__construct();
60     }
61
62     protected function configure()
63     {
64         $this
65             ->setName('generate:plugin:field')
66             ->setDescription($this->trans('commands.generate.plugin.field.description'))
67             ->setHelp($this->trans('commands.generate.plugin.field.help'))
68             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
69             ->addOption(
70                 'type-class',
71                 null,
72                 InputOption::VALUE_REQUIRED,
73                 $this->trans('commands.generate.plugin.field.options.type-class')
74             )
75             ->addOption(
76                 'type-label',
77                 null,
78                 InputOption::VALUE_OPTIONAL,
79                 $this->trans('commands.generate.plugin.field.options.type-label')
80             )
81             ->addOption(
82                 'type-plugin-id',
83                 null,
84                 InputOption::VALUE_OPTIONAL,
85                 $this->trans('commands.generate.plugin.field.options.type-plugin-id')
86             )
87             ->addOption(
88                 'type-description',
89                 null,
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.generate.plugin.field.options.type-type-description')
92             )
93             ->addOption(
94                 'formatter-class',
95                 null,
96                 InputOption::VALUE_REQUIRED,
97                 $this->trans('commands.generate.plugin.field.options.class')
98             )
99             ->addOption(
100                 'formatter-label',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.plugin.field.options.formatter-label')
104             )
105             ->addOption(
106                 'formatter-plugin-id',
107                 null,
108                 InputOption::VALUE_OPTIONAL,
109                 $this->trans('commands.generate.plugin.field.options.formatter-plugin-id')
110             )
111             ->addOption(
112                 'widget-class',
113                 null,
114                 InputOption::VALUE_REQUIRED,
115                 $this->trans('commands.generate.plugin.field.options.formatter-class')
116             )
117             ->addOption(
118                 'widget-label',
119                 null,
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.generate.plugin.field.options.widget-label')
122             )
123             ->addOption(
124                 'widget-plugin-id',
125                 null,
126                 InputOption::VALUE_OPTIONAL,
127                 $this->trans('commands.generate.plugin.field.options.widget-plugin-id')
128             )
129             ->addOption(
130                 'field-type',
131                 null,
132                 InputOption::VALUE_OPTIONAL,
133                 $this->trans('commands.generate.plugin.field.options.field-type')
134             )
135             ->addOption(
136                 'default-widget',
137                 null,
138                 InputOption::VALUE_OPTIONAL,
139                 $this->trans('commands.generate.plugin.field.options.default-widget')
140             )
141             ->addOption(
142                 'default-formatter',
143                 null,
144                 InputOption::VALUE_OPTIONAL,
145                 $this->trans('commands.generate.plugin.field.options.default-formatter')
146             );
147     }
148
149     /**
150      * {@inheritdoc}
151      */
152     protected function execute(InputInterface $input, OutputInterface $output)
153     {
154         $io = new DrupalStyle($input, $output);
155
156         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
157         if (!$this->confirmGeneration($io)) {
158             return 1;
159         }
160
161         $this->chainQueue
162             ->addCommand(
163                 'generate:plugin:fieldtype', [
164                 '--module' => $input->getOption('module'),
165                 '--class' => $input->getOption('type-class'),
166                 '--label' => $input->getOption('type-label'),
167                 '--plugin-id' => $input->getOption('type-plugin-id'),
168                 '--description' => $input->getOption('type-description'),
169                 '--default-widget' => $input->getOption('default-widget'),
170                 '--default-formatter' => $input->getOption('default-formatter'),
171                 ],
172                 false
173             );
174
175         $this->chainQueue
176             ->addCommand(
177                 'generate:plugin:fieldwidget', [
178                 '--module' => $input->getOption('module'),
179                 '--class' => $input->getOption('widget-class'),
180                 '--label' => $input->getOption('widget-label'),
181                 '--plugin-id' => $input->getOption('widget-plugin-id'),
182                 '--field-type' => $input->getOption('field-type'),
183                 ],
184                 false
185             );
186         $this->chainQueue
187             ->addCommand(
188                 'generate:plugin:fieldformatter', [
189                 '--module' => $input->getOption('module'),
190                 '--class' => $input->getOption('formatter-class'),
191                 '--label' => $input->getOption('formatter-label'),
192                 '--plugin-id' => $input->getOption('formatter-plugin-id'),
193                 '--field-type' => $input->getOption('field-type'),
194                 ],
195                 false
196             );
197
198         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery'], false);
199
200         return 0;
201     }
202
203     protected function interact(InputInterface $input, OutputInterface $output)
204     {
205         $io = new DrupalStyle($input, $output);
206
207         // --module option
208         $module = $input->getOption('module');
209         if (!$module) {
210             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
211             $module = $this->moduleQuestion($io);
212             $input->setOption('module', $module);
213         }
214
215         // --type-class option
216         $typeClass = $input->getOption('type-class');
217         if (!$typeClass) {
218             $typeClass = $io->ask(
219                 $this->trans('commands.generate.plugin.field.questions.type-class'),
220                 'ExampleFieldType'
221             );
222             $input->setOption('type-class', $typeClass);
223         }
224
225         // --type-label option
226         $label = $input->getOption('type-label');
227         if (!$label) {
228             $label = $io->ask(
229                 $this->trans('commands.generate.plugin.field.questions.type-label'),
230                 $this->stringConverter->camelCaseToHuman($typeClass)
231             );
232             $input->setOption('type-label', $label);
233         }
234
235         // --type-plugin-id option
236         $plugin_id = $input->getOption('type-plugin-id');
237         if (!$plugin_id) {
238             $plugin_id = $io->ask(
239                 $this->trans('commands.generate.plugin.field.questions.type-plugin-id'),
240                 $this->stringConverter->camelCaseToUnderscore($typeClass)
241             );
242             $input->setOption('type-plugin-id', $plugin_id);
243         }
244
245         // --type-description option
246         $description = $input->getOption('type-description');
247         if (!$description) {
248             $description = $io->ask(
249                 $this->trans('commands.generate.plugin.field.questions.type-description'),
250                 'My Field Type'
251             );
252             $input->setOption('type-description', $description);
253         }
254
255         // --widget-class option
256         $widgetClass = $input->getOption('widget-class');
257         if (!$widgetClass) {
258             $widgetClass = $io->ask(
259                 $this->trans('commands.generate.plugin.field.questions.widget-class'),
260                 'ExampleWidgetType'
261             );
262             $input->setOption('widget-class', $widgetClass);
263         }
264
265         // --widget-label option
266         $widgetLabel = $input->getOption('widget-label');
267         if (!$widgetLabel) {
268             $widgetLabel = $io->ask(
269                 $this->trans('commands.generate.plugin.field.questions.widget-label'),
270                 $this->stringConverter->camelCaseToHuman($widgetClass)
271             );
272             $input->setOption('widget-label', $widgetLabel);
273         }
274
275         // --widget-plugin-id option
276         $widget_plugin_id = $input->getOption('widget-plugin-id');
277         if (!$widget_plugin_id) {
278             $widget_plugin_id = $io->ask(
279                 $this->trans('commands.generate.plugin.field.questions.widget-plugin-id'),
280                 $this->stringConverter->camelCaseToUnderscore($widgetClass)
281             );
282             $input->setOption('widget-plugin-id', $widget_plugin_id);
283         }
284
285         // --formatter-class option
286         $formatterClass = $input->getOption('formatter-class');
287         if (!$formatterClass) {
288             $formatterClass = $io->ask(
289                 $this->trans('commands.generate.plugin.field.questions.formatter-class'),
290                 'ExampleFormatterType'
291             );
292             $input->setOption('formatter-class', $formatterClass);
293         }
294
295         // --formatter-label option
296         $formatterLabel = $input->getOption('formatter-label');
297         if (!$formatterLabel) {
298             $formatterLabel = $io->ask(
299                 $this->trans('commands.generate.plugin.field.questions.formatter-label'),
300                 $this->stringConverter->camelCaseToHuman($formatterClass)
301             );
302             $input->setOption('formatter-label', $formatterLabel);
303         }
304
305         // --formatter-plugin-id option
306         $formatter_plugin_id = $input->getOption('formatter-plugin-id');
307         if (!$formatter_plugin_id) {
308             $formatter_plugin_id = $io->ask(
309                 $this->trans('commands.generate.plugin.field.questions.formatter-plugin-id'),
310                 $this->stringConverter->camelCaseToUnderscore($formatterClass)
311             );
312             $input->setOption('formatter-plugin-id', $formatter_plugin_id);
313         }
314
315         // --field-type option
316         $field_type = $input->getOption('field-type');
317         if (!$field_type) {
318             $field_type = $io->ask(
319                 $this->trans('commands.generate.plugin.field.questions.field-type'),
320                 $plugin_id
321             );
322             $input->setOption('field-type', $field_type);
323         }
324
325         // --default-widget option
326         $default_widget = $input->getOption('default-widget');
327         if (!$default_widget) {
328             $default_widget = $io->ask(
329                 $this->trans('commands.generate.plugin.field.questions.default-widget'),
330                 $widget_plugin_id
331             );
332             $input->setOption('default-widget', $default_widget);
333         }
334
335         // --default-formatter option
336         $default_formatter = $input->getOption('default-formatter');
337         if (!$default_formatter) {
338             $default_formatter = $io->ask(
339                 $this->trans('commands.generate.plugin.field.questions.default-formatter'),
340                 $formatter_plugin_id
341             );
342             $input->setOption('default-formatter', $default_formatter);
343         }
344     }
345 }