From 22b66984ca17d73305a0c28905d2e5187aa632a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20B=C3=B6swetter?= Date: Fri, 3 Nov 2023 16:27:01 +0100 Subject: [PATCH] [BUGFIX] updates Companion->writeVariable() to support (existing) comments and empty lines in .env files --- src/Companienv/Companion.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Companienv/Companion.php b/src/Companienv/Companion.php index e283f0a..8f1a5a9 100644 --- a/src/Companienv/Companion.php +++ b/src/Companienv/Companion.php @@ -95,12 +95,22 @@ private function writeVariable(string $name, string $value) $reader = (new DotenvReader(new ParserV3()))->load($this->envFileName); foreach ($reader->entries(true) as $entry) { if (isset($entry['parsed_data'])) { - $writer->appendSetter( - $entry['parsed_data']['key'], - trim($entry['parsed_data']['value'], '"'), - $entry['parsed_data']['comment'], - $entry['parsed_data']['export'] - ); + switch ($entry['parsed_data']['type']) { + case 'comment': + $writer->appendComment($entry['parsed_data']['comment']); + break; + case 'empty': + $writer->appendEmpty(); + break; + case 'setter': + $writer->appendSetter( + $entry['parsed_data']['key'], + trim($entry['parsed_data']['value'], '"'), + $entry['parsed_data']['comment'], + $entry['parsed_data']['export'] + ); + break; + } } }