Skip to content

Commit 1a794a8

Browse files
committed
Properly set values containing dollar signs
1 parent 88ededf commit 1a794a8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/DotEnvUpdater.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ protected function restoreValue($value)
133133
*/
134134
protected function replaceExistingLine($key, $value, $env)
135135
{
136-
return preg_replace($this->keyReplacementPattern($key), $this->formatKeyValuePair($key, $value), $env);
136+
return preg_replace_callback($this->keyReplacementPattern($key), function () use ($key, $value) {
137+
return $this->formatKeyValuePair($key, $value);
138+
}, $env);
137139
}
138140

139141
/**

tests/DotEnvUpdaterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,24 @@ public function it_sets_an_empty_value()
148148
]);
149149
}
150150

151+
/** @test */
152+
public function it_sets_a_value_with_dollar_sign()
153+
{
154+
$this->createEnvFile([
155+
'EXISTING_KEY="String Value"',
156+
]);
157+
158+
$updater = new DotEnvUpdater($this->getEnvPath());
159+
160+
$updater->set('EXISTING_KEY', '$2y$10$mQZg');
161+
162+
$this->assertEquals('$2y$10$mQZg', $updater->get('EXISTING_KEY'));
163+
164+
$this->assertEnvFileContents([
165+
'EXISTING_KEY="$2y$10$mQZg"',
166+
]);
167+
}
168+
151169
/**
152170
* Assert that the .env file contains the given lines of content.
153171
*

0 commit comments

Comments
 (0)