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