@@ -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 ?? 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 |
@@ -134,10 +134,10 @@ discard block |
||
134 | 134 | * |
135 | 135 | * @return $this |
136 | 136 | */ |
137 | - protected function __unsetEnv(string $var){ |
|
137 | + protected function __unsetEnv(string $var) { |
|
138 | 138 | $var = strtoupper($var); |
139 | 139 | |
140 | - if($this->_global === true){ |
|
140 | + if ($this->_global === true) { |
|
141 | 141 | unset($_ENV[$var]); |
142 | 142 | putenv($var); |
143 | 143 | } |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @return $this |
154 | 154 | */ |
155 | - protected function __clearEnv(){ |
|
155 | + protected function __clearEnv() { |
|
156 | 156 | |
157 | - if($this->_global === true){ |
|
157 | + if ($this->_global === true) { |
|
158 | 158 | $_ENV = []; |
159 | 159 | } |
160 | 160 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | */ |
172 | 172 | private function __read(string $file):array{ |
173 | 173 | |
174 | - if(!is_readable($file) || !is_file($file)){ |
|
174 | + if (!is_readable($file) || !is_file($file)) { |
|
175 | 175 | throw new TraitException('invalid file: '.$file); |
176 | 176 | } |
177 | 177 | |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
182 | 182 | ini_set('auto_detect_line_endings', $autodetect); |
183 | 183 | |
184 | - if(!is_array($lines) || empty($lines)){ |
|
184 | + if (!is_array($lines) || empty($lines)) { |
|
185 | 185 | throw new TraitException('error while reading file: '.$file); |
186 | 186 | } |
187 | 187 | |
@@ -194,19 +194,19 @@ discard block |
||
194 | 194 | * |
195 | 195 | * @return $this |
196 | 196 | */ |
197 | - private function __load(array $data, bool $overwrite){ |
|
197 | + private function __load(array $data, bool $overwrite) { |
|
198 | 198 | |
199 | - foreach($data as $line){ |
|
199 | + foreach ($data as $line) { |
|
200 | 200 | |
201 | 201 | // skip empty lines and comments |
202 | - if(empty($line) || strpos($line, '#') === 0){ |
|
202 | + if (empty($line) || strpos($line, '#') === 0) { |
|
203 | 203 | continue; |
204 | 204 | } |
205 | 205 | |
206 | 206 | $kv = array_map('trim', explode('=', $line, 2)); |
207 | 207 | |
208 | 208 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
209 | - if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)){ |
|
209 | + if (empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)) { |
|
210 | 210 | continue; |
211 | 211 | } |
212 | 212 | |
@@ -221,9 +221,9 @@ discard block |
||
221 | 221 | * |
222 | 222 | * @return string|null |
223 | 223 | */ |
224 | - private function __parse(string $value = null){ |
|
224 | + private function __parse(string $value = null) { |
|
225 | 225 | |
226 | - if($value !== null){ |
|
226 | + if ($value !== null) { |
|
227 | 227 | |
228 | 228 | $q = $value[0] ?? null; |
229 | 229 | |
@@ -237,8 +237,8 @@ discard block |
||
237 | 237 | $value = implode(PHP_EOL, explode('\\n', $value)); |
238 | 238 | |
239 | 239 | // handle nested ${VARS} |
240 | - if(strpos($value, '$') !== false){ |
|
241 | - $value = preg_replace_callback('/\${(?<var>[_a-z\d]+)}/i', function($matches){ |
|
240 | + if (strpos($value, '$') !== false) { |
|
241 | + $value = preg_replace_callback('/\${(?<var>[_a-z\d]+)}/i', function($matches) { |
|
242 | 242 | return $this->__getEnv($matches['var']); |
243 | 243 | }, $value); |
244 | 244 | } |
@@ -254,14 +254,14 @@ discard block |
||
254 | 254 | * @return $this |
255 | 255 | * @throws \chillerlan\Traits\TraitException |
256 | 256 | */ |
257 | - private function __check(array $required = null){ |
|
257 | + private function __check(array $required = null) { |
|
258 | 258 | |
259 | - if($required === null || empty($required)){ |
|
259 | + if ($required === null || empty($required)) { |
|
260 | 260 | return $this; |
261 | 261 | } |
262 | 262 | |
263 | - foreach($required as $var){ |
|
264 | - if(!$this->__issetEnv($var)){ |
|
263 | + foreach ($required as $var) { |
|
264 | + if (!$this->__issetEnv($var)) { |
|
265 | 265 | throw new TraitException('required variable not set: '.strtoupper($var)); |
266 | 266 | } |
267 | 267 | } |