@@ -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
0 commit comments