@@ -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,13 @@ 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+ $ envFile = realpath ($ envFile );
139+
140+ assert (is_string ($ envFile ));
141+
142+ if (! $ this ->writePreviousKeys ($ previousKeys , $ envFile )) {
143+ CLI ::error (sprintf ('Failed to write `encryption.previousKeys` to %s. ' , $ envFile ));
138144
139145 return EXIT_ERROR ;
140146 }
@@ -225,10 +231,8 @@ private function mergePreviousKeys(string $currentKey, array $existing, int $kee
225231 *
226232 * @param list<string> $previousKeys
227233 */
228- private function writePreviousKeys (array $ previousKeys ): bool
234+ private function writePreviousKeys (array $ previousKeys, string $ envFile ): bool
229235 {
230- $ envFile = ((new Paths ())->envDirectory ?? ROOTPATH ) . '.env ' ; // @phpstan-ignore nullCoalesce.property
231-
232236 if (! is_file ($ envFile )) {
233237 return false ; // @codeCoverageIgnore
234238 }
@@ -259,11 +263,9 @@ private function writePreviousKeys(array $previousKeys): bool
259263 );
260264
261265 if ($ injected === $ contents ) {
262- // @codeCoverageIgnoreStart
263266 // Fallback: append to the end. Shouldn't trigger because `key:generate`
264267 // writes the `encryption.key` line just before this method runs.
265- $ injected = $ contents . "\nencryption.previousKeys = {$ value }" ;
266- // @codeCoverageIgnoreEnd
268+ $ injected = $ contents . "\nencryption.previousKeys = {$ value }" ; // @codeCoverageIgnore
267269 }
268270
269271 return file_put_contents ($ envFile , $ injected ) !== false ;
0 commit comments