| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EdmondsCommerce\DoctrineStaticMeta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class SimpleEnv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * A simplistic take on reading .env files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * We only require parsing very simple key=value pairs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * For a more fully featured library, see https://github.com/vlucas/phpdotenv | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package EdmondsCommerce\DoctrineStaticMeta | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @SuppressWarnings(PHPMD.Superglobals) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class SimpleEnv | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public static function setEnv(string $filePath, array &$server = null): void | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         if (null === $server) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             $server =& $_SERVER; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         if (!file_exists($filePath)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             throw new ConfigException('Env file path '.$filePath.' does not exist'); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $lines = file($filePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         foreach ($lines as $line) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             self::processLine($line, $server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private static function processLine(string $line, array &$server) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         #skip comments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (preg_match('%^\s*#%', $line)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         preg_match( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             #strip leading spaces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             '%^[[:space:]]*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             #strip leading `export` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             .'(?:export[[:space:]]+|)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             #parse out the key and assign to named match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             .'(?<key>[^=]+?)' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             #strip out `=`, possibly with space around it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             .'[[:space:]]*=[[:space:]]*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             #strip out possible quotes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ."(?:\"|'|)" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             #parse out the value and assign to named match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ."(?<value>[^\"']+?)" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             #strip out possible quotes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             ."(?:\"|'|)" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             #string out trailing space to end of line | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             .'[[:space:]]*$%', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $line, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $matches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if (5 !== count($matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         list(, $key, $value) = $matches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (empty($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $value = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (!isset($server[$key])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $server[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |