| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace CarstenWindler\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use CarstenWindler\Config\Exception\ConfigErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use CarstenWindler\Config\Exception\ConfigKeyNotSetException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Config implements ConfigInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private $config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $configPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $strictMode = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 1 |  |     public function setConfigPath(string $configPath): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |         $this->configPath = $configPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Set Config into strict mode, which means: if config keys are NOT set, Config will not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * return the default value, but throw an exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * You should not activate this in production, but use it in local dev and for CI to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * identify setup problems. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |     public function useStrictMode(): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |         $this->strictMode = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 23 |  |     private function load(string $file): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 23 |  |         if (!empty($this->configPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             $file = $this->configPath . '/' . $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 23 |  |         if (!file_exists($file)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             throw new ConfigErrorException('Config ' . $file . ' not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 22 |  |         $configArray = include($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 22 |  |         if (!is_array($configArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             throw new ConfigErrorException('Config ' . $file . ' contains no array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 21 |  |         return $configArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 23 |  |     public function addConfigFile(string $file): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 23 |  |         $this->addConfigArray($this->load($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 21 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |     public function set(string $key, $value): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         $loc = &$this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         foreach (explode('.', $key) as $step) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             $loc = &$loc[$step]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         $loc = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 24 |  |     public function addConfigArray(array $config): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 24 |  |         $this->config = $this->arrayMergeRecursiveDistinct($this->config, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 24 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 17 |  |     public function get(string $key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 17 |  |         $loc = &$this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 17 |  |         foreach (explode('.', $key) as $step) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 17 |  |             if (array_key_exists($step, $loc)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 12 |  |                 $loc = &$loc[$step]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 5 |  |             if ($this->strictMode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                 throw new ConfigKeyNotSetException('Config key ' . $key . ' not set'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  |             return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 12 |  |         return $loc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 5 |  |     public function has(string $key): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 5 |  |         $loc = &$this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 5 |  |         foreach (explode('.', $key) as $step) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 5 |  |             if (!array_key_exists($step, $loc)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |             $loc = &$loc[$step]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 4 |  |     public function toArray(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  |         return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |     public function clear(): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         $this->config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 24 |  |     private function arrayMergeRecursiveDistinct(array &$array1, array &$array2): array | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 24 |  |         $merged = $array1; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 24 |  |         foreach ($array2 as $key => &$value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 | 24 |  |             if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 1 |  |                 $merged [$key] = $this->arrayMergeRecursiveDistinct($merged[$key], $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 | 1 |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 24 |  |             $merged[$key] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 24 |  |         return $merged; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |