Version 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', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
69             ->addOption(
70                 'type-class',
71                 '',
72                 InputOption::VALUE_REQUIRED,
73                 $this->trans('commands.generate.plugin.field.options.type-class')
74             )
75             ->addOption(
76                 'type-label',
77                 '',
78                 InputOption::VALUE_OPTIONAL,
79                 $this->trans('commands.generate.plugin.field.options.type-label')
80             )
81             ->addOption(
82                 'type-plugin-id',
83                 '',
84                 InputOption::VALUE_OPTIONAL,
85                 $this->trans('commands.generate.plugin.field.options.type-plugin-id')
86             )
87             ->addOption(
88                 'type-description',
89                 '',
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.generate.plugin.field.options.type-type-description')
92             )
93             ->addOption(
94                 'formatter-class',
95                 '',
96                 InputOption::VALUE_REQUIRED,
97                 $this->trans('commands.generate.plugin.field.options.class')
98             )
99             ->addOption(
100                 'formatter-label',
101                 '',
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.generate.plugin.field.options.formatter-label')
104             )
105             ->addOption(
106                 'formatter-plugin-id',
107                 '',
108                 InputOption::VALUE_OPTIONAL,
109                 $this->trans('commands.generate.plugin.field.options.formatter-plugin-id')
110             )
111             ->addOption(
112                 'widget-class',
113                 '',
114                 InputOption::VALUE_REQUIRED,
115                 $this->trans('commands.generate.plugin.field.options.formatter-class')
116             )
117             ->addOption(
118                 'widget-label',
119                 '',
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.generate.plugin.field.options.widget-label')
122             )
123             ->addOption(
124                 'widget-plugin-id',
125                 '',
126                 InputOption::VALUE_OPTIONAL,
127                 $this->trans('commands.generate.plugin.field.options.widget-plugin-id')
128             )
129             ->addOption(
130                 'field-type',
131                 '',
132                 InputOption::VALUE_OPTIONAL,
133                 $this->trans('commands.generate.plugin.field.options.field-type')
134             )
135             ->addOption(
136                 'default-widget',
137                 '',
138                 InputOption::VALUE_OPTIONAL,
139                 $this->trans('commands.generate.plugin.field.options.default-widget')
140             )
141             ->addOption(
142                 'default-formatter',
143                 '',
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;
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
201     protected function interact(InputInterface $input, OutputInterface $output)
202     {
203         $io = new DrupalStyle($input, $output);
204
205         // --module option
206         $module = $input->getOption('module');
207         if (!$module) {
208             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
209             $module = $this->moduleQuestion($io);
210             $input->setOption('module', $module);
211         }
212
213         // --type-class option
214         $typeClass = $input->getOption('type-class');
215         if (!$typeClass) {
216             $typeClass = $io->ask(
217                 $this->trans('commands.generate.plugin.field.questions.type-class'),
218                 'ExampleFieldType'
219             );
220             $input->setOption('type-class', $typeClass);
221         }
222
223         // --type-label option
224         $label = $input->getOption('type-label');
225         if (!$label) {
226             $label = $io->ask(
227                 $this->trans('commands.generate.plugin.field.questions.type-label'),
228                 $this->stringConverter->camelCaseToHuman($typeClass)
229             );
230             $input->setOption('type-label', $label);
231         }
232
233         // --type-plugin-id option
234         $plugin_id = $input->getOption('type-plugin-id');
235         if (!$plugin_id) {
236             $plugin_id = $io->ask(
237                 $this->trans('commands.generate.plugin.field.questions.type-plugin-id'),
238                 $this->stringConverter->camelCaseToUnderscore($typeClass)
239             );
240             $input->setOption('type-plugin-id', $plugin_id);
241         }
242
243         // --type-description option
244         $description = $input->getOption('type-description');
245         if (!$description) {
246             $description = $io->ask(
247                 $this->trans('commands.generate.plugin.field.questions.type-description'),
248                 'My Field Type'
249             );
250             $input->setOption('type-description', $description);
251         }
252
253         // --widget-class option
254         $widgetClass = $input->getOption('widget-class');
255         if (!$widgetClass) {
256             $widgetClass = $io->ask(
257                 $this->trans('commands.generate.plugin.field.questions.widget-class'),
258                 'ExampleWidgetType'
259             );
260             $input->setOption('widget-class', $widgetClass);
261         }
262
263         // --widget-label option
264         $widgetLabel = $input->getOption('widget-label');
265         if (!$widgetLabel) {
266             $widgetLabel = $io->ask(
267                 $this->trans('commands.generate.plugin.field.questions.widget-label'),
268                 $this->stringConverter->camelCaseToHuman($widgetClass)
269             );
270             $input->setOption('widget-label', $widgetLabel);
271         }
272
273         // --widget-plugin-id option
274         $widget_plugin_id = $input->getOption('widget-plugin-id');
275         if (!$widget_plugin_id) {
276             $widget_plugin_id = $io->ask(
277                 $this->trans('commands.generate.plugin.field.questions.widget-plugin-id'),
278                 $this->stringConverter->camelCaseToUnderscore($widgetClass)
279             );
280             $input->setOption('widget-plugin-id', $widget_plugin_id);
281         }
282
283         // --formatter-class option
284         $formatterClass = $input->getOption('formatter-class');
285         if (!$formatterClass) {
286             $formatterClass = $io->ask(
287                 $this->trans('commands.generate.plugin.field.questions.formatter-class'),
288                 'ExampleFormatterType'
289             );
290             $input->setOption('formatter-class', $formatterClass);
291         }
292
293         // --formatter-label option
294         $formatterLabel = $input->getOption('formatter-label');
295         if (!$formatterLabel) {
296             $formatterLabel = $io->ask(
297                 $this->trans('commands.generate.plugin.field.questions.formatter-label'),
298                 $this->stringConverter->camelCaseToHuman($formatterClass)
299             );
300             $input->setOption('formatter-label', $formatterLabel);
301         }
302
303         // --formatter-plugin-id option
304         $formatter_plugin_id = $input->getOption('formatter-plugin-id');
305         if (!$formatter_plugin_id) {
306             $formatter_plugin_id = $io->ask(
307                 $this->trans('commands.generate.plugin.field.questions.formatter-plugin-id'),
308                 $this->stringConverter->camelCaseToUnderscore($formatterClass)
309             );
310             $input->setOption('formatter-plugin-id', $formatter_plugin_id);
311         }
312
313         // --field-type option
314         $field_type = $input->getOption('field-type');
315         if (!$field_type) {
316             $field_type = $io->ask(
317                 $this->trans('commands.generate.plugin.field.questions.field-type'),
318                 $plugin_id
319             );
320             $input->setOption('field-type', $field_type);
321         }
322
323         // --default-widget option
324         $default_widget = $input->getOption('default-widget');
325         if (!$default_widget) {
326             $default_widget = $io->ask(
327                 $this->trans('commands.generate.plugin.field.questions.default-widget'),
328                 $widget_plugin_id
329             );
330             $input->setOption('default-widget', $default_widget);
331         }
332
333         // --default-formatter option
334         $default_formatter = $input->getOption('default-formatter');
335         if (!$default_formatter) {
336             $default_formatter = $io->ask(
337                 $this->trans('commands.generate.plugin.field.questions.default-formatter'),
338                 $formatter_plugin_id
339             );
340             $input->setOption('default-formatter', $default_formatter);
341         }
342     }
343 }