protected function updateConfiguration()
{
$config = [];
// Read the current config values into an array for the writeEnvFile method
foreach (file(base_path('.env')) 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;
}
// JWT secret needs to be generated if it is not already set
if (!isset($config['jwt']) || $config['jwt']['secret'] === 'changeme') {
$config['jwt']['secret'] = $this->generateJWTKey();
}
// Backup the .env file, just in case it failed because we don't want to lose APP_KEY
copy(base_path('.env'), base_path('.env.prev'));
// Copy the example file so that new values are copied
copy(base_path('.env.example'), base_path('.env'));
// Write the file to disk
$this->writeEnvFile($config);
// If the updated .env is the same as the backup remove the backup
if (md5_file(base_path('.env')) === md5_file(base_path('.env.prev'))) {
unlink(base_path('.env.prev'));
}
}