@@ -103,12 +103,13 @@ protected function execute(array $arguments, array $options): int
103103
104104 if ($ options ['force ' ] === false ) {
105105 if ($ this ->isInteractive ()) {
106- CLI ::error ('Key rotation cancelled. ' );
107- } else {
108- CLI ::error ('Key rotation aborted. ' );
109- CLI ::error ('If you want, use the "--force" option to force the rotation. ' );
106+ CLI ::write ('Key rotation cancelled. ' , 'yellow ' );
107+
108+ return EXIT_SUCCESS ;
110109 }
111110
111+ CLI ::error ('Key rotation aborted: pass --force to rotate the encryption key in non-interactive mode. ' );
112+
112113 return EXIT_ERROR ;
113114 }
114115
@@ -133,8 +134,10 @@ protected function execute(array $arguments, array $options): int
133134 // Write previousKeys first. If the subsequent `key:generate` call fails,
134135 // the worst case is a stale-but-still-decryptable `.env` (the rotated-out
135136 // key is preserved on disk).
136- if (! $ this ->writePreviousKeys ($ previousKeys )) {
137- CLI ::error ('Error in writing `encryption.previousKeys` to `.env` file. ' );
137+ $ envFile = ((new Paths ())->envDirectory ?? ROOTPATH ) . '.env ' ; // @phpstan-ignore nullCoalesce.property
138+
139+ if (! $ this ->writePreviousKeys ($ previousKeys , $ envFile )) {
140+ CLI ::error (sprintf ('Failed to write `encryption.previousKeys` to %s. ' , clean_path ($ envFile )));
138141
139142 return EXIT_ERROR ;
140143 }
@@ -225,10 +228,8 @@ private function mergePreviousKeys(string $currentKey, array $existing, int $kee
225228 *
226229 * @param list<string> $previousKeys
227230 */
228- private function writePreviousKeys (array $ previousKeys ): bool
231+ private function writePreviousKeys (array $ previousKeys, string $ envFile ): bool
229232 {
230- $ envFile = ((new Paths ())->envDirectory ?? ROOTPATH ) . '.env ' ; // @phpstan-ignore nullCoalesce.property
231-
232233 if (! is_file ($ envFile )) {
233234 return false ; // @codeCoverageIgnore
234235 }
@@ -259,11 +260,9 @@ private function writePreviousKeys(array $previousKeys): bool
259260 );
260261
261262 if ($ injected === $ contents ) {
262- // @codeCoverageIgnoreStart
263263 // Fallback: append to the end. Shouldn't trigger because `key:generate`
264264 // writes the `encryption.key` line just before this method runs.
265- $ injected = $ contents . "\nencryption.previousKeys = {$ value }" ;
266- // @codeCoverageIgnoreEnd
265+ $ injected = $ contents . "\nencryption.previousKeys = {$ value }" ; // @codeCoverageIgnore
267266 }
268267
269268 return file_put_contents ($ envFile , $ injected ) !== false ;
0 commit comments