@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * @link http://php.net/variables-order |
22 | 22 | * |
23 | 23 | */ |
24 | -trait Env{ |
|
24 | +trait Env { |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * a backup environment in case everything goes downhill |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @return $this |
49 | 49 | */ |
50 | - protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null, bool $global = null){ |
|
50 | + protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null, bool $global = null) { |
|
51 | 51 | $this->_global = $global !== null ? $global : false; |
52 | 52 | $content = $this->__read(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.($filename ?? '.env')); |
53 | 53 | |
@@ -62,20 +62,20 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return bool|mixed |
64 | 64 | */ |
65 | - protected function __getEnv(string $var){ |
|
65 | + protected function __getEnv(string $var) { |
|
66 | 66 | $var = strtoupper($var); |
67 | 67 | $env = null; |
68 | 68 | |
69 | - if($this->_global === true){ |
|
69 | + if ($this->_global === true) { |
|
70 | 70 | |
71 | - if(array_key_exists($var, $_ENV)){ |
|
71 | + if (array_key_exists($var, $_ENV)) { |
|
72 | 72 | $env = $_ENV[$var]; |
73 | 73 | } |
74 | - elseif(function_exists('getenv')){ |
|
74 | + elseif (function_exists('getenv')) { |
|
75 | 75 | $env = getenv($var); |
76 | 76 | } |
77 | 77 | // @codeCoverageIgnoreStart |
78 | - elseif(function_exists('apache_getenv')){ |
|
78 | + elseif (function_exists('apache_getenv')) { |
|
79 | 79 | $env = apache_getenv($var); |
80 | 80 | } |
81 | 81 | // @codeCoverageIgnoreEnd |
@@ -91,18 +91,18 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return $this |
93 | 93 | */ |
94 | - protected function __setEnv(string $var, string $value = null){ |
|
94 | + protected function __setEnv(string $var, string $value = null) { |
|
95 | 95 | $var = strtoupper($var); |
96 | 96 | $value = $this->__parse($value); |
97 | 97 | |
98 | - if($this->_global === true){ |
|
98 | + if ($this->_global === true) { |
|
99 | 99 | putenv($var.'='.$value); |
100 | 100 | |
101 | 101 | // fill $_ENV explicitly, assuming variables_order="GPCS" (production) |
102 | 102 | $_ENV[$var] = $value; |
103 | 103 | |
104 | 104 | // @codeCoverageIgnoreStart |
105 | - if(function_exists('apache_setenv')){ |
|
105 | + if (function_exists('apache_setenv')) { |
|
106 | 106 | apache_setenv($var, $value); |
107 | 107 | } |
108 | 108 | // @codeCoverageIgnoreEnd |
@@ -119,10 +119,10 @@ discard block |
||
119 | 119 | * |
120 | 120 | * @return $this |
121 | 121 | */ |
122 | - protected function __unsetEnv(string $var){ |
|
122 | + protected function __unsetEnv(string $var) { |
|
123 | 123 | $var = strtoupper($var); |
124 | 124 | |
125 | - if($this->_global === true){ |
|
125 | + if ($this->_global === true) { |
|
126 | 126 | unset($_ENV[$var]); |
127 | 127 | putenv($var); |
128 | 128 | } |
@@ -137,9 +137,9 @@ discard block |
||
137 | 137 | * |
138 | 138 | * @return $this |
139 | 139 | */ |
140 | - protected function __clearEnv(){ |
|
140 | + protected function __clearEnv() { |
|
141 | 141 | |
142 | - if($this->_global === true){ |
|
142 | + if ($this->_global === true) { |
|
143 | 143 | $_ENV = []; |
144 | 144 | } |
145 | 145 | |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | */ |
157 | 157 | private function __read(string $file):array{ |
158 | 158 | |
159 | - if(!is_readable($file) || !is_file($file)){ |
|
159 | + if (!is_readable($file) || !is_file($file)) { |
|
160 | 160 | throw new TraitException('invalid file: '.$file); |
161 | 161 | } |
162 | 162 | |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
167 | 167 | ini_set('auto_detect_line_endings', $autodetect); |
168 | 168 | |
169 | - if(!is_array($lines) || empty($lines)){ |
|
169 | + if (!is_array($lines) || empty($lines)) { |
|
170 | 170 | throw new TraitException('error while reading file: '.$file); |
171 | 171 | } |
172 | 172 | |
@@ -179,19 +179,19 @@ discard block |
||
179 | 179 | * |
180 | 180 | * @return $this |
181 | 181 | */ |
182 | - private function __load(array $data, bool $overwrite){ |
|
182 | + private function __load(array $data, bool $overwrite) { |
|
183 | 183 | |
184 | - foreach($data as $line){ |
|
184 | + foreach ($data as $line) { |
|
185 | 185 | |
186 | 186 | // skip empty lines and comments |
187 | - if(empty($line) || strpos($line, '#') === 0){ |
|
187 | + if (empty($line) || strpos($line, '#') === 0) { |
|
188 | 188 | continue; |
189 | 189 | } |
190 | 190 | |
191 | 191 | $kv = array_map('trim', explode('=', $line, 2)); |
192 | 192 | |
193 | 193 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
194 | - if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)){ |
|
194 | + if (empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)) { |
|
195 | 195 | continue; |
196 | 196 | } |
197 | 197 | |
@@ -206,9 +206,9 @@ discard block |
||
206 | 206 | * |
207 | 207 | * @return string|null |
208 | 208 | */ |
209 | - private function __parse(string $value = null){ |
|
209 | + private function __parse(string $value = null) { |
|
210 | 210 | |
211 | - if($value !== null){ |
|
211 | + if ($value !== null) { |
|
212 | 212 | |
213 | 213 | $q = $value[0] ?? null; |
214 | 214 | |
@@ -222,8 +222,8 @@ discard block |
||
222 | 222 | $value = implode(PHP_EOL, explode('\\n', $value)); |
223 | 223 | |
224 | 224 | // handle nested ${VARS} |
225 | - if(strpos($value, '$') !== false){ |
|
226 | - $value = preg_replace_callback('/\${(?<var>[_a-z\d]+)}/i', function($matches){ |
|
225 | + if (strpos($value, '$') !== false) { |
|
226 | + $value = preg_replace_callback('/\${(?<var>[_a-z\d]+)}/i', function($matches) { |
|
227 | 227 | return $this->__getEnv($matches['var']); |
228 | 228 | }, $value); |
229 | 229 | } |
@@ -239,14 +239,14 @@ discard block |
||
239 | 239 | * @return $this |
240 | 240 | * @throws \chillerlan\Traits\TraitException |
241 | 241 | */ |
242 | - private function __check(array $required = null){ |
|
242 | + private function __check(array $required = null) { |
|
243 | 243 | |
244 | - if($required === null || empty($required)){ |
|
244 | + if ($required === null || empty($required)) { |
|
245 | 245 | return $this; |
246 | 246 | } |
247 | 247 | |
248 | - foreach($required as $var){ |
|
249 | - if($this->__getEnv($var) === false || $this->__getEnv($var) === null){ |
|
248 | + foreach ($required as $var) { |
|
249 | + if ($this->__getEnv($var) === false || $this->__getEnv($var) === null) { |
|
250 | 250 | throw new TraitException('required variable not set: '.strtoupper($var)); |
251 | 251 | } |
252 | 252 | } |
@@ -70,8 +70,7 @@ |
||
70 | 70 | |
71 | 71 | if(array_key_exists($var, $_ENV)){ |
72 | 72 | $env = $_ENV[$var]; |
73 | - } |
|
74 | - elseif(function_exists('getenv')){ |
|
73 | + } elseif(function_exists('getenv')){ |
|
75 | 74 | $env = getenv($var); |
76 | 75 | } |
77 | 76 | // @codeCoverageIgnoreStart |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | * @method DotEnv unset(string $var) |
21 | 21 | * @method DotEnv clear() |
22 | 22 | */ |
23 | -class DotEnv{ |
|
23 | +class DotEnv { |
|
24 | 24 | use Env{ |
25 | 25 | // allow a magic getter & setter |
26 | 26 | __getEnv as public __get; |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @param string|null $filename |
50 | 50 | * @param bool|null $global |
51 | 51 | */ |
52 | - public function __construct(string $path, string $filename = null, bool $global = null){ |
|
52 | + public function __construct(string $path, string $filename = null, bool $global = null) { |
|
53 | 53 | $this->path = $path; |
54 | 54 | $this->filename = $filename; |
55 | 55 | $this->_global = $global ?? true; // emulate vlucas/dotenv behaviour by default |