@@ -77,14 +77,17 @@ public function handle(): void
77
77
*/
78
78
public function setEnvVariable (string $ envFileContent , string $ key , string $ value ): array
79
79
{
80
+ $ oldPair = $ this ->readKeyValuePair ($ envFileContent , $ key );
81
+ $ newPair = $ key . '= ' . $ value ;
82
+
80
83
// 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 ];
84
87
}
85
88
86
89
// For a new key.
87
- return [$ envFileContent . "\n" . $ key . ' = ' . $ value . "\n" , true ];
90
+ return [$ envFileContent . "\n" . $ newPair . "\n" , true ];
88
91
}
89
92
90
93
/**
@@ -99,7 +102,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu
99
102
public function readKeyValuePair (string $ envFileContent , string $ key ): ?string
100
103
{
101
104
// 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 )) {
103
106
return $ matches [0 ];
104
107
}
105
108
@@ -122,7 +125,7 @@ public function parseCommandArguments(string $_key, ?string $_value, ?string $_e
122
125
$ envFilePath = null ;
123
126
124
127
// Parse "key=value" key argument.
125
- if (preg_match ('#^([^=]+)=(.*)$#umu ' , $ _key , $ matches )) {
128
+ if (preg_match ('#^([^=]+)=(.*)$#umU ' , $ _key , $ matches )) {
126
129
[1 => $ key , 2 => $ value ] = $ matches ;
127
130
128
131
// Use second argument as path to env file:
@@ -142,7 +145,7 @@ public function parseCommandArguments(string $_key, ?string $_value, ?string $_e
142
145
$ this ->assertKeyIsValid ($ key );
143
146
144
147
// 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 )) {
146
149
$ value = '" ' . $ value . '" ' ;
147
150
}
148
151
0 commit comments