/
EnvFile.php
166 lines (129 loc) · 4.84 KB
/
EnvFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace REBELinBLUE\Deployer\Console\Commands\Installer;
use REBELinBLUE\Deployer\Services\Filesystem\Filesystem;
/**
* Class to update the .env file.
*/
class EnvFile
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* EnvFile constructor.
*
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
/**
* Saves the config file.
*
* @param array $input
*
* @return int
*/
public function save(array $input)
{
$path = base_path('.env');
$config = $this->filesystem->get($path);
foreach ($input as $section => $data) {
foreach ($data as $key => $value) {
$env = strtoupper($section . '_' . $key);
$config = preg_replace('/' . $env . '=(.*)/', $env . '=' . $value, $config);
}
}
// Remove SSL certificate keys if not using HTTPS
if (substr($input['socket']['url'], 0, 5) !== 'https') {
foreach (['key', 'cert', 'ca'] as $key) {
$key = strtoupper($key);
$config = preg_replace('/SOCKET_SSL_' . $key . '_FILE=(.*)[\n]/', '', $config);
}
$config = preg_replace('/SOCKET_SSL_KEY_PASSPHRASE=(.*)[\n]/', '', $config);
}
// Remove keys not needed for sqlite
if ($input['db']['connection'] === 'sqlite') {
foreach (['host', 'database', 'username', 'password', 'port'] as $key) {
$key = strtoupper($key);
$config = preg_replace('/DB_' . $key . '=(.*)[\n]/', '', $config);
}
}
// Remove keys not needed by SMTP
if ($input['mail']['driver'] !== 'smtp') {
foreach (['host', 'port', 'username', 'password'] as $key) {
$key = strtoupper($key);
$config = preg_replace('/MAIL_' . $key . '=(.*)[\n]/', '', $config);
}
}
// Remove redis password if null
$config = preg_replace('/REDIS_PASSWORD=null[\n]/', '', $config);
// Remove github keys if not needed, only really exists on my dev copy
if (!isset($input['github']) || empty($input['github']['oauth_token'])) {
$config = preg_replace('/GITHUB_OAUTH_TOKEN=(.*)[\n]/', '', $config);
}
// Remove trusted_proxies if not set
if (!isset($input['trusted']) || !isset($input['trusted']['proxied'])) {
$config = preg_replace('/TRUSTED_PROXIES=(.*)[\n]/', '', $config);
}
// Remove comments
$config = preg_replace('/#(.*)[\n]/', '', $config);
$config = preg_replace('/[\n]{3,}/m', PHP_EOL . PHP_EOL, $config);
return $this->filesystem->put($path, trim($config) . PHP_EOL);
}
/**
* Checks for new configuration values in .env.dist and copy them to .env.
*
* @return bool
*/
public function update()
{
$prev = base_path('.env.prev');
$current = base_path('.env');
$dist = base_path('.env.dist');
$config = [];
// Read the current config values into an array for the writeEnvFile method
$content = $this->filesystem->get($current);
foreach (explode(PHP_EOL, $content) as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
$parts = explode('=', $line);
if (count($parts) < 2) {
continue;
}
$env = strtolower($parts[0]);
$value = trim($parts[1]);
$section = substr($env, 0, strpos($env, '_'));
$key = substr($env, strpos($env, '_') + 1);
$config[$section][$key] = $value;
}
// Backup the .env file, just in case it failed because we don't want to lose APP_KEY
$this->filesystem->copy($current, $prev);
// Copy the example file so that new values are copied
$this->filesystem->copy($dist, $current);
// FIXME: Not sure these 2 if statements are needed anymore
// Move the socket value to the correct key
if (isset($config['app']['socket'])) {
$config['socket']['url'] = $config['app']['socket'];
unset($config['app']['socket']);
}
if (isset($config['app']['ssl'])) {
foreach ($config['app']['ssl'] as $key => $value) {
$config['socket']['ssl_' . $key] = $value;
}
unset($config['app']['ssl']);
}
if (!$this->save($config)) {
return false;
}
// If the updated .env is the same as the backup remove the backup
if ($this->filesystem->md5($current) === $this->filesystem->md5($prev)) {
$this->filesystem->delete($prev);
}
return true;
}
}