| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Eccube\Doctrine\Common\CsvDataFixtures; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\DataFixtures\FixtureInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * CSVファイルのローダー. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @see https://github.com/doctrine/data-fixtures/blob/master/lib/Doctrine/Common/DataFixtures/Loader.php | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class Loader | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     protected $fixtures; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * Load fixtures from directory. | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * 同一階層に, Fixture のロード順を定義した definition.yml が必要. | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param string $dir | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @return array fixtures. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function loadFromDirectory($dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if (!dir($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             throw new \InvalidArgumentException(sprintf('"%s" does not exist', $dir)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // import順序の定義ファイルを取得. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $file = $dir.'/definition.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (!file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             throw new \InvalidArgumentException(sprintf('"%s" does not exist', $file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $definition = Yaml::parse(file_get_contents($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $definition = array_flip($definition); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $finder = Finder::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->in($dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->name('*.csv') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->sort( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 // 定義ファイルに記載の順にソート. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 function (\SplFileInfo $a, \SplFileInfo $b) use ($definition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     if (!isset($definition[$a->getFilename()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                         throw new \Exception(sprintf('"%s" is undefined in %s', $a->getFilename())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     if (!isset($definition[$b->getFilename()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                         throw new \Exception(sprintf('"%s" is undefined in %s', $b->getFilename())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     $a_rank = $definition[$a->getFilename()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     $b_rank = $definition[$b->getFilename()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     if ($a_rank < $b_rank) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         return -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     } elseif ($a_rank > $b_rank) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                         return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->files(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $this->loadFromIterator($finder->getIterator()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * Load fixtures from Iterator. | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @param \Iterator $Iterator Iterator of \SplFileInfo  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @return array fixtures. | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function loadFromIterator(\Iterator $Iterator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $fixtures = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         foreach ($Iterator as $fixture) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             // TODO $fixture が \SplFileInfo ではない場合の対応 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $CsvFixture = new CsvFixture($fixture->openFile()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $this->addFixture($CsvFixture); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $fixtures[] = $CsvFixture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $fixtures; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function getFixtures() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->fixtures; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function addFixture(FixtureInterface $fixture) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->fixtures[] = $fixture; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                        
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.