| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Hautelook\AliceBundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Baldur Rensch <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Hautelook\AliceBundle\Doctrine\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\DependencyInjection\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Finder\Finder as SymfonyFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\HttpKernel\Bundle\BundleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Extends its parent class to take into account doctrine data loaders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Théo FIDRY <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | class FixturesFinder extends \Hautelook\AliceBundle\Finder\FixturesFinder implements ContainerAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var ContainerInterface|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 168 |  |     public function setContainer(ContainerInterface $container = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 168 |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 168 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Extended to look for data loaders. If a data loader is found, will take the fixtures from it instead of taking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * all the fixtures files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 117 |  |     public function getFixturesFromDirectory($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 117 |  |         $fixtures = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 117 |  |         $loaders = $this->getDataLoadersFromDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 114 |  |         foreach ($loaders as $loader) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 96 |  |             $fixtures = array_merge($fixtures, $loader->getFixtures()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 114 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // If no data loader is found, takes all fixtures files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 114 |  |         if (0 === count($loaders)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 75 |  |             return parent::getFixturesFromDirectory($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 96 |  |         return $fixtures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Gets all data loaders instances. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * For first get all the path for where to look for data loaders. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param BundleInterface[] $bundles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string            $environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return LoaderInterface[] Fixtures files real paths. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 87 |  |     public function getDataLoaders(array $bundles, $environment) | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 87 |  |         $loadersPaths = $this->getLoadersPaths($bundles, $environment); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         // Add all fixtures to the new Doctrine loader | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 87 |  |         $loaders = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 87 |  |         foreach ($loadersPaths as $path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 87 |  |             $loaders = array_merge($loaders, $this->getDataLoadersFromDirectory($path)); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 87 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 87 |  |         return $loaders; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Get data loaders inside the given directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $path Directory path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return LoaderInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 126 |  |     private function getDataLoadersFromDirectory($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 126 |  |         $loaders = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         // Get all PHP classes in given folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 126 |  |         $phpClasses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 126 |  |         $finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 123 |  |         foreach ($finder as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             /* @var SplFileInfo $file */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 102 |  |             $phpClasses[$file->getRealPath()] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 102 |  |             require_once $file->getRealPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 123 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         // Check if PHP classes are data loaders or not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 123 |  |         foreach (get_declared_classes() as $className) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 123 |  |             $reflectionClass = new \ReflectionClass($className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 123 |  |             $sourceFile = $reflectionClass->getFileName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 123 |  |             if (true === isset($phpClasses[$sourceFile])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 102 |  |                 if ($reflectionClass->implementsInterface('Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 102 |  |                     $loader = new $className(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 102 |  |                     $loaders[$className] = $loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 102 |  |                     if ($loader instanceof ContainerAwareInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 102 |  |                         $loader->setContainer($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 102 |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 102 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 102 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 123 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 123 |  |         return $loaders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  |