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

Allow default value and implement a put function which stores into .env

parent 80cb2e63
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -37,18 +37,37 @@ final class Dotenv { ...@@ -37,18 +37,37 @@ final class Dotenv {
* *
* @return string * @return string
*/ */
public function receive($key, $prompt) { public function receive($key, $prompt, $default = '') {
$key = $this->name . '_' . strtoupper($key); $key = $this->name . '_' . strtoupper($key);
$value = getenv($key); $value = getenv($key);
if (empty($value)) { if (empty($value)) {
if ($this->io->isInteractive()) { if ($this->io->isInteractive()) {
$value = $this->io->ask($prompt . ': '); $value = $this->io->ask($prompt . ': ');
$this->putenv($key, $value);
} }
if (empty($value)) {
$value = $default;
}
$this->put($key, $value);
} }
return $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() { private function load() {
try { try {
$this->dotenv->load('.env'); $this->dotenv->load('.env');
...@@ -58,11 +77,4 @@ final class Dotenv { ...@@ -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();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment