4
4
5
5
use Illuminate \Support \Str ;
6
6
use Illuminate \Console \Command ;
7
- use Symfony \Component \Console \Input \InputOption ;
8
7
9
8
class KeyGenerateCommand extends Command
10
9
{
11
10
/**
12
- * The console command name .
11
+ * The name and signature of the console command.
13
12
*
14
13
* @var string
15
14
*/
16
- protected $ name = 'key:generate ' ;
15
+ protected $ signature = 'key:generate {--show : Display the key instead of modifying files} ' ;
17
16
18
17
/**
19
18
* The console command description.
@@ -29,55 +28,46 @@ class KeyGenerateCommand extends Command
29
28
*/
30
29
public function fire ()
31
30
{
32
- $ app = $ this ->laravel ;
33
-
34
- $ key = $ this ->getRandomKey ($ app ['config ' ]['app.cipher ' ]);
31
+ $ key = $ this ->generateRandomKey ();
35
32
36
33
if ($ this ->option ('show ' )) {
37
34
return $ this ->line ('<comment> ' .$ key .'</comment> ' );
38
35
}
39
36
40
- $ path = $ app ->environmentPath ().'/ ' .$ app ->environmentFile ();
41
-
42
- if (file_exists ($ path )) {
43
- $ content = str_replace ('APP_KEY= ' .$ app ['config ' ]['app.key ' ], 'APP_KEY= ' .$ key , file_get_contents ($ path ));
44
-
45
- if (! Str::contains ($ content , 'APP_KEY ' )) {
46
- $ content = sprintf ("%s \nAPP_KEY=%s \n" , $ content , $ key );
47
- }
37
+ // Next, we will replace the application key in the environment file so it is
38
+ // automatically setup for this developer. This key gets generated using a
39
+ // secure random byte generator and is later base64 encoded for storage.
40
+ $ this ->setKeyInEnvironmentFile ($ key );
48
41
49
- file_put_contents ($ path , $ content );
50
- }
51
-
52
- $ app ['config ' ]['app.key ' ] = $ key ;
42
+ $ this ->laravel ['config ' ]['app.key ' ] = $ key ;
53
43
54
44
$ this ->info ("Application key [ $ key] set successfully. " );
55
45
}
56
46
57
47
/**
58
- * Generate a random key for the application .
48
+ * Set the environmeny key in the environment file .
59
49
*
60
- * @param string $cipher
61
- * @return string
50
+ * @param string $key
51
+ * @return void
62
52
*/
63
- protected function getRandomKey ( $ cipher )
53
+ protected function setKeyInEnvironmentFile ( $ key )
64
54
{
65
- if ( $ cipher === ' AES-128-CBC ' ) {
66
- return Str:: random ( 16 );
67
- }
68
-
69
- return Str:: random ( 32 );
55
+ file_put_contents ( $ this -> laravel -> environmentFilePath (), str_replace (
56
+ ' APP_KEY= ' . $ this -> laravel [ ' config ' ][ ' app.key ' ],
57
+ ' APP_KEY= ' . $ key ,
58
+ file_get_contents ( $ this -> laravel -> environmentFilePath ())
59
+ ) );
70
60
}
71
61
72
62
/**
73
- * Get the console command options .
63
+ * Generate a random key for the application .
74
64
*
75
- * @return array
65
+ * @return string
76
66
*/
77
- protected function getOptions ()
67
+ protected function generateRandomKey ()
78
68
{
79
- return [
80
- [ ' show ' , null , InputOption:: VALUE_NONE , ' Simply display the key instead of modifying files. ' ],
81
- ] ;
69
+ return ' base64: ' . base64_encode ( random_bytes (
70
+ $ this -> laravel [ ' config ' ][ ' app.cipher ' ] == ' AES-128-CBC ' ? 16 : 32
71
+ )) ;
82
72
}
83
73
}
0 commit comments