Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Encryption\Encrypter;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Str;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
 
12
#[AsCommand(name: 'env:encrypt')]
13
class EnvironmentEncryptCommand extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'env:encrypt
21
                    {--key= : The encryption key}
22
                    {--cipher= : The encryption cipher}
23
                    {--env= : The environment to be encrypted}
24
                    {--force : Overwrite the existing encrypted environment file}';
25
 
26
    /**
27
     * The name of the console command.
28
     *
29
     * This name is used to identify the command during lazy loading.
30
     *
31
     * @var string|null
32
     *
33
     * @deprecated
34
     */
35
    protected static $defaultName = 'env:encrypt';
36
 
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Encrypt an environment file';
43
 
44
    /**
45
     * The filesystem instance.
46
     *
47
     * @var \Illuminate\Filesystem\Filesystem
48
     */
49
    protected $files;
50
 
51
    /**
52
     * Create a new command instance.
53
     *
54
     * @param  \Illuminate\Filesystem\Filesystem  $files
55
     * @return void
56
     */
57
    public function __construct(Filesystem $files)
58
    {
59
        parent::__construct();
60
 
61
        $this->files = $files;
62
    }
63
 
64
    /**
65
     * Execute the console command.
66
     *
67
     * @return void
68
     */
69
    public function handle()
70
    {
71
        $cipher = $this->option('cipher') ?: 'AES-256-CBC';
72
 
73
        $key = $this->option('key');
74
 
75
        $keyPassed = $key !== null;
76
 
77
        $environmentFile = $this->option('env')
78
                            ? base_path('.env').'.'.$this->option('env')
79
                            : $this->laravel->environmentFilePath();
80
 
81
        $encryptedFile = $environmentFile.'.encrypted';
82
 
83
        if (! $keyPassed) {
84
            $key = Encrypter::generateKey($cipher);
85
        }
86
 
87
        if (! $this->files->exists($environmentFile)) {
88
            $this->components->error('Environment file not found.');
89
 
90
            return Command::FAILURE;
91
        }
92
 
93
        if ($this->files->exists($encryptedFile) && ! $this->option('force')) {
94
            $this->components->error('Encrypted environment file already exists.');
95
 
96
            return Command::FAILURE;
97
        }
98
 
99
        try {
100
            $encrypter = new Encrypter($this->parseKey($key), $cipher);
101
 
102
            $this->files->put(
103
                $encryptedFile,
104
                $encrypter->encrypt($this->files->get($environmentFile))
105
            );
106
        } catch (Exception $e) {
107
            $this->components->error($e->getMessage());
108
 
109
            return Command::FAILURE;
110
        }
111
 
112
        $this->components->info('Environment successfully encrypted.');
113
 
991 lars 114
        $this->components->twoColumnDetail('Key', $keyPassed ? $key : 'base64:'.base64_encode($key));
148 lars 115
        $this->components->twoColumnDetail('Cipher', $cipher);
116
        $this->components->twoColumnDetail('Encrypted file', $encryptedFile);
117
 
118
        $this->newLine();
119
    }
120
 
121
    /**
122
     * Parse the encryption key.
123
     *
124
     * @param  string  $key
125
     * @return string
126
     */
127
    protected function parseKey(string $key)
128
    {
129
        if (Str::startsWith($key, $prefix = 'base64:')) {
130
            $key = base64_decode(Str::after($key, $prefix));
131
        }
132
 
133
        return $key;
134
    }
135
}