d60c4bcb5513d9b847c4ab8512c7f4a03f7735a7
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginFieldTypeCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginFieldTypeCommand.
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\PluginFieldTypeGenerator;
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 PluginFieldTypeCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class PluginFieldTypeCommand 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 PluginFieldTypeGenerator
42 */
43     protected $generator;
44
45     /**
46      * @var StringConverter
47      */
48     protected $stringConverter;
49
50     /**
51      * @var ChainQueue
52      */
53     protected $chainQueue;
54
55
56     /**
57      * PluginFieldTypeCommand constructor.
58      *
59      * @param Manager                  $extensionManager
60      * @param PluginFieldTypeGenerator $generator
61      * @param StringConverter          $stringConverter
62      * @param ChainQueue               $chainQueue
63      */
64     public function __construct(
65         Manager $extensionManager,
66         PluginFieldTypeGenerator $generator,
67         StringConverter $stringConverter,
68         ChainQueue $chainQueue
69     ) {
70         $this->extensionManager = $extensionManager;
71         $this->generator = $generator;
72         $this->stringConverter = $stringConverter;
73         $this->chainQueue = $chainQueue;
74         parent::__construct();
75     }
76
77     protected function configure()
78     {
79         $this
80             ->setName('generate:plugin:fieldtype')
81             ->setDescription($this->trans('commands.generate.plugin.fieldtype.description'))
82             ->setHelp($this->trans('commands.generate.plugin.fieldtype.help'))
83             ->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
84             ->addOption(
85                 'class',
86                 '',
87                 InputOption::VALUE_REQUIRED,
88                 $this->trans('commands.generate.plugin.fieldtype.options.class')
89             )
90             ->addOption(
91                 'label',
92                 '',
93                 InputOption::VALUE_OPTIONAL,
94                 $this->trans('commands.generate.plugin.fieldtype.options.label')
95             )
96             ->addOption(
97                 'plugin-id',
98                 '',
99                 InputOption::VALUE_OPTIONAL,
100                 $this->trans('commands.generate.plugin.fieldtype.options.plugin-id')
101             )
102             ->addOption(
103                 'description',
104                 '',
105                 InputOption::VALUE_OPTIONAL,
106                 $this->trans('commands.generate.plugin.fieldtype.options.description')
107             )
108             ->addOption(
109                 'default-widget',
110                 '',
111                 InputOption::VALUE_OPTIONAL,
112                 $this->trans('commands.generate.plugin.fieldtype.options.default-widget')
113             )
114             ->addOption(
115                 'default-formatter',
116                 '',
117                 InputOption::VALUE_OPTIONAL,
118                 $this->trans('commands.generate.plugin.fieldtype.options.default-formatter')
119             );
120     }
121
122     /**
123      * {@inheritdoc}
124      */
125     protected function execute(InputInterface $input, OutputInterface $output)
126     {
127         $io = new DrupalStyle($input, $output);
128
129         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
130         if (!$this->confirmGeneration($io)) {
131             return;
132         }
133
134         $module = $input->getOption('module');
135         $class_name = $input->getOption('class');
136         $label = $input->getOption('label');
137         $plugin_id = $input->getOption('plugin-id');
138         $description = $input->getOption('description');
139         $default_widget = $input->getOption('default-widget');
140         $default_formatter = $input->getOption('default-formatter');
141
142         $this->generator
143             ->generate($module, $class_name, $label, $plugin_id, $description, $default_widget, $default_formatter);
144
145         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery'], false);
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.fieldtype.questions.class'),
165                 'ExampleFieldType'
166             );
167             $input->setOption('class', $class_name);
168         }
169
170         // --label option
171         $label = $input->getOption('label');
172         if (!$label) {
173             $label = $io->ask(
174                 $this->trans('commands.generate.plugin.fieldtype.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.fieldtype.questions.plugin-id'),
185                 $this->stringConverter->camelCaseToUnderscore($class_name)
186             );
187             $input->setOption('plugin-id', $plugin_id);
188         }
189
190         // --description option
191         $description = $input->getOption('description');
192         if (!$description) {
193             $description = $io->ask(
194                 $this->trans('commands.generate.plugin.fieldtype.questions.description'),
195                 'My Field Type'
196             );
197             $input->setOption('description', $description);
198         }
199
200         // --default-widget option
201         $default_widget = $input->getOption('default-widget');
202         if (!$default_widget) {
203             $default_widget = $io->askEmpty(
204                 $this->trans('commands.generate.plugin.fieldtype.questions.default-widget')
205             );
206             $input->setOption('default-widget', $default_widget);
207         }
208
209         // --default-formatter option
210         $default_formatter = $input->getOption('default-formatter');
211         if (!$default_formatter) {
212             $default_formatter = $io->askEmpty(
213                 $this->trans('commands.generate.plugin.fieldtype.questions.default-formatter')
214             );
215             $input->setOption('default-formatter', $default_formatter);
216         }
217     }
218 }