| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Gacela\Framework\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Gacela\Framework\AbstractFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Gacela\Framework\Bootstrap\SetupGacelaInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigFromBootstrapFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigUsingGacelaPhpFileFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Gacela\Framework\Config\PathNormalizer\AbsolutePathNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Gacela\Framework\Config\PathNormalizer\WithoutSuffixAbsolutePathStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Gacela\Framework\Config\PathNormalizer\WithSuffixAbsolutePathStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class ConfigFactory extends AbstractFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private const GACELA_PHP_CONFIG_FILENAME = 'gacela'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private const GACELA_PHP_CONFIG_EXTENSION = '.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private static ?GacelaConfigFileInterface $gacelaFileConfig = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         private string $appRootDir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         private SetupGacelaInterface $setup, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public static function resetCache(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self::$gacelaFileConfig = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function createConfigLoader(): ConfigLoader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return new ConfigLoader( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $this->createGacelaFileConfig(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $this->createPathFinder(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->createPathNormalizer(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function createGacelaFileConfig(): GacelaConfigFileInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (self::$gacelaFileConfig !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             return self::$gacelaFileConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $gacelaConfigFiles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $fileIo = $this->createFileIo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $gacelaPhpDefaultPath = $this->getGacelaPhpDefaultPath(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if ($fileIo->existsFile($gacelaPhpDefaultPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 $gacelaPhpDefaultPath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 $this->setup, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $fileIo, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $gacelaPhpPath = $this->getGacelaPhpPathFromEnv(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if ($fileIo->existsFile($gacelaPhpPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         self::$gacelaFileConfig = array_reduce( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $gacelaConfigFiles, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             static fn (GacelaConfigFileInterface $carry, GacelaConfigFileInterface $item) => $carry->combine($item), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             (new GacelaConfigFromBootstrapFactory($this->setup))->createGacelaFileConfig(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         return self::$gacelaFileConfig; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     private function createFileIo(): FileIoInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         return new FileIo(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     private function getGacelaPhpDefaultPath(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             '%s/%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $this->appRootDir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             self::GACELA_PHP_CONFIG_FILENAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             self::GACELA_PHP_CONFIG_EXTENSION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function getGacelaPhpPathFromEnv(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             '%s/%s-%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $this->appRootDir, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             self::GACELA_PHP_CONFIG_FILENAME, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->env(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             self::GACELA_PHP_CONFIG_EXTENSION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     private function env(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return getenv('APP_ENV') ?: ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function createPathFinder(): PathFinderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return new PathFinder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     private function createPathNormalizer(): PathNormalizerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return new AbsolutePathNormalizer([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |