diff --git a/src/DotenvEditor.php b/src/DotenvEditor.php index 2032eb5..cac49f0 100644 --- a/src/DotenvEditor.php +++ b/src/DotenvEditor.php @@ -12,6 +12,11 @@ class DotenvEditor */ protected $env = []; + /** + * @var array + */ + protected $tracked = []; + /** * @var \sixlive\DotenvEditor\EnvFile */ @@ -50,6 +55,7 @@ public function load($path) public function set($key, $value) { $this->env[$key] = $value; + $this->tracked[] = $key; return $this; } @@ -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; diff --git a/tests/DotenvEditorTest.php b/tests/DotenvEditorTest.php index ca9634e..740820e 100644 --- a/tests/DotenvEditorTest.php +++ b/tests/DotenvEditorTest.php @@ -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 */ @@ -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 */ @@ -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 ); } @@ -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 ); } @@ -136,7 +136,7 @@ public function headings_can_be_added() $editor->save(); $this->assertFileContents( - "# Examples\nEXAMPLE_CONFIG=foo", + "# Examples\nEXAMPLE_CONFIG=\"foo\"", $this->path ); } @@ -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 ); } @@ -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 ); } @@ -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 ); }