Version 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', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
97             ->addOption(
98                 'class',
99                 '',
100                 InputOption::VALUE_REQUIRED,
101                 $this->trans('commands.generate.plugin.fieldwidget.options.class')
102             )
103             ->addOption(
104                 'label',
105                 '',
106                 InputOption::VALUE_OPTIONAL,
107                 $this->trans('commands.generate.plugin.fieldwidget.options.label')
108             )
109             ->addOption(
110                 'plugin-id',
111                 '',
112                 InputOption::VALUE_OPTIONAL,
113                 $this->trans('commands.generate.plugin.fieldwidget.options.plugin-id')
114             )
115             ->addOption(
116                 'field-type',
117                 '',
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;
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
146     protected function interact(InputInterface $input, OutputInterface $output)
147     {
148         $io = new DrupalStyle($input, $output);
149
150         // --module option
151         $module = $input->getOption('module');
152         if (!$module) {
153             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
154             $module = $this->moduleQuestion($io);
155             $input->setOption('module', $module);
156         }
157
158         // --class option
159         $class_name = $input->getOption('class');
160         if (!$class_name) {
161             $class_name = $io->ask(
162                 $this->trans('commands.generate.plugin.fieldwidget.questions.class'),
163                 'ExampleFieldWidget'
164             );
165             $input->setOption('class', $class_name);
166         }
167
168         // --plugin label option
169         $label = $input->getOption('label');
170         if (!$label) {
171             $label = $io->ask(
172                 $this->trans('commands.generate.plugin.fieldwidget.questions.label'),
173                 $this->stringConverter->camelCaseToHuman($class_name)
174             );
175             $input->setOption('label', $label);
176         }
177
178         // --plugin-id option
179         $plugin_id = $input->getOption('plugin-id');
180         if (!$plugin_id) {
181             $plugin_id = $io->ask(
182                 $this->trans('commands.generate.plugin.fieldwidget.questions.plugin-id'),
183                 $this->stringConverter->camelCaseToUnderscore($class_name)
184             );
185             $input->setOption('plugin-id', $plugin_id);
186         }
187
188         // --field-type option
189         $field_type = $input->getOption('field-type');
190         if (!$field_type) {
191             // Gather valid field types.
192             $field_type_options = [];
193             foreach ($this->fieldTypePluginManager->getGroupedDefinitions($this->fieldTypePluginManager->getUiDefinitions()) as $category => $field_types) {
194                 foreach ($field_types as $name => $field_type) {
195                     $field_type_options[] = $name;
196                 }
197             }
198
199             $field_type  = $io->choice(
200                 $this->trans('commands.generate.plugin.fieldwidget.questions.field-type'),
201                 $field_type_options
202             );
203
204             $input->setOption('field-type', $field_type);
205         }
206     }
207 }