| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the box project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (c) Kevin Herrera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *     Théo Fidry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace KevinGH\Box\RequirementChecker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Finder\SplFileInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function KevinGH\Box\FileSystem\file_contents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use function str_replace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use function var_export; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @private | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | final class RequirementsDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public const CHECK_FILE_NAME = 'check_requirements.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private const REQUIREMENTS_CHECKER_TEMPLATE = <<<'PHP' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * This file is part of the box project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * (c) Kevin Herrera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *     Théo Fidry <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * This source file is subject to the MIT license that is bundled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  * with this source code in the file LICENSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | namespace KevinGH\Box\RequirementChecker; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | require 'bin/check-requirements.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | PHP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private const REQUIREMENTS_CONFIG_TEMPLATE = <<<'PHP' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | return __CONFIG__; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | PHP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     private const REQUIRMEMENT_CHECKER_PATH = __DIR__.'/../../.requirement-checker'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return string[][] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public static function dump(array $composerLockDecodedContents): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $filesWithContents = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             self::dumpRequirementsConfig($composerLockDecodedContents), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             [self::CHECK_FILE_NAME, self::REQUIREMENTS_CHECKER_TEMPLATE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         /** @var SplFileInfo[] $requirementCheckerFiles */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $requirementCheckerFiles = Finder::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->in(self::REQUIRMEMENT_CHECKER_PATH) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($requirementCheckerFiles as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $filesWithContents[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $file->getRelativePathname(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 file_contents($file->getPathname()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $filesWithContents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     private static function dumpRequirementsConfig(array $composerLockDecodedContents): array | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $config = AppRequirementsFactory::create($composerLockDecodedContents); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             '.requirements.php', | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             str_replace( | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 '__CONFIG__', | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 var_export($config, true), | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 self::REQUIREMENTS_CONFIG_TEMPLATE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |