dotenv-editor
dotenv-editor copied to clipboard
DotenvEditor::format() should quotes values when they contain certain characters
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.
https://github.com/sixlive/dotenv-editor/blob/edf0a7523fd2c49a435feffad7524ad60475cb47/src/DotenvEditor.php#L155
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
This is definitely something that should be implemented. Would you be willing work open a PR for this?