Skip to content

Commit 0d16d35

Browse files
Fixed working with nested key names (for example, APP_KEY and PUSHER_APP_KEY).
1 parent 1194241 commit 0d16d35

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
### Security
1212

1313

14+
## [1.1.5] - 2020-05-26
15+
### Fixed
16+
- Fixed working with nested key names (for example, APP_KEY and PUSHER_APP_KEY).
17+
1418
## [1.1.4] - 2020-05-13
1519
### Added
1620
- Added the command description.
@@ -47,6 +51,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4751
### Added
4852
- Initial release
4953

54+
[1.1.5]: https://github.com/imliam/laravel-env-set-command/compare/1.1.4...1.1.5
5055
[1.1.4]: https://github.com/imliam/laravel-env-set-command/compare/1.1.3...1.1.4
5156
[1.1.3]: https://github.com/imliam/laravel-env-set-command/compare/1.1.2...1.1.3
5257
[1.1.2]: https://github.com/imliam/laravel-env-set-command/compare/1.1.1...1.1.2

src/EnvironmentSetCommand.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,17 @@ public function handle(): void
7777
*/
7878
public function setEnvVariable(string $envFileContent, string $key, string $value): array
7979
{
80+
$oldPair = $this->readKeyValuePair($envFileContent, $key);
81+
$newPair = $key . '=' . $value;
82+
8083
// For existed key.
81-
$oldKeyValuePair = $this->readKeyValuePair($envFileContent, $key);
82-
if ($oldKeyValuePair !== null) {
83-
return [str_replace($oldKeyValuePair, $key . '=' . $value, $envFileContent), false];
84+
if ($oldPair !== null) {
85+
$replaced = preg_replace('/^' . preg_quote($oldPair, '/') . '$/uimU', $newPair, $envFileContent);
86+
return [$replaced, false];
8487
}
8588

8689
// For a new key.
87-
return [$envFileContent . "\n" . $key . '=' . $value . "\n", true];
90+
return [$envFileContent . "\n" . $newPair . "\n", true];
8891
}
8992

9093
/**
@@ -99,7 +102,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu
99102
public function readKeyValuePair(string $envFileContent, string $key): ?string
100103
{
101104
// Match the given key at the beginning of a line
102-
if (preg_match("#^ *{$key} *= *[^\r\n]*$#imu", $envFileContent, $matches)) {
105+
if (preg_match("#^ *{$key} *= *[^\r\n]*$#uimU", $envFileContent, $matches)) {
103106
return $matches[0];
104107
}
105108

@@ -122,7 +125,7 @@ public function parseCommandArguments(string $_key, ?string $_value, ?string $_e
122125
$envFilePath = null;
123126

124127
// Parse "key=value" key argument.
125-
if (preg_match('#^([^=]+)=(.*)$#umu', $_key, $matches)) {
128+
if (preg_match('#^([^=]+)=(.*)$#umU', $_key, $matches)) {
126129
[1 => $key, 2 => $value] = $matches;
127130

128131
// Use second argument as path to env file:
@@ -142,7 +145,7 @@ public function parseCommandArguments(string $_key, ?string $_value, ?string $_e
142145
$this->assertKeyIsValid($key);
143146

144147
// If the value contains spaces but not is not enclosed in quotes.
145-
if (preg_match('#^[^\'"].*\s+.*[^\'"]$#um', $value)) {
148+
if (preg_match('#^[^\'"].*\s+.*[^\'"]$#umU', $value)) {
146149
$value = '"' . $value . '"';
147150
}
148151

tests/Unit/EnvironmentSetCommandTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ public function testSetEnvVariable(
4242
$this->assertEquals($expectedNewEnvFile, $newEnvFileContent);
4343
}
4444

45+
/**
46+
* @covers EnvironmentSetCommand::setEnvVariable
47+
*/
48+
public function testSetEnvVariableTestOfNestedKeys(): void
49+
{
50+
// APP_KEY is a subset of PUSHER_APP_KEY:
51+
$env = '# it is a comment' . "\n"
52+
. 'APP_KEY=' . "\n"
53+
. 'PUSHER_APP_KEY=' . "\n"
54+
. 'some_key=some_value' . "\n";
55+
56+
$expectedEnv = '# it is a comment' . "\n"
57+
. 'APP_KEY=test' . "\n"
58+
. 'PUSHER_APP_KEY=' . "\n"
59+
. 'some_key=some_value' . "\n";
60+
61+
[$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_KEY', 'test');
62+
$this->assertEquals($expectedEnv, $newEnv);
63+
}
64+
4565
/**
4666
* @covers EnvironmentSetCommand::readKeyValuePair
4767
* @dataProvider readKeyValuePairDataProvider
@@ -91,6 +111,7 @@ public function testAssertKeyIsValid(string $key, bool $isGood): void
91111
*/
92112
public function setEnvVariableDataProvider(): array
93113
{
114+
// Unfortunately, we can't test nested key names using str_replace().
94115
$envFileContent = $this->getTestEnvFile();
95116
return [
96117
[

0 commit comments

Comments
 (0)