Skip to content

Commit 2e08f2f

Browse files
committed
Handle quoted values
1 parent 080b04e commit 2e08f2f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/EnvironmentSetCommand.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu
8080
$oldPair = $this->readKeyValuePair($envFileContent, $key);
8181

8282
// Wrap values that have a space or equals in quotes to escape them
83-
if (preg_match('/\s/',$value) || strpos($value, '=') !== false) {
84-
$value = '"' . $value . '"';
83+
$trimmedValue = trim($value);
84+
if (!str_starts_with($trimmedValue, '"') && !str_ends_with($trimmedValue, '"') && preg_match('/\s/',$value) || strpos($value, '=') !== false) {
85+
$value = '"' . str_replace('"', '\"', $value) . '"';
8586
}
8687

8788
$newPair = $key . '=' . $value;

tests/Unit/EnvironmentSetCommandTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ public function testWhitespaceAsValueDoesntCreateNewEntry(): void
7070
$this->assertEquals($expectedEnv, $newEnv);
7171
}
7272

73+
/**
74+
* @covers EnvironmentSetCommand::setEnvVariable
75+
*/
76+
public function testQuotedCharactersArePreserved(): void
77+
{
78+
$env = 'APP_NAME=' . "\n";
79+
80+
$expectedEnv = 'APP_NAME="MY.NAME & C."' . "\n";
81+
82+
[$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_NAME', 'MY.NAME & C.');
83+
$this->assertEquals($expectedEnv, $newEnv);
84+
}
85+
7386
/**
7487
* @covers EnvironmentSetCommand::readKeyValuePair
7588
* @dataProvider readKeyValuePairDataProvider

0 commit comments

Comments
 (0)