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