Yaffs site version 1.1
[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', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
84             ->addOption(
85                 'class',
86                 null,
87                 InputOption::VALUE_REQUIRED,
88                 $this->trans('commands.generate.plugin.fieldtype.options.class')
89             )
90             ->addOption(
91                 'label',
92                 null,
93                 InputOption::VALUE_OPTIONAL,
94                 $this->trans('commands.generate.plugin.fieldtype.options.label')
95             )
96             ->addOption(
97                 'plugin-id',
98                 null,
99                 InputOption::VALUE_OPTIONAL,
100                 $this->trans('commands.generate.plugin.fieldtype.options.plugin-id')
101             )
102             ->addOption(
103                 'description',
104                 null,
105                 InputOption::VALUE_OPTIONAL,
106                 $this->trans('commands.generate.plugin.fieldtype.options.description')
107             )
108             ->addOption(
109                 'default-widget',
110                 null,
111                 InputOption::VALUE_OPTIONAL,
112                 $this->trans('commands.generate.plugin.fieldtype.options.default-widget')
113             )
114             ->addOption(
115                 'default-formatter',
116                 null,
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 1;
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         return 0;
148     }
149
150     protected function interact(InputInterface $input, OutputInterface $output)
151     {
152         $io = new DrupalStyle($input, $output);
153
154         // --module option
155         $module = $input->getOption('module');
156         if (!$module) {
157             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
158             $module = $this->moduleQuestion($io);
159             $input->setOption('module', $module);
160         }
161
162         // --class option
163         $class_name = $input->getOption('class');
164         if (!$class_name) {
165             $class_name = $io->ask(
166                 $this->trans('commands.generate.plugin.fieldtype.questions.class'),
167                 'ExampleFieldType'
168             );
169             $input->setOption('class', $class_name);
170         }
171
172         // --label option
173         $label = $input->getOption('label');
174         if (!$label) {
175             $label = $io->ask(
176                 $this->trans('commands.generate.plugin.fieldtype.questions.label'),
177                 $this->stringConverter->camelCaseToHuman($class_name)
178             );
179             $input->setOption('label', $label);
180         }
181
182         // --plugin-id option
183         $plugin_id = $input->getOption('plugin-id');
184         if (!$plugin_id) {
185             $plugin_id = $io->ask(
186                 $this->trans('commands.generate.plugin.fieldtype.questions.plugin-id'),
187                 $this->stringConverter->camelCaseToUnderscore($class_name)
188             );
189             $input->setOption('plugin-id', $plugin_id);
190         }
191
192         // --description option
193         $description = $input->getOption('description');
194         if (!$description) {
195             $description = $io->ask(
196                 $this->trans('commands.generate.plugin.fieldtype.questions.description'),
197                 'My Field Type'
198             );
199             $input->setOption('description', $description);
200         }
201
202         // --default-widget option
203         $default_widget = $input->getOption('default-widget');
204         if (!$default_widget) {
205             $default_widget = $io->askEmpty(
206                 $this->trans('commands.generate.plugin.fieldtype.questions.default-widget')
207             );
208             $input->setOption('default-widget', $default_widget);
209         }
210
211         // --default-formatter option
212         $default_formatter = $input->getOption('default-formatter');
213         if (!$default_formatter) {
214             $default_formatter = $io->askEmpty(
215                 $this->trans('commands.generate.plugin.fieldtype.questions.default-formatter')
216             );
217             $input->setOption('default-formatter', $default_formatter);
218         }
219     }
220 }