When exporting a .env file, values are never wrapped in quotes. When a value contains certain characters, like =, $, or \n (new line), this causes the resultant contents to be invalid.
E.g.
<?php
use sixlive\DotenvEditor\DotenvEditor;
include "vendor/autoload.php";
$editor = new DotenvEditor;
$editor->load(__DIR__.'/.env');
$editor->heading('Examples');
$editor->set('A', 't9maWr2GA6=');
$editor->set('B', 'this-should-be-$ingle-quoted');
$editor->set('C', 'multi
line
should be
quoted');
$editor->save();
Results in
# Examples
A=t9maWr2GA6=
B=this-should-be-$ingle-quoted
C=multi
line
should be
quoted
This is not a valid dotenv file and, for example, Laravel will not be able to load it.
|
? sprintf('%s=%s', $key, $item) |
There needs to be some different preparation of values, that need quoting, prior to writing them to the .env file.
For example:
- If contains
=, #, or a $ as part of variable/command substitution (e.g. "$(bleh)" or "${FOO}") then wrap in double quotes
- If contains
$ not used for variable/command substitution, then wrap in single quotes
- If contains new line character (
\n) either wrap in double quotes and\or convert the new line characters to a \n literal string
When exporting a
.envfile, values are never wrapped in quotes. When a value contains certain characters, like=,$, or\n(new line), this causes the resultant contents to be invalid.E.g.
Results in
This is not a valid dotenv file and, for example, Laravel will not be able to load it.
dotenv-editor/src/DotenvEditor.php
Line 155 in edf0a75
There needs to be some different preparation of values, that need quoting, prior to writing them to the
.envfile.For example:
=,#, or a$as part of variable/command substitution (e.g."$(bleh)"or"${FOO}") then wrap in double quotes$not used for variable/command substitution, then wrap in single quotes\n) either wrap in double quotes and\or convert the new line characters to a\nliteral string