Skip to content
Snippets Groups Projects
Commit ccd81f17 authored by jurgenhaas's avatar jurgenhaas
Browse files

Always store received env variables in .env file

Optimize code
parent 00715dec
No related branches found
No related tags found
No related merge requests found
...@@ -72,11 +72,12 @@ final class Dotenv { ...@@ -72,11 +72,12 @@ final class Dotenv {
if ($this->io->isInteractive()) { if ($this->io->isInteractive()) {
$value = $this->io->ask($prompt . ': '); $value = $this->io->ask($prompt . ': ');
} }
/** @noinspection NotOptimalIfConditionsInspection */
if (empty($value)) { if (empty($value)) {
$value = $default; $value = $default;
} }
$this->put($key, $value);
} }
$this->put($key, $value);
return $value; return $value;
} }
...@@ -91,6 +92,7 @@ final class Dotenv { ...@@ -91,6 +92,7 @@ final class Dotenv {
* The name of the environment variable. * The name of the environment variable.
* @param string $value * @param string $value
* The value of the environment variable. * The value of the environment variable.
* @param bool $overwrite
*/ */
public function put($key, $value, $overwrite = FALSE) { public function put($key, $value, $overwrite = FALSE) {
$envContent = file_exists('.env') ? file_get_contents('.env') : ''; $envContent = file_exists('.env') ? file_get_contents('.env') : '';
...@@ -101,8 +103,8 @@ final class Dotenv { ...@@ -101,8 +103,8 @@ final class Dotenv {
} }
$data[$key] = $value; $data[$key] = $value;
$envContent = ''; $envContent = '';
foreach ($data as $key => $value) { foreach ($data as $k => $v) {
$envContent .= $key . '=' . $value . PHP_EOL; $envContent .= $k . '=' . $v . PHP_EOL;
} }
file_put_contents('.env', $envContent); file_put_contents('.env', $envContent);
$this->load(); $this->load();
...@@ -132,6 +134,7 @@ final class Dotenv { ...@@ -132,6 +134,7 @@ final class Dotenv {
* @param string $key * @param string $key
*/ */
private function findEnvironmentVariables(&$item, &$key) { private function findEnvironmentVariables(&$item, &$key) {
/** @noinspection NotOptimalRegularExpressionsInspection */
$pattern = '@\{\$env:([A-Za-z0-9_]*)\}@i'; $pattern = '@\{\$env:([A-Za-z0-9_]*)\}@i';
preg_match_all($pattern, $item, $item_matches); preg_match_all($pattern, $item, $item_matches);
preg_match_all($pattern, $key, $key_matches); preg_match_all($pattern, $key, $key_matches);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment