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