| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Micro | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author    Fabian Jucker <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @license   MIT https://opensource.org/licenses/MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Micro\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use \Micro\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Environment implements ConfigInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * DELIMITER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private const DELIMITER = '_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Load config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param  string $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param  string $env | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function __construct(string $config = null, string $env = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->store = $this->variablesToTree(array_merge($_ENV, $_SERVER)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Get from config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param   string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return  mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function __get(string $name) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         return $this->store->{$name}; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Get native config format as config instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param   array $environmentVariables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return  Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function map($environmentVariables = null): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($environmentVariables === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $this->store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $this->variablesToTree($environmentVariables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Transform array of environment variables into a tree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param   array $environmentVars | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return  Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     protected function variablesToTree(array $environmentVars): Config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // transform keys to lowercase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $environmentVars = array_change_key_case($environmentVars); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // initialize root node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $root = new Config(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         foreach ($environmentVars as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             // split variable name by delimiter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             $name = explode(self::DELIMITER, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             // go back to root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $tree = $root; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             // iterate over key parts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             for ($i = 0; $i < count($name); $i++) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $key = $name[$i]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 // try to access subtree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                   // create new subtree if requested subtree already has a value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                   if (!is_a($tree->$key, '\Micro\Config')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                       $tree[$key] = new Config([$tree->$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                   $tree = $tree->$key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     // set value if last keypart or create subtree | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     if($i == (count($name) - 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                         $tree[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                         $tree[$key] = new Config(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                         $tree = $tree->$key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return $root; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.