@@ -165,36 +165,41 @@ private function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): bo
165165 }
166166
167167 $ oldFileContents = (string ) file_get_contents ($ envFile );
168- $ replacementKey = "\nencryption.key = {$ newKey }" ;
169168
170- if (! str_contains ($ oldFileContents , 'encryption.key ' )) {
171- return file_put_contents ($ envFile , $ replacementKey , FILE_APPEND ) !== false ;
169+ // Match an active setting line, preserving any leading whitespace and `export` prefix.
170+ $ activePattern = $ this ->keyPattern ($ oldKey );
171+
172+ if (preg_match ($ activePattern , $ oldFileContents ) === 1 ) {
173+ $ newFileContents = (string ) preg_replace ($ activePattern , '$1 ' . $ newKey , $ oldFileContents , 1 );
174+
175+ return file_put_contents ($ envFile , $ newFileContents ) !== false ;
172176 }
173177
174- $ newFileContents = preg_replace ($ this ->keyPattern ($ oldKey ), $ replacementKey , $ oldFileContents );
178+ // Match a commented-out setting line (e.g., from the shipped `env` template) and
179+ // uncomment it. The optional `export` prefix is dropped on uncomment for predictability.
180+ $ commentedPattern = '/^\h*#\h*(?:export\h+)?encryption\.key\h*=\h*[^\r\n]*$/m ' ;
181+
182+ if (preg_match ($ commentedPattern , $ oldFileContents ) === 1 ) {
183+ $ newFileContents = (string ) preg_replace ($ commentedPattern , "encryption.key = {$ newKey }" , $ oldFileContents , 1 );
175184
176- if ($ newFileContents === $ oldFileContents ) {
177- $ newFileContents = preg_replace (
178- '/^[#\s]*encryption.key[=\s]*(?:hex2bin\:[a-f0-9]{64}|base64\:(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?)$/m ' ,
179- $ replacementKey ,
180- $ oldFileContents ,
181- );
185+ return file_put_contents ($ envFile , $ newFileContents ) !== false ;
182186 }
183187
184- return file_put_contents ($ envFile , $ newFileContents ) !== false ;
188+ // No setting present (active or commented); append.
189+ return file_put_contents ($ envFile , "\nencryption.key = {$ newKey }" , FILE_APPEND ) !== false ;
185190 }
186191
187192 /**
188- * Get the regex of the current encryption key.
193+ * Returns the regex used to locate an active `encryption.key = ...` setting in the `.env`
194+ * contents. The single capture group spans everything up to (and including) the `=` and any
195+ * separating whitespace, so a `preg_replace` substitution preserves an optional `export`
196+ * prefix while rewriting only the value.
197+ *
198+ * The `$oldKey` parameter is retained for backward compatibility with subclasses that
199+ * override this method; it is no longer consulted because the pattern matches any value.
189200 */
190201 private function keyPattern (string $ oldKey ): string
191202 {
192- $ escaped = preg_quote ($ oldKey , '/ ' );
193-
194- if ($ escaped !== '' ) {
195- $ escaped = "[ {$ escaped }]* " ;
196- }
197-
198- return "/^[# \\s]*encryption.key[= \\s]* {$ escaped }$/m " ;
203+ return '/^(\h*(?:export\h+)?encryption\.key\h*=\h*)[^\r\n]*$/m ' ;
199204 }
200205}
0 commit comments