DotEnvWriter icon indicating copy to clipboard operation
DotEnvWriter copied to clipboard

ErrorException on multiline value

Open maxmarkus opened this issue 2 years ago • 3 comments

When reading the file, I see an error on multiline values.

Example:

OAUTH_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
ABCDefBADAnbgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQZCdZMQwLW9ytam
ny5xRMdXMWEPZnQ+hHmi7E4/qiVkXv+BB3iXmY60OLg438Z10sBN7bFr71m4RwOw

Error:

   ErrorException

  Undefined array key 1

  at vendor/mirazmac/dotenvwriter/src/Writer.php:235
         $lines = preg_split('/\r\n|\r|\n/', $this->content);
  
         foreach ($lines as $line) {
           if (mb_strlen(trim($line)) && !(mb_strpos(trim($line), '#') === 0)) {
              [$key, $value] = explode('=', (string) $line);
              $this->variables[$key] = $this->formatValue($value);
           }
        }
    }

First iteration could be to just ignore them in the parse function with an ignoreInvalidLines flag to at least not throw on read and just not support that usecase. But still those lines would needed to be preserved on the output file.

maxmarkus avatar Feb 29 '24 10:02 maxmarkus

Correct me if I'm wrong but I don't think .env supports multiline values?

MirazMac avatar Feb 29 '24 10:02 MirazMac

We use this .env in a laravel project and env('OAUTH_PRIVATE_KEY') shows the multiline key properly. Apart from that I also have not seen it multilined in other places before, so I am not sure if its worth investigating. I have a very specific usecase and can do it a different way without this lib. Just wanted to note down what I found :-)

maxmarkus avatar Feb 29 '24 12:02 maxmarkus

What about a multi-line-support-mode?

basteyy avatar Jul 28 '24 18:07 basteyy

+1 on this.

Also have private key data on .env for OAUTH and Laravel works with it perfectly.

mariomeyer avatar May 14 '25 15:05 mariomeyer

Opened a PR to fix this. Would be great if this could be merged and a new version deployed, @MirazMac

mariomeyer avatar May 14 '25 16:05 mariomeyer

I thought about a mode, where the user needs to enable the multi-line support. Enable it by default .. I'm not sure about that

basteyy avatar May 14 '25 17:05 basteyy

@basteyy It's simpler just to check for open and unclosed quotes than having people go through documentation to understand there's a mode setting to do that. If there's an unclosed quote the .env should fail parsing on other packages as well. At least that's my pov

mariomeyer avatar May 14 '25 17:05 mariomeyer

Should be fixed by #7 please upgrade to 0.6

MirazMac avatar May 22 '25 17:05 MirazMac