Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Console\ConfirmableTrait;
7
use Illuminate\Encryption\Encrypter;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
 
10
#[AsCommand(name: 'key:generate')]
11
class KeyGenerateCommand extends Command
12
{
13
    use ConfirmableTrait;
14
 
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'key:generate
21
                    {--show : Display the key instead of modifying files}
22
                    {--force : Force the operation to run when in production}';
23
 
24
    /**
25
     * The name of the console command.
26
     *
27
     * This name is used to identify the command during lazy loading.
28
     *
29
     * @var string|null
30
     *
31
     * @deprecated
32
     */
33
    protected static $defaultName = 'key:generate';
34
 
35
    /**
36
     * The console command description.
37
     *
38
     * @var string
39
     */
40
    protected $description = 'Set the application key';
41
 
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return void
46
     */
47
    public function handle()
48
    {
49
        $key = $this->generateRandomKey();
50
 
51
        if ($this->option('show')) {
52
            return $this->line('<comment>'.$key.'</comment>');
53
        }
54
 
55
        // Next, we will replace the application key in the environment file so it is
56
        // automatically setup for this developer. This key gets generated using a
57
        // secure random byte generator and is later base64 encoded for storage.
58
        if (! $this->setKeyInEnvironmentFile($key)) {
59
            return;
60
        }
61
 
62
        $this->laravel['config']['app.key'] = $key;
63
 
64
        $this->components->info('Application key set successfully.');
65
    }
66
 
67
    /**
68
     * Generate a random key for the application.
69
     *
70
     * @return string
71
     */
72
    protected function generateRandomKey()
73
    {
74
        return 'base64:'.base64_encode(
75
            Encrypter::generateKey($this->laravel['config']['app.cipher'])
76
        );
77
    }
78
 
79
    /**
80
     * Set the application key in the environment file.
81
     *
82
     * @param  string  $key
83
     * @return bool
84
     */
85
    protected function setKeyInEnvironmentFile($key)
86
    {
87
        $currentKey = $this->laravel['config']['app.key'];
88
 
89
        if (strlen($currentKey) !== 0 && (! $this->confirmToProceed())) {
90
            return false;
91
        }
92
 
93
        if (! $this->writeNewEnvironmentFileWith($key)) {
94
            return false;
95
        }
96
 
97
        return true;
98
    }
99
 
100
    /**
101
     * Write a new environment file with the given key.
102
     *
103
     * @param  string  $key
104
     * @return bool
105
     */
106
    protected function writeNewEnvironmentFileWith($key)
107
    {
108
        $replaced = preg_replace(
109
            $this->keyReplacementPattern(),
110
            'APP_KEY='.$key,
111
            $input = file_get_contents($this->laravel->environmentFilePath())
112
        );
113
 
114
        if ($replaced === $input || $replaced === null) {
115
            $this->error('Unable to set application key. No APP_KEY variable was found in the .env file.');
116
 
117
            return false;
118
        }
119
 
120
        file_put_contents($this->laravel->environmentFilePath(), $replaced);
121
 
122
        return true;
123
    }
124
 
125
    /**
126
     * Get a regex pattern that will match env APP_KEY with any random key.
127
     *
128
     * @return string
129
     */
130
    protected function keyReplacementPattern()
131
    {
132
        $escaped = preg_quote('='.$this->laravel['config']['app.key'], '/');
133
 
134
        return "/^APP_KEY{$escaped}/m";
135
    }
136
}