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