4848 */
4949class PhpDotEnv
5050{
51- public const FULL_KEY = 'PHP_DOTENV_VARS ' ;
51+ public const FULL_ENV_KEY = 'PHP_DOTENV_VARS ' ;
52+ public const DEFAULT_NAME = '.env ' ;
53+
54+ /**
55+ * @var self
56+ */
57+ private static $ global ;
58+
59+ /**
60+ * @var array
61+ */
62+ private $ loadedFiles = [];
63+
64+ /**
65+ * @return static
66+ */
67+ public static function global (): self
68+ {
69+ if (!self ::$ global ) {
70+ self ::$ global = new self ('' );
71+ }
72+
73+ return self ::$ global ;
74+ }
5275
5376 /**
5477 * @param string $fileDir
5578 * @param string $fileName
5679 *
5780 * @return static
5881 */
59- public static function load (string $ fileDir , string $ fileName = ' .env ' ): self
82+ public static function load (string $ fileDir , string $ fileName = self :: DEFAULT_NAME ): self
6083 {
6184 return new self ($ fileDir , $ fileName );
6285 }
6386
6487 /**
65- * constructor.
88+ * class constructor.
6689 *
6790 * @param string $fileDir
6891 * @param string $fileName
6992 */
70- public function __construct (string $ fileDir , string $ fileName = ' .env ' )
93+ public function __construct (string $ fileDir , string $ fileName = self :: DEFAULT_NAME )
7194 {
72- $ file = $ fileDir . DIRECTORY_SEPARATOR . ($ fileName ?: '.env ' );
73-
74- $ this ->add ($ file );
95+ if ($ fileDir ) {
96+ $ file = $ fileDir . DIRECTORY_SEPARATOR . ($ fileName ?: self ::DEFAULT_NAME );
97+ $ this ->add ($ file );
98+ }
7599 }
76100
77101 /**
@@ -80,6 +104,7 @@ public function __construct(string $fileDir, string $fileName = '.env')
80104 public function add (string $ file ): void
81105 {
82106 if (is_file ($ file ) && is_readable ($ file )) {
107+ $ this ->loadedFiles [] = $ file ;
83108 $ this ->settingEnv (parse_ini_file ($ file ));
84109 }
85110 }
@@ -91,18 +116,23 @@ public function add(string $file): void
91116 */
92117 private function settingEnv (array $ data ): void
93118 {
94- $ loadedVars = array_flip (explode (', ' , getenv (self ::FULL_KEY )));
119+ $ loadedVars = array_flip (explode (', ' , ( string ) getenv (self ::FULL_ENV_KEY )));
95120 unset($ loadedVars ['' ]);
96121
97122 foreach ($ data as $ name => $ value ) {
98123 if (is_int ($ name ) || !is_string ($ value )) {
99124 continue ;
100125 }
101126
102- $ name = strtoupper ($ name );
103- $ notHttpName = 0 !== strpos ($ name , 'HTTP_ ' );
127+ // fix: comments start with #
128+ if ($ name [0 ] === '# ' ) {
129+ continue ;
130+ }
131+
132+ $ name = strtoupper ($ name );
104133
105134 // don't check existence with getenv() because of thread safety issues
135+ $ notHttpName = 0 !== strpos ($ name , 'HTTP_ ' );
106136 if ((isset ($ _ENV [$ name ]) || (isset ($ _SERVER [$ name ]) && $ notHttpName )) && !isset ($ loadedVars [$ name ])) {
107137 continue ;
108138 }
@@ -125,9 +155,17 @@ private function settingEnv(array $data): void
125155
126156 if ($ loadedVars ) {
127157 $ loadedVars = implode (', ' , array_keys ($ loadedVars ));
128- putenv (self ::FULL_KEY . "= $ loadedVars " );
129- $ _ENV [self ::FULL_KEY ] = $ loadedVars ;
130- $ _SERVER [self ::FULL_KEY ] = $ loadedVars ;
158+ putenv (self ::FULL_ENV_KEY . "= $ loadedVars " );
159+ $ _ENV [self ::FULL_ENV_KEY ] = $ loadedVars ;
160+ $ _SERVER [self ::FULL_ENV_KEY ] = $ loadedVars ;
131161 }
132162 }
163+
164+ /**
165+ * @return array
166+ */
167+ public function getLoadedFiles (): array
168+ {
169+ return $ this ->loadedFiles ;
170+ }
133171}
0 commit comments