| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Facile\MongoDbBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Facile\MongoDbBundle\Fixtures\FixtureSorter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Facile\MongoDbBundle\Fixtures\MongoFixtureInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Facile\MongoDbBundle\Fixtures\MongoFixturesLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Bundle\FrameworkBundle\Console\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpKernel\KernelInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class LoadFixturesCommand. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class LoadFixturesCommand extends AbstractCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var MongoFixturesLoader */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 8 |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 8 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 8 |  |         parent::configure(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 8 |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 8 |  |             ->setName('mongodb:fixtures:load') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |             ->addArgument('addFixturesPath', InputArgument::OPTIONAL, 'Add a path to search in for fixtures files') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ->setDescription('Load fixtures and applies them'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |     protected function initialize(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |         parent::initialize($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->loader = new MongoFixturesLoader($this->getContainer()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |         $this->io->writeln('Loading mongo fixtures'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         /** @var Application $application */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |         $application = $this->getApplication(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         $paths = $this->prepareSearchPaths($input, $application->getKernel()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         $this->loadPaths($paths); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         $fixtures = $this->loader->getLoadedClasses(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         if (empty($fixtures)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw new \InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 sprintf('Could not find any class to load in: %s', "\n\n- " . implode("\n- ", $paths)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         foreach (FixtureSorter::sort($fixtures) as $fixture) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             $this->loadFixture($fixture); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         $this->io->writeln(sprintf('Done, loaded %d fixtures files', \count($fixtures))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |      * @param MongoFixtureInterface $indexList | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |     private function loadFixture(MongoFixtureInterface $indexList) | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 2 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 2 |  |         $indexList->loadData(); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 2 |  |         $indexList->loadIndexes(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $this->io->writeln('Loaded fixture: ' . \get_class($indexList)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private function prepareSearchPaths(InputInterface $input, KernelInterface $kernel): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $paths = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         if ($input->getArgument('addFixturesPath')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $paths[] = $input->getArgument('addFixturesPath'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |         foreach ($kernel->getBundles() as $bundle) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |             $paths[] = $bundle->getPath() . '/DataFixtures/Mongo'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |         return $paths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     private function loadPaths(array $paths) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if (is_dir($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $this->loader->loadFromDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if (is_file($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |                 $this->loader->loadFromFile($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |