| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * MIT License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * For full license information, please view the LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Phinx\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class SeedRun extends AbstractCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected static $defaultName = 'seed:run'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         parent::configure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->setDescription('Run database seeders') | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?') | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             ->setHelp( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 <<<EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | The <info>seed:run</info> command runs all available or individual seeders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | <info>phinx seed:run -e development</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | <info>phinx seed:run -e development -s UserSeeder</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 35 |  | <info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | <info>phinx seed:run -e development -v</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 35 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 35 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 35 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 35 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 35 |  |      * Run database seeders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 35 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param \Symfony\Component\Console\Input\InputInterface $input Input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param \Symfony\Component\Console\Output\OutputInterface $output Output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return int integer 0 on success, or an error code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->bootstrap($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 35 |  |         $seedSet = $input->getOption('seed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 35 |  |         $environment = $input->getOption('environment'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($environment === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $environment = $this->getConfig()->getDefaultEnvironment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $output->writeln('<info>using environment</info> ' . $environment); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |         if (!$this->getConfig()->hasEnvironment($environment)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return self::CODE_ERROR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $envOptions = $this->getConfig()->getEnvironment($environment); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |         if (isset($envOptions['adapter'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |             $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |         if (isset($envOptions['wrapper'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |         if (isset($envOptions['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |             $output->writeln('<info>using database</info> ' . $envOptions['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 4 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             return self::CODE_ERROR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 4 |  |         if (isset($envOptions['table_prefix'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |             $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         if (isset($envOptions['table_suffix'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |         $start = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (empty($seedSet)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |             // run all the seed(ers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->getManager()->seed($environment); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             // run seed(ers) specified in a comma-separated list of classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |             foreach ($seedSet as $seed) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->getManager()->seed($environment, trim($seed)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $end = microtime(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return self::CODE_SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  |