From dbafef1925937952185ebd2900d70b8e067a366a Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Wed, 25 Oct 2017 15:44:28 +0200
Subject: [PATCH] Allow default value and implement a put function which stores
 into .env

---
 Dotenv.php | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/Dotenv.php b/Dotenv.php
index 91bda79..79b2b38 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();
-  }
-
 }
-- 
GitLab