diff --git a/Dotenv.php b/Dotenv.php index 91bda7943d03027cb45d86821c727b3d4f9765c1..79b2b3849c2c971544429ee0e529d41ca6792e77 100644 --- a/Dotenv.php +++ b/Dotenv.php @@ -37,18 +37,37 @@ final class Dotenv { * * @return string */ - public function receive($key, $prompt) { + public function receive($key, $prompt, $default = '') { $key = $this->name . '_' . strtoupper($key); $value = getenv($key); if (empty($value)) { if ($this->io->isInteractive()) { $value = $this->io->ask($prompt . ': '); - $this->putenv($key, $value); } + if (empty($value)) { + $value = $default; + } + $this->put($key, $value); } return $value; } + /** + * @param string $key + * @param string $value + */ + public function put($key, $value) { + $key = strtoupper($key); + if (!empty(getenv($key))) { + return; + } + $env = file_exists('.env') ? file_get_contents('.env') : ''; + $env .= PHP_EOL . $key . '=' . $value . PHP_EOL; + file_put_contents('.env', $env); + $this->load(); + passthru(escapeshellcmd('git -c "user.email=dotenv@lakedrops.com" ignore .env')); + } + private function load() { try { $this->dotenv->load('.env'); @@ -58,11 +77,4 @@ final class Dotenv { } } - private function putenv($key, $value) { - $env = file_exists('.env') ? file_get_contents('.env') : ''; - $env .= PHP_EOL . $key . '=' . $value . PHP_EOL; - file_put_contents('.env', $env); - $this->load(); - } - }