| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SLLH\IsoCodesValidator\Dev\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Gnugat\NomoSpaco\File\FileRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Gnugat\NomoSpaco\FqcnRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Gnugat\NomoSpaco\Token\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Sullivan Senechal <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | final class GenerateCommand extends Command | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             ->setName('generate') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $isoCodePath = __DIR__.'/../../../vendor/ronanguilloux/isocodes'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if (!file_exists($isoCodePath.'/.git')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 '<error>No .git directory found on ronanguilloux/isocodes package. Please run rm -r vendor/ronanguilloux/isocodes && composer update ronanguilloux/isocodes --prefer-source command.</error>' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $fqcnRepository = new FqcnRepository(new FileRepository(), new ParserFactory()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $isoCodesClasses = array_map(function ($fqcn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             return str_replace('IsoCodes\\', '', $fqcn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         }, $fqcnRepository->findIn(__DIR__.'/../../../vendor/ronanguilloux/isocodes/src/IsoCodes')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $constraintClasses = array_filter(array_map(function ($fqcn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             return str_replace('SLLH\\IsoCodesValidator\\Constraints\\', '', $fqcn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         }, $fqcnRepository->findIn(__DIR__.'/../../../src/Constraints')), function ($className) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return !empty(trim($className)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/../..')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         foreach (array_udiff($isoCodesClasses, $constraintClasses, 'strcasecmp') as $className) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $classVersion = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 'v', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 exec("git -C ${isoCodePath} tag --list --contains $(git -C ${isoCodePath} log --pretty=format:\"%h\" --diff-filter=A src/IsoCodes/${className}.php) | head -n 1") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $output->writeln("Generate <comment>${className}</comment> constraint (<info>v${classVersion}</info>)."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 __DIR__."/../../../src/Constraints/${className}.php", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $twig->render('templates/Constraint.php.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     'class_name'    => $className, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     'class_version' => $classVersion, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 __DIR__."/../../../tests/Constraints/${className}ValidatorTest.php", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $twig->render('templates/ConstraintValidatorTest.php.twig', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     'class_name' => $className, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return 0; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                        
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.