48
48
*/
49
49
class PhpDotEnv
50
50
{
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
+ }
52
75
53
76
/**
54
77
* @param string $fileDir
55
78
* @param string $fileName
56
79
*
57
80
* @return static
58
81
*/
59
- public static function load (string $ fileDir , string $ fileName = ' .env ' ): self
82
+ public static function load (string $ fileDir , string $ fileName = self :: DEFAULT_NAME ): self
60
83
{
61
84
return new self ($ fileDir , $ fileName );
62
85
}
63
86
64
87
/**
65
- * constructor.
88
+ * class constructor.
66
89
*
67
90
* @param string $fileDir
68
91
* @param string $fileName
69
92
*/
70
- public function __construct (string $ fileDir , string $ fileName = ' .env ' )
93
+ public function __construct (string $ fileDir , string $ fileName = self :: DEFAULT_NAME )
71
94
{
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
+ }
75
99
}
76
100
77
101
/**
@@ -80,6 +104,7 @@ public function __construct(string $fileDir, string $fileName = '.env')
80
104
public function add (string $ file ): void
81
105
{
82
106
if (is_file ($ file ) && is_readable ($ file )) {
107
+ $ this ->loadedFiles [] = $ file ;
83
108
$ this ->settingEnv (parse_ini_file ($ file ));
84
109
}
85
110
}
@@ -91,18 +116,23 @@ public function add(string $file): void
91
116
*/
92
117
private function settingEnv (array $ data ): void
93
118
{
94
- $ loadedVars = array_flip (explode (', ' , getenv (self ::FULL_KEY )));
119
+ $ loadedVars = array_flip (explode (', ' , ( string ) getenv (self ::FULL_ENV_KEY )));
95
120
unset($ loadedVars ['' ]);
96
121
97
122
foreach ($ data as $ name => $ value ) {
98
123
if (is_int ($ name ) || !is_string ($ value )) {
99
124
continue ;
100
125
}
101
126
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 );
104
133
105
134
// don't check existence with getenv() because of thread safety issues
135
+ $ notHttpName = 0 !== strpos ($ name , 'HTTP_ ' );
106
136
if ((isset ($ _ENV [$ name ]) || (isset ($ _SERVER [$ name ]) && $ notHttpName )) && !isset ($ loadedVars [$ name ])) {
107
137
continue ;
108
138
}
@@ -125,9 +155,17 @@ private function settingEnv(array $data): void
125
155
126
156
if ($ loadedVars ) {
127
157
$ 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 ;
131
161
}
132
162
}
163
+
164
+ /**
165
+ * @return array
166
+ */
167
+ public function getLoadedFiles (): array
168
+ {
169
+ return $ this ->loadedFiles ;
170
+ }
133
171
}
0 commit comments