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