Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Migrate / SetupCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Migrate\SetupCommand.
6  */
7
8 namespace Drupal\Console\Command\Migrate;
9
10 use Drupal\Console\Core\Style\DrupalStyle;
11 use Drupal\Core\State\StateInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
17 use Drupal\Console\Command\Shared\DatabaseTrait;
18 use Drupal\Console\Command\Shared\MigrationTrait;
19 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
20 use Drupal\migrate\Plugin\RequirementsInterface;
21 use Drupal\migrate\Exception\RequirementsException;
22 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
23
24 class SetupCommand extends Command
25 {
26     use ContainerAwareCommandTrait;
27     use DatabaseTrait;
28     use MigrationTrait;
29
30     /**
31      * @var StateInterface $state
32      */
33     protected $state;
34
35     /**
36      * @var MigrationPluginManagerInterface $pluginManagerMigration
37      */
38     protected $pluginManagerMigration;
39
40     /**
41      * SetupCommand constructor.
42      *
43      * @param StateInterface $pluginManagerMigration
44      */
45     public function __construct(StateInterface $state, MigrationPluginManagerInterface $pluginManagerMigration)
46     {
47         $this->state = $state;
48         $this->pluginManagerMigration = $pluginManagerMigration;
49         parent::__construct();
50     }
51
52     protected function configure()
53     {
54         $this
55             ->setName('migrate:setup')
56             ->setDescription($this->trans('commands.migrate.setup.description'))
57             ->addOption(
58                 'db-type',
59                 null,
60                 InputOption::VALUE_REQUIRED,
61                 $this->trans('commands.migrate.setup.options.db-type')
62             )
63             ->addOption(
64                 'db-host',
65                 null,
66                 InputOption::VALUE_REQUIRED,
67                 $this->trans('commands.migrate.setup.options.db-host')
68             )
69             ->addOption(
70                 'db-name',
71                 null,
72                 InputOption::VALUE_REQUIRED,
73                 $this->trans('commands.migrate.setup.options.db-name')
74             )
75             ->addOption(
76                 'db-user',
77                 null,
78                 InputOption::VALUE_REQUIRED,
79                 $this->trans('commands.migrate.setup.options.db-user')
80             )
81             ->addOption(
82                 'db-pass',
83                 null,
84                 InputOption::VALUE_OPTIONAL,
85                 $this->trans('commands.migrate.setup.options.db-pass')
86             )
87             ->addOption(
88                 'db-prefix',
89                 null,
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.migrate.setup.options.db-prefix')
92             )
93             ->addOption(
94                 'db-port',
95                 null,
96                 InputOption::VALUE_REQUIRED,
97                 $this->trans('commands.migrate.setup.options.db-port')
98             )
99             ->addOption(
100                 'source-base_path',
101                 null,
102                 InputOption::VALUE_OPTIONAL,
103                 $this->trans('commands.migrate.setup.options.source-base_path')
104             );
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     protected function interact(InputInterface $input, OutputInterface $output)
111     {
112         $io = new DrupalStyle($input, $output);
113
114         // --db-type option
115         $db_type = $input->getOption('db-type');
116         if (!$db_type) {
117             $db_type = $this->dbDriverTypeQuestion($io);
118             $input->setOption('db-type', $db_type);
119         }
120
121         // --db-host option
122         $db_host = $input->getOption('db-host');
123         if (!$db_host) {
124             $db_host = $this->dbHostQuestion($io);
125             $input->setOption('db-host', $db_host);
126         }
127
128         // --db-name option
129         $db_name = $input->getOption('db-name');
130         if (!$db_name) {
131             $db_name = $this->dbNameQuestion($io);
132             $input->setOption('db-name', $db_name);
133         }
134
135         // --db-user option
136         $db_user = $input->getOption('db-user');
137         if (!$db_user) {
138             $db_user = $this->dbUserQuestion($io);
139             $input->setOption('db-user', $db_user);
140         }
141
142         // --db-pass option
143         $db_pass = $input->getOption('db-pass');
144         if (!$db_pass) {
145             $db_pass = $this->dbPassQuestion($io);
146             $input->setOption('db-pass', $db_pass);
147         }
148
149         // --db-prefix
150         $db_prefix = $input->getOption('db-prefix');
151         if (!$db_prefix) {
152             $db_prefix = $this->dbPrefixQuestion($io);
153             $input->setOption('db-prefix', $db_prefix);
154         }
155
156         // --db-port prefix
157         $db_port = $input->getOption('db-port');
158         if (!$db_port) {
159             $db_port = $this->dbPortQuestion($io);
160             $input->setOption('db-port', $db_port);
161         }
162
163         // --source-base_path
164         $sourceBasepath = $input->getOption('source-base_path');
165         if (!$sourceBasepath) {
166             $sourceBasepath = $io->ask(
167                 $this->trans('commands.migrate.setup.questions.source-base_path'),
168                 ''
169             );
170             $input->setOption('source-base_path', $sourceBasepath);
171         }
172     }
173
174     protected function execute(InputInterface $input, OutputInterface $output)
175     {
176         $io = new DrupalStyle($input, $output);
177
178         $sourceBasepath = $input->getOption('source-base_path');
179         $configuration['source']['constants']['source_base_path'] = rtrim($sourceBasepath, '/') . '/';
180
181         $this->registerMigrateDB($input, $io);
182         $this->migrateConnection = $this->getDBConnection($io, 'default', 'upgrade');
183
184         if (!$drupal_version = $this->getLegacyDrupalVersion($this->migrateConnection)) {
185             $io->error($this->trans('commands.migrate.setup.migrations.questions.not-drupal'));
186             return 1;
187         }
188         
189         $database = $this->getDBInfo();
190         $version_tag = 'Drupal ' . $drupal_version;
191         
192         $this->createDatabaseStateSettings($database, $drupal_version);
193         
194         $migrations  = $this->getMigrations($version_tag, false, $configuration);
195         
196         if ($migrations) {
197             $io->info(
198                 sprintf(
199                     $this->trans('commands.migrate.setup.messages.migrations-created'),
200                     count($migrations),
201                     $version_tag
202                 )
203             );
204         }
205
206         return 0;
207     }
208 }