| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WebComplete\core\utils\migration\commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Question\ConfirmationQuestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WebComplete\core\cube\CubeManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WebComplete\core\utils\migration\MigrationService; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class MigrationDownCommand extends Command | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @var MigrationService | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     protected $migrationService; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @param MigrationService $migrationService | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @throws \Symfony\Component\Console\Exception\LogicException | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(MigrationService $migrationService) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         parent::__construct(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |         $this->migrationService = $migrationService; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @throws \Symfony\Component\Console\Exception\InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->setName('migrate:down') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->setDescription('Rollback migration[s]') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |             ->addArgument('class', InputArgument::OPTIONAL, 'Migration class'); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @param InputInterface $input | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * @param OutputInterface $output | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @return null|int | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @throws \Symfony\Component\Console\Exception\LogicException | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @throws \Symfony\Component\Console\Exception\InvalidArgumentException | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @throws \Psr\Container\NotFoundExceptionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \Psr\Container\ContainerExceptionInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if ($class = $input->getArgument('class')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $rollbackMigrations = [$class]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $rollbackMigrations = $this->migrationService->getRegistered(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $rollbackMigrations = \array_reverse($rollbackMigrations, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (!$rollbackMigrations) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $output->writeln('Migrations not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $message = "Rollback migrations: \n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         foreach ($rollbackMigrations as $migrationClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $message .= $migrationClass . "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $output->writeln($message . "\n"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $helper = $this->getHelper('question'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $question = new ConfirmationQuestion('Continue? (Y/N): ', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($class || $helper->ask($input, $output, $question)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             foreach ($rollbackMigrations as $migrationClass) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $output->writeln('Rollback: ' . $migrationClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->migrationService->down($migrationClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $output->writeln('Done!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $output->writeln('Cancel'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |