Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginMigrateSourceCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\PluginBlockCommand.
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 Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Generator\PluginMigrateSourceGenerator;
15 use Drupal\Console\Command\Shared\ModuleTrait;
16 use Drupal\Console\Command\Shared\ConfirmationTrait;
17 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
18 use Drupal\Console\Extension\Manager;
19 use Drupal\Console\Utils\Validator;
20 use Drupal\Console\Core\Utils\StringConverter;
21 use Drupal\Console\Core\Style\DrupalStyle;
22 use Drupal\Console\Core\Utils\ChainQueue;
23 use Drupal\Core\Config\ConfigFactory;
24 use Drupal\Core\Entity\EntityTypeManagerInterface;
25 use Drupal\Core\Render\ElementInfoManagerInterface;
26
27 class PluginMigrateSourceCommand extends Command
28 {
29     use ModuleTrait;
30     use ConfirmationTrait;
31     use ContainerAwareCommandTrait;
32
33     /**
34      * @var ConfigFactory
35      */
36     protected $configFactory;
37
38     /**
39      * @var ChainQueue
40      */
41     protected $chainQueue;
42
43     /**
44      * @var PluginMigrateSourceGenerator
45      */
46     protected $generator;
47
48     /**
49      * @var EntityTypeManagerInterface
50      */
51     protected $entityTypeManager;
52
53     /**
54      * @var Manager
55      */
56     protected $extensionManager;
57
58     /**
59      * @var Validator
60      */
61     protected $validator;
62
63     /**
64      * @var StringConverter
65      */
66     protected $stringConverter;
67
68     /**
69      * @var ElementInfoManagerInterface
70      */
71     protected $elementInfoManager;
72
73     /**
74      * PluginBlockCommand constructor.
75      *
76      * @param ConfigFactory               $configFactory
77      * @param ChainQueue                  $chainQueue
78      * @param PluginBlockGenerator        $generator
79      * @param EntityTypeManagerInterface  $entityTypeManager
80      * @param Manager                     $extensionManager
81      * @param Validator                   $validator
82      * @param StringConverter             $stringConverter
83      * @param ElementInfoManagerInterface $elementInfoManager
84      */
85     public function __construct(
86         ConfigFactory $configFactory,
87         ChainQueue $chainQueue,
88         PluginMigrateSourceGenerator $generator,
89         EntityTypeManagerInterface $entityTypeManager,
90         Manager $extensionManager,
91         Validator $validator,
92         StringConverter $stringConverter,
93         ElementInfoManagerInterface $elementInfoManager
94     ) {
95         $this->configFactory = $configFactory;
96         $this->chainQueue = $chainQueue;
97         $this->generator = $generator;
98         $this->entityTypeManager = $entityTypeManager;
99         $this->extensionManager = $extensionManager;
100         $this->validator = $validator;
101         $this->stringConverter = $stringConverter;
102         $this->elementInfoManager = $elementInfoManager;
103         parent::__construct();
104     }
105
106     protected function configure()
107     {
108         $this
109             ->setName('generate:plugin:migrate:source')
110             ->setDescription($this->trans('commands.generate.plugin.migrate.source.description'))
111             ->setHelp($this->trans('commands.generate.plugin.migrate.source.help'))
112             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
113             ->addOption(
114                 'class',
115                 null,
116                 InputOption::VALUE_OPTIONAL,
117                 $this->trans('commands.generate.plugin.migrate.source.options.class')
118             )
119             ->addOption(
120                 'plugin-id',
121                 null,
122                 InputOption::VALUE_OPTIONAL,
123                 $this->trans('commands.generate.plugin.migrate.source.options.plugin-id')
124             )
125             ->addOption(
126                 'table',
127                 null,
128                 InputOption::VALUE_OPTIONAL,
129                 $this->trans('commands.generate.plugin.migrate.source.options.table')
130             )
131             ->addOption(
132                 'alias',
133                 null,
134                 InputOption::VALUE_OPTIONAL,
135                 $this->trans('commands.generate.plugin.migrate.source.options.alias')
136             )
137             ->addOption(
138                 'group-by',
139                 null,
140                 InputOption::VALUE_OPTIONAL,
141                 $this->trans('commands.generate.plugin.migrate.source.options.group-by')
142             )
143             ->addOption(
144                 'fields',
145                 null,
146                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
147                 $this->trans('commands.generate.plugin.migrate.source.options.fields')
148             );
149     }
150
151     /**
152      * {@inheritdoc}
153      */
154     protected function execute(InputInterface $input, OutputInterface $output)
155     {
156         $io = new DrupalStyle($input, $output);
157
158         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
159         if (!$this->confirmGeneration($io)) {
160             return 1;
161         }
162
163         $module = $input->getOption('module');
164         $class_name = $input->getOption('class');
165         $plugin_id = $input->getOption('plugin-id');
166         $table = $input->getOption('table');
167         $alias = $input->getOption('alias');
168         $group_by = $input->getOption('group-by');
169         $fields = $input->getOption('fields');
170
171         $this->generator
172             ->generate(
173                 $module,
174                 $class_name,
175                 $plugin_id,
176                 $table,
177                 $alias,
178                 $group_by,
179                 $fields
180             );
181
182         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
183     }
184
185     protected function interact(InputInterface $input, OutputInterface $output)
186     {
187         $io = new DrupalStyle($input, $output);
188
189         $module = $input->getOption('module');
190         if (!$module) {
191             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
192             $module = $this->moduleQuestion($io);
193             $input->setOption('module', $module);
194         }
195
196         $class = $input->getOption('class');
197         if (!$class) {
198             $class = $io->ask(
199                 $this->trans('commands.generate.plugin.migrate.source.questions.class'),
200                 ucfirst($this->stringConverter->underscoreToCamelCase($module)),
201                 function ($class) {
202                     return $this->validator->validateClassName($class);
203                 }
204             );
205             $input->setOption('class', $class);
206         }
207
208         $pluginId = $input->getOption('plugin-id');
209         if (!$pluginId) {
210             $pluginId = $io->ask(
211                 $this->trans('commands.generate.plugin.migrate.source.questions.plugin-id'),
212                 $this->stringConverter->camelCaseToUnderscore($class)
213             );
214             $input->setOption('plugin-id', $pluginId);
215         }
216
217         $table = $input->getOption('table');
218         if (!$table) {
219             $table = $io->ask(
220                 $this->trans('commands.generate.plugin.migrate.source.questions.table'),
221                 ''
222             );
223             $input->setOption('table', $table);
224         }
225
226         $alias = $input->getOption('alias');
227         if (!$alias) {
228             $alias = $io->ask(
229                 $this->trans('commands.generate.plugin.migrate.source.questions.alias'),
230                 substr($table, 0, 1)
231             );
232             $input->setOption('alias', $alias);
233         }
234
235         $groupBy = $input->getOption('group-by');
236         if ($groupBy == '') {
237             $groupBy = $io->ask(
238                 $this->trans('commands.generate.plugin.migrate.source.questions.group-by'),
239                 false
240             );
241             $input->setOption('group-by', $groupBy);
242         }
243
244         $fields = $input->getOption('fields');
245         if (!$fields) {
246             $fields = [];
247             while (true) {
248                 $id = $io->ask(
249                     $this->trans('commands.generate.plugin.migrate.source.questions.fields.id'),
250                     false
251                 );
252                 if (!$id) {
253                     break;
254                 }
255                 $description = $io->ask(
256                     $this->trans('commands.generate.plugin.migrate.source.questions.fields.description'),
257                     $id
258                 );
259                 $fields[] = [
260                     'id' => $id,
261                     'description' => $description,
262                 ];
263             }
264             $input->setOption('fields', $fields);
265         }
266     }
267 }