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