| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Bone\BoneDoctrine\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\Common\DataFixtures\FixtureInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\Common\DataFixtures\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\Common\DataFixtures\Purger\ORMPurger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class LoadFixturesCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private EntityManager $entityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private array $fixtures; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 1 |  |     public function __construct(EntityManager $entityManager, array $fixtures = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 1 |  |         parent::__construct('migrant:fixtures'); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |         $this->entityManager = $entityManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 1 |  |         $this->fixtures = $fixtures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $this->setDescription('[fixtures] Loads data fixtures.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $this->setHelp('Loads data fixtures.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $this->addOption('purge', 'p', InputOption::VALUE_OPTIONAL, 'Purge the database before seeding', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $output->writeln('💀 Bone Framework database fixtures'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $output->writeln('   Loading fixtures....'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $loader = new Loader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if (\count($this->fixtures) === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $output->writeln('🤷♂️ No fixtures found, exiting.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return Command::SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         foreach ($this->fixtures as $fixture) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $instance = new $fixture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             if ($instance instanceof FixtureInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 $output->writeln('   <info>Executing fixture ' . $fixture . '</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $loader->addFixture($instance); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $executor = new ORMExecutor($this->entityManager, new ORMPurger()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $executor->execute($loader->getFixtures(), $input->getOption('purge')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return Command::SUCCESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |