From 996d20157547b513ee0f35964d19d66ffa7a96a9 Mon Sep 17 00:00:00 2001 From: Sieciech Date: Sat, 7 Aug 2021 16:54:24 +0200 Subject: [PATCH] fix --- tools/putenv/putenv.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tools/putenv/putenv.php b/tools/putenv/putenv.php index c269a32..333cf29 100644 --- a/tools/putenv/putenv.php +++ b/tools/putenv/putenv.php @@ -33,8 +33,24 @@ class PutEnv { if (!file_exists($this->file)) { throw new Exception('File not found'); } + $changed = 0; + $lineWasEmpty = false; + $key = $this->key; + $value = $this->value; + if (strpos($value, ' ') !== false) { + $value = '"' . addslashes($value) . '"'; + } $this->content = file($this->file); foreach ($this->content as $l => $line) { + if (strlen($line) < 1) { + if ($lineWasEmpty) { + unset ($this->content[$l]); + } else { + $lineWasEmpty = true; + } + continue; + } + $lineWasEmpty = false; if (!str_starts_with($line, $this->key)) { continue; } @@ -42,12 +58,13 @@ class PutEnv { if (strlen($next) === 0 || $next[0] !== '=') { continue; } - $key = $this->key; - $value = $this->value; - if (strpos($value, ' ') !== false) { - $value = '"' . addslashes($value) . '"'; - } $this->content[$l] = $key . '=' . $value; + $changed++; + } + if ($changed === 0) { + $this->content[] = $key . '=' . $value; + } else if ($changed > 1) { + echo "Key $key changed $changed times\n"; } $content = implode("\n", $this->content); file_put_contents($this->file, $content);