Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/DotenvEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class DotenvEditor
*/
protected $env = [];

/**
* @var array
*/
protected $tracked = [];

/**
* @var \sixlive\DotenvEditor\EnvFile
*/
Expand Down Expand Up @@ -50,6 +55,7 @@ public function load($path)
public function set($key, $value)
{
$this->env[$key] = $value;
$this->tracked[] = $key;

return $this;
}
Expand Down Expand Up @@ -151,6 +157,12 @@ public function __destruct()
private function format()
{
$valuePairs = Arr::mapWithKeys($this->env, function ($item, $key) {
// If we are adding the key we should wrap the contents to prevent
// any special characters from leaking through.
if (in_array($key, $this->tracked)) {
return sprintf('%s="%s"', $key, $item);
}

return is_string($key)
? sprintf('%s=%s', $key, $item)
: $item;
Expand Down
16 changes: 8 additions & 8 deletions tests/DotenvEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function config_values_can_be_saved()
$editor->set('EXAMPLE_CONFIG', 'foo');
$editor->save();

$this->assertFileContents('EXAMPLE_CONFIG=foo', $this->path);
$this->assertFileContents('EXAMPLE_CONFIG="foo"', $this->path);
}

/** @test */
Expand All @@ -87,7 +87,7 @@ public function config_values_can_be_saved_to_a_new_path()
$editor->set('EXAMPLE_CONFIG', 'foo');
$editor->save($newPath);

$this->assertFileContents('EXAMPLE_CONFIG=foo', $newPath);
$this->assertFileContents('EXAMPLE_CONFIG="foo"', $newPath);
}

/** @test */
Expand All @@ -103,7 +103,7 @@ public function multiple_config_values_can_be_saved()
$editor->save();

$this->assertFileContents(
"EXAMPLE_CONFIG=foo\nEXAMPLE_CONFIG_2=bar",
"EXAMPLE_CONFIG=\"foo\"\nEXAMPLE_CONFIG_2=\"bar\"",
$this->path
);
}
Expand All @@ -120,7 +120,7 @@ public function line_breaks_can_be_added()
$editor->save();

$this->assertFileContents(
"EXAMPLE_CONFIG=foo\n\nEXAMPLE_CONFIG_2=bar",
"EXAMPLE_CONFIG=\"foo\"\n\nEXAMPLE_CONFIG_2=\"bar\"",
$this->path
);
}
Expand All @@ -136,7 +136,7 @@ public function headings_can_be_added()
$editor->save();

$this->assertFileContents(
"# Examples\nEXAMPLE_CONFIG=foo",
"# Examples\nEXAMPLE_CONFIG=\"foo\"",
$this->path
);
}
Expand All @@ -155,7 +155,7 @@ public function headings_get_added_with_a_new_line_after_a_non_blank_entry()
$editor->save();

$this->assertFileContents(
"APP_KEY=bar\n\n# Examples\nEXAMPLE_CONFIG=foo",
"APP_KEY=\"bar\"\n\n# Examples\nEXAMPLE_CONFIG=\"foo\"",
$this->path
);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public function configuration_values_can_be_merge_with_an_existing_config()
$editor->save();

$this->assertFileContents(
"EXAMPLE=bar\n\n# Section\nEXAMPLE_3=bar\n\n# Foo\nFOO=bar",
"EXAMPLE=bar\n\n# Section\nEXAMPLE_3=bar\n\n# Foo\nFOO=\"bar\"",
$this->path
);
}
Expand All @@ -217,7 +217,7 @@ public function leaves_blank_settings_as_they_were()
$editor->save();

$this->assertFileContents(
file_get_contents($fixturePath)."\nFOO=bar",
file_get_contents($fixturePath)."\nFOO=\"bar\"",
$this->path
);
}
Expand Down