91a2fe202a362763b11a99e826f7e3cfd36b8183
[yaffs-website] / vendor / drupal / console / src / Command / Migrate / RollBackCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Migrate\RollBackCommand
6  */
7
8 namespace Drupal\Console\Command\Migrate;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Command\Shared\MigrationTrait;
15 use Drupal\Console\Core\Style\DrupalStyle;
16 use Drupal\Console\Annotations\DrupalCommand;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Command\Shared\CommandTrait;
19 use Drupal\migrate_plus\Entity\MigrationGroup;
20 use Drupal\migrate\Plugin\MigrationInterface;
21 use Drupal\migrate\MigrateExecutable;
22 use Drupal\Console\Utils\MigrateExecuteMessageCapture;
23 use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
24
25 /**
26  * @DrupalCommand(
27  *     extension = "migrate",
28  *     extensionType = "module"
29  * )
30  */
31
32 class RollBackCommand extends Command
33 {
34     use MigrationTrait;
35     use CommandTrait;
36
37     /**
38      * @var MigrationPluginManagerInterface $pluginManagerMigration
39      */
40     protected $pluginManagerMigration;
41
42     /**
43      * RollBackCommand constructor.
44      *
45      * @param MigrationPluginManagerInterface $pluginManagerMigration
46      */
47     public function __construct(MigrationPluginManagerInterface $pluginManagerMigration)
48     {
49         $this->pluginManagerMigration = $pluginManagerMigration;
50         parent::__construct();
51     }
52
53     protected function configure()
54     {
55         $this
56             ->setName('migrate:rollback')
57             ->setDescription($this->trans('commands.migrate.rollback.description'))
58             ->addArgument('migration-ids', InputArgument::IS_ARRAY, $this->trans('commands.migrate.rollback.arguments.id'))
59             ->addOption(
60                 'source-base_path',
61                 null,
62                 InputOption::VALUE_OPTIONAL,
63                 $this->trans('commands.migrate.setup.options.source-base_path')
64             );
65     }
66
67     protected function execute(InputInterface $input, OutputInterface $output)
68     {
69         $io = new DrupalStyle($input, $output);
70
71         $sourceBasepath = $input->getOption('source-base_path');
72         $configuration['source']['constants']['source_base_path'] = rtrim($sourceBasepath, '/') . '/';
73         // --migration-id prefix
74         $migration_id = $input->getArgument('migration-ids');
75         $migrations_list = array_keys($this->getMigrations($version_tag));
76         // If migrations weren't provided finish execution
77         if (empty($migration_id)) {
78             return 1;
79         }
80
81
82         if (!in_array('all', $migration_id)) {
83             $migration_ids = $migration_id;
84         } else {
85             $migration_ids = $migrations_list;
86         }
87
88         foreach ($migration_ids as  $migration) {
89             if (!in_array($migration, $migrations_list)) {
90                 $io->warning(
91                     sprintf(
92                         $this->trans('commands.migrate.rollback.messages.not-available'),
93                         $migration
94                     )
95                 );
96                 continue;
97             }
98             $migration_service = $this->pluginManagerMigration->createInstance($migration, $configuration);
99             if ($migration_service) {
100                 $messages = new MigrateExecuteMessageCapture();
101                 $executable = new MigrateExecutable($migration_service, $messages);
102
103                 $migration_status = $executable->rollback();
104                 switch ($migration_status) {
105                 case MigrationInterface::RESULT_COMPLETED:
106                     $io->info(
107                         sprintf(
108                             $this->trans('commands.migrate.rollback.messages.processing'),
109                             $migration
110                         )
111                     );
112                     break;
113                 case MigrationInterface::RESULT_INCOMPLETE:
114                     $io->info(
115                         sprintf(
116                             $this->trans('commands.migrate.execute.messages.importing-incomplete'),
117                             $migration
118                         )
119                     );
120                     break;
121                 case MigrationInterface::RESULT_STOPPED:
122                     $io->error(
123                         sprintf(
124                             $this->trans('commands.migrate.execute.messages.import-stopped'),
125                             $migration
126                         )
127                     );
128                     break;
129                 }
130             }
131         }
132
133         return 0;
134     }
135
136     /**
137      * {@inheritdoc}
138      */
139     protected function interact(InputInterface $input, OutputInterface $output)
140     {
141         $io = new DrupalStyle($input, $output);
142         // Get migrations
143         $migrations_list = $this->getMigrations($version_tag);
144
145         // --migration-id prefix
146         $migration_id = $input->getArgument('migration-ids');
147
148
149         if (!$migration_id) {
150             $migrations_ids = [];
151
152             while (true) {
153                 $migration_id = $io->choiceNoList(
154                     $this->trans('commands.migrate.execute.questions.id'),
155                     array_keys($migrations_list),
156                     'all'
157                 );
158
159                 if (empty($migration_id) || $migration_id == 'all') {
160                     // Only add all if it's the first option
161                     if (empty($migrations_ids) && $migration_id == 'all') {
162                         $migrations_ids[] = $migration_id;
163                     }
164                     break;
165                 } else {
166                     $migrations_ids[] = $migration_id;
167                 }
168             }
169
170             $input->setArgument('migration-ids', $migrations_ids);
171         }
172
173         // --source-base_path
174         $sourceBasepath = $input->getOption('source-base_path');
175         if (!$sourceBasepath) {
176             $sourceBasepath = $io->ask(
177                 $this->trans('commands.migrate.setup.questions.source-base_path'),
178                 ''
179             );
180             $input->setOption('source-base_path', $sourceBasepath);
181         }
182     }
183 }