@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | |
13 | 13 | namespace chillerlan\Traits; |
14 | 14 | |
15 | -class DotEnv{ |
|
15 | +class DotEnv { |
|
16 | 16 | use Env; |
17 | 17 | |
18 | 18 | /** |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | * @param string $path |
32 | 32 | * @param string|null $filename |
33 | 33 | */ |
34 | - public function __construct(string $path, string $filename = null){ |
|
34 | + public function __construct(string $path, string $filename = null) { |
|
35 | 35 | $this->path = $path; |
36 | 36 | $this->filename = $filename; |
37 | 37 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @return bool|mixed |
64 | 64 | */ |
65 | - public function get(string $var){ |
|
65 | + public function get(string $var) { |
|
66 | 66 | return $this->__getEnv($var); |
67 | 67 | } |
68 | 68 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @return $this |
74 | 74 | */ |
75 | - public function set(string $var, string $value){ |
|
75 | + public function set(string $var, string $value) { |
|
76 | 76 | return $this->__setEnv($var, $value); |
77 | 77 | } |
78 | 78 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | * |
20 | 20 | * @link https://github.com/vlucas/phpdotenv |
21 | 21 | */ |
22 | -trait Env{ |
|
22 | +trait Env { |
|
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @param string $path |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | * |
30 | 30 | * @return $this |
31 | 31 | */ |
32 | - protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null){ |
|
32 | + protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null) { |
|
33 | 33 | $overwrite = $overwrite !== null ? $overwrite : false; |
34 | 34 | $content = $this->__read(rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.($filename ?? '.env')); |
35 | 35 | |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | * |
45 | 45 | * @return bool|mixed |
46 | 46 | */ |
47 | - protected function __getEnv(string $var){ |
|
48 | - $var = strtoupper($var); |
|
47 | + protected function __getEnv(string $var) { |
|
48 | + $var = strtoupper($var); |
|
49 | 49 | |
50 | 50 | return array_key_exists($var, $_ENV) ? $_ENV[$var] : false; |
51 | 51 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | * |
57 | 57 | * @return $this |
58 | 58 | */ |
59 | - protected function __setEnv(string $var, string $value = null){ |
|
59 | + protected function __setEnv(string $var, string $value = null) { |
|
60 | 60 | $var = strtoupper($var); |
61 | 61 | $value = $this->__parse($value); |
62 | 62 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @return $this |
74 | 74 | */ |
75 | - protected function __unsetEnv(string $var){ |
|
75 | + protected function __unsetEnv(string $var) { |
|
76 | 76 | $var = strtoupper($var); |
77 | 77 | |
78 | 78 | unset($_ENV[$var]); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @return $this |
88 | 88 | */ |
89 | - protected function __clearEnv(){ |
|
89 | + protected function __clearEnv() { |
|
90 | 90 | $_ENV = []; |
91 | 91 | |
92 | 92 | return $this; |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | */ |
101 | 101 | private function __read(string $file):array{ |
102 | 102 | |
103 | - if(!is_readable($file) || !is_file($file)){ |
|
103 | + if (!is_readable($file) || !is_file($file)) { |
|
104 | 104 | throw new TraitException('invalid file: '.$file); |
105 | 105 | } |
106 | 106 | |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
111 | 111 | ini_set('auto_detect_line_endings', $autodetect); |
112 | 112 | |
113 | - if(!is_array($lines) || empty($lines)){ |
|
113 | + if (!is_array($lines) || empty($lines)) { |
|
114 | 114 | throw new TraitException('error while reading file: '.$file); |
115 | 115 | } |
116 | 116 | |
@@ -125,19 +125,19 @@ discard block |
||
125 | 125 | * |
126 | 126 | * @return $this |
127 | 127 | */ |
128 | - private function __load(array $data, bool $overwrite){ |
|
128 | + private function __load(array $data, bool $overwrite) { |
|
129 | 129 | |
130 | - foreach($data as $line){ |
|
130 | + foreach ($data as $line) { |
|
131 | 131 | |
132 | 132 | // skip empty lines and comments |
133 | - if(empty($line) || strpos($line, '#') === 0){ |
|
133 | + if (empty($line) || strpos($line, '#') === 0) { |
|
134 | 134 | continue; |
135 | 135 | } |
136 | 136 | |
137 | 137 | $kv = array_map('trim', explode('=', $line, 2)); |
138 | 138 | |
139 | 139 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
140 | - if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && array_key_exists($kv[0], $_ENV))){ |
|
140 | + if (empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && array_key_exists($kv[0], $_ENV))) { |
|
141 | 141 | continue; |
142 | 142 | } |
143 | 143 | |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @return string|null |
154 | 154 | */ |
155 | - private function __parse(string $value = null){ |
|
155 | + private function __parse(string $value = null) { |
|
156 | 156 | |
157 | - if($value !== null){ |
|
157 | + if ($value !== null) { |
|
158 | 158 | |
159 | 159 | $q = $value[0] ?? null; |
160 | 160 | |
@@ -168,8 +168,8 @@ discard block |
||
168 | 168 | $value = implode(PHP_EOL, explode('\\n', $value)); |
169 | 169 | |
170 | 170 | // handle nested ${VARS} |
171 | - if(strpos($value, '$') !== false){ |
|
172 | - $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches){ |
|
171 | + if (strpos($value, '$') !== false) { |
|
172 | + $value = preg_replace_callback('/\${([_a-z\d]+)}/i', function($matches) { |
|
173 | 173 | return $this->__getEnv($matches[1]); |
174 | 174 | }, $value); |
175 | 175 | } |
@@ -185,14 +185,14 @@ discard block |
||
185 | 185 | * @return $this |
186 | 186 | * @throws \chillerlan\Traits\TraitException |
187 | 187 | */ |
188 | - private function __check(array $required = null){ |
|
188 | + private function __check(array $required = null) { |
|
189 | 189 | |
190 | - if($required === null || empty($required)){ |
|
190 | + if ($required === null || empty($required)) { |
|
191 | 191 | return $this; |
192 | 192 | } |
193 | 193 | |
194 | - foreach($required as $var){ |
|
195 | - if(!$this->__getEnv($var) || $this->__getEnv($var) === null){ |
|
194 | + foreach ($required as $var) { |
|
195 | + if (!$this->__getEnv($var) || $this->__getEnv($var) === null) { |
|
196 | 196 | throw new TraitException('required variable not set: '.strtoupper($var)); |
197 | 197 | } |
198 | 198 | } |