@@ -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 |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @return $this |
| 40 | 40 | */ |
| 41 | - protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null){ |
|
| 41 | + protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null) { |
|
| 42 | 42 | $overwrite = $overwrite !== null ? $overwrite : false; |
| 43 | 43 | $content = $this->__read(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.($filename ?? '.env')); |
| 44 | 44 | |
@@ -53,20 +53,20 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return bool|mixed |
| 55 | 55 | */ |
| 56 | - protected function __getEnv(string $var){ |
|
| 56 | + protected function __getEnv(string $var) { |
|
| 57 | 57 | $var = strtoupper($var); |
| 58 | 58 | |
| 59 | - if(array_key_exists($var, $_ENV)){ |
|
| 59 | + if (array_key_exists($var, $_ENV)) { |
|
| 60 | 60 | return $_ENV[$var]; |
| 61 | 61 | } |
| 62 | - elseif(function_exists('getenv')){ |
|
| 63 | - if($e = getenv($var) !== false){ |
|
| 62 | + elseif (function_exists('getenv')) { |
|
| 63 | + if ($e = getenv($var) !== false) { |
|
| 64 | 64 | return $e; |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | // @codeCoverageIgnoreStart |
| 68 | - elseif(function_exists('apache_getenv')){ |
|
| 69 | - if($e = apache_getenv($var) !== false){ |
|
| 68 | + elseif (function_exists('apache_getenv')) { |
|
| 69 | + if ($e = apache_getenv($var) !== false) { |
|
| 70 | 70 | return $e; |
| 71 | 71 | } |
| 72 | 72 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * |
| 82 | 82 | * @return $this |
| 83 | 83 | */ |
| 84 | - protected function __setEnv(string $var, string $value = null){ |
|
| 84 | + protected function __setEnv(string $var, string $value = null) { |
|
| 85 | 85 | $var = strtoupper($var); |
| 86 | 86 | $value = $this->__parse($value); |
| 87 | 87 | |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $this->_ENV[$var] = $value; |
| 94 | 94 | |
| 95 | 95 | // @codeCoverageIgnoreStart |
| 96 | - if(function_exists('apache_setenv')){ |
|
| 96 | + if (function_exists('apache_setenv')) { |
|
| 97 | 97 | apache_setenv($var, $value); |
| 98 | 98 | } |
| 99 | 99 | // @codeCoverageIgnoreEnd |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * |
| 107 | 107 | * @return $this |
| 108 | 108 | */ |
| 109 | - protected function __unsetEnv(string $var){ |
|
| 109 | + protected function __unsetEnv(string $var) { |
|
| 110 | 110 | $var = strtoupper($var); |
| 111 | 111 | |
| 112 | 112 | unset($_ENV[$var]); |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * |
| 122 | 122 | * @return $this |
| 123 | 123 | */ |
| 124 | - protected function __clearEnv(){ |
|
| 124 | + protected function __clearEnv() { |
|
| 125 | 125 | $_ENV = []; |
| 126 | 126 | $this->_ENV = []; |
| 127 | 127 | |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | */ |
| 137 | 137 | private function __read(string $file):array{ |
| 138 | 138 | |
| 139 | - if(!is_readable($file) || !is_file($file)){ |
|
| 139 | + if (!is_readable($file) || !is_file($file)) { |
|
| 140 | 140 | throw new TraitException('invalid file: '.$file); |
| 141 | 141 | } |
| 142 | 142 | |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
| 147 | 147 | ini_set('auto_detect_line_endings', $autodetect); |
| 148 | 148 | |
| 149 | - if(!is_array($lines) || empty($lines)){ |
|
| 149 | + if (!is_array($lines) || empty($lines)) { |
|
| 150 | 150 | throw new TraitException('error while reading file: '.$file); |
| 151 | 151 | } |
| 152 | 152 | |
@@ -159,19 +159,19 @@ discard block |
||
| 159 | 159 | * |
| 160 | 160 | * @return $this |
| 161 | 161 | */ |
| 162 | - private function __load(array $data, bool $overwrite){ |
|
| 162 | + private function __load(array $data, bool $overwrite) { |
|
| 163 | 163 | |
| 164 | - foreach($data as $line){ |
|
| 164 | + foreach ($data as $line) { |
|
| 165 | 165 | |
| 166 | 166 | // skip empty lines and comments |
| 167 | - if(empty($line) || strpos($line, '#') === 0){ |
|
| 167 | + if (empty($line) || strpos($line, '#') === 0) { |
|
| 168 | 168 | continue; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | $kv = array_map('trim', explode('=', $line, 2)); |
| 172 | 172 | |
| 173 | 173 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
| 174 | - if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)){ |
|
| 174 | + if (empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)) { |
|
| 175 | 175 | continue; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | * |
| 187 | 187 | * @return string|null |
| 188 | 188 | */ |
| 189 | - private function __parse(string $value = null){ |
|
| 189 | + private function __parse(string $value = null) { |
|
| 190 | 190 | |
| 191 | - if($value !== null){ |
|
| 191 | + if ($value !== null) { |
|
| 192 | 192 | |
| 193 | 193 | $q = $value[0] ?? null; |
| 194 | 194 | |
@@ -202,8 +202,8 @@ discard block |
||
| 202 | 202 | $value = implode(PHP_EOL, explode('\\n', $value)); |
| 203 | 203 | |
| 204 | 204 | // handle nested ${VARS} |
| 205 | - if(strpos($value, '$') !== false){ |
|
| 206 | - $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches){ |
|
| 205 | + if (strpos($value, '$') !== false) { |
|
| 206 | + $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches) { |
|
| 207 | 207 | return $this->__getEnv($matches[1]); |
| 208 | 208 | }, $value); |
| 209 | 209 | } |
@@ -219,14 +219,14 @@ discard block |
||
| 219 | 219 | * @return $this |
| 220 | 220 | * @throws \chillerlan\Traits\TraitException |
| 221 | 221 | */ |
| 222 | - private function __check(array $required = null){ |
|
| 222 | + private function __check(array $required = null) { |
|
| 223 | 223 | |
| 224 | - if($required === null || empty($required)){ |
|
| 224 | + if ($required === null || empty($required)) { |
|
| 225 | 225 | return $this; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - foreach($required as $var){ |
|
| 229 | - if($this->__getEnv($var) === false || $this->__getEnv($var) === null){ |
|
| 228 | + foreach ($required as $var) { |
|
| 229 | + if ($this->__getEnv($var) === false || $this->__getEnv($var) === null) { |
|
| 230 | 230 | throw new TraitException('required variable not set: '.strtoupper($var)); |
| 231 | 231 | } |
| 232 | 232 | } |