|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of laravel-env-security. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Signature Tech Studio, Inc <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace STS\EnvSecurity\Console; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Console\Command; |
|
14
|
|
|
use STS\EnvSecurity\Console\Concerns\HandlesEnvFiles; |
|
15
|
|
|
use STS\EnvSecurity\EnvSecurityManager; |
|
16
|
|
|
use Symfony\Component\Process\Process; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Edit |
|
20
|
|
|
* @package STS\EnvSecurity\Console |
|
21
|
|
|
*/ |
|
22
|
|
|
class Edit extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
use HandlesEnvFiles; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The name and signature of the console command. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $signature = 'env:edit {environment : Which environment file you wish to decrypt}'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The console command description. |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $description = 'Edit an encrypted env file'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var EnvSecurityManager |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $envSecurity; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(EnvSecurityManager $envSecurity) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->envSecurity = $envSecurity; |
|
48
|
|
|
|
|
49
|
|
|
parent::__construct(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Execute the console command. |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function handle() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->saveEnvContents( |
|
60
|
|
|
$this->edit( |
|
61
|
|
|
$this->loadEnvContents() |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
$this->info("Successfully updated .env for environment [{$this->environment()}]"); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $contents |
|
70
|
|
|
* @codeCoverageIgnore |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function edit($contents) |
|
75
|
|
|
{ |
|
76
|
|
|
$tmpFile = tmpfile(); |
|
77
|
|
|
fwrite($tmpFile, $contents); |
|
|
|
|
|
|
78
|
|
|
$meta = stream_get_meta_data($tmpFile); |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$process = new Process(config('env-security.editor') . ' ' . $meta['uri']); |
|
|
|
|
|
|
81
|
|
|
$process->setTty(true); |
|
82
|
|
|
$process->mustRun(); |
|
83
|
|
|
|
|
84
|
|
|
return file_get_contents($meta['uri']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function environment() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->argument('environment'); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function loadEnvContents() |
|
99
|
|
|
{ |
|
100
|
|
|
if($ciphertext = $this->loadEncrypted($this->environment())) { |
|
101
|
|
|
return $this->envSecurity->decrypt($ciphertext); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return ''; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $plaintext |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function saveEnvContents($plaintext) |
|
111
|
|
|
{ |
|
112
|
|
|
$ciphertext = !empty($plaintext) |
|
113
|
|
|
? $this->envSecurity->encrypt($plaintext) |
|
|
|
|
|
|
114
|
|
|
: ''; |
|
115
|
|
|
|
|
116
|
|
|
$this->saveEncrypted($ciphertext, $this->environment()); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|