| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Console\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Console\Traits\DbHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * MigrateDownCommand | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class MigrateDownCommand extends Command | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     use DbHelper; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Configuration of command | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ->setName('migrate:down') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->setDescription('Command for rollback migration') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->addArgument('migration', InputArgument::REQUIRED, 'What kind of migration do you want to rollback?') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         ; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * Execute method of command | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @param InputInterface $input | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param OutputInterface $output | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $migrationName = $input->getArgument('migration'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $file = MIGRATIONS_PATH.'/'.$migrationName.'.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (false === file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             throw new \RunTimeException('This migration not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (!$this->isRowExist($migrationName, $this->migrationsTable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             throw new \RunTimeException('This migration not applied'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $fileNamePieces = explode('_', $migrationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         require_once($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $class = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($fileNamePieces as $key => $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             if ($key == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $class .= ucfirst($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $obj = new $class(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $obj->down(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->deleteRow($migrationName, $this->migrationsTable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $output->writeln([sprintf('<info>Rollback `%s` migration - done</info>', $migrationName)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |