1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

update variables for .env instead of writing completely new file

This commit is contained in:
Benjamin Beganović 2020-11-24 11:45:27 +01:00
parent 3b765d32c1
commit 242d48cd1a
3 changed files with 90 additions and 72 deletions

View File

@ -61,8 +61,6 @@ class SetupController extends Controller
return response('Oops, something went wrong. Check your logs.'); /* We should never reach this block, but just in case. */
}
return $request->all();
$mail_driver = $request->input('mail_driver');
if (! $this->failsafeMailCheck($request)) {
@ -71,47 +69,37 @@ class SetupController extends Controller
$url = $request->input('url');
if (substr($url, -1) != '/')
if (substr($url, -1) != '/') {
$url = $url . '/';
}
$_ENV['APP_KEY'] = config('app.key');
$_ENV['APP_URL'] = $url;
$_ENV['APP_DEBUG'] = $request->input('debug') ? 'true' : 'false';
$_ENV['REQUIRE_HTTPS'] = $request->input('https') ? 'true' : 'false';
$_ENV['DB_TYPE'] = 'mysql';
$_ENV['DB_HOST1'] = $request->input('host');
$_ENV['DB_DATABASE1'] = $request->input('database');
$_ENV['DB_USERNAME1'] = $request->input('db_username');
$_ENV['DB_PASSWORD1'] = $request->input('db_password');
$_ENV['MAIL_MAILER'] = $mail_driver;
$_ENV['MAIL_PORT'] = $request->input('mail_port');
$_ENV['MAIL_ENCRYPTION'] = $request->input('encryption');
$_ENV['MAIL_HOST'] = $request->input('mail_host');
$_ENV['MAIL_USERNAME'] = $request->input('mail_username');
$_ENV['MAIL_FROM_NAME'] = $request->input('mail_name');
$_ENV['MAIL_FROM_ADDRESS'] = $request->input('mail_address');
$_ENV['MAIL_PASSWORD'] = $request->input('mail_password');
$_ENV['NINJA_ENVIRONMENT'] = 'selfhost';
$_ENV['DB_CONNECTION'] = 'db-ninja-01';
$env_values = [
'APP_URL' => $url,
'REQUIRE_HTTPS' => $request->input('https') ? 'true' : 'false',
'APP_DEBUG' => $request->input('debug') ? 'true' : 'false',
$config = '';
'DB_HOST1' => $request->input('host'),
'DB_DATABASE1' => $request->input('database'),
'DB_USERNAME1' => $request->input('db_username'),
'DB_PASSWORD1' => $request->input('db_password'),
'MAIL_MAILER' => $mail_driver,
'MAIL_PORT' => $request->input('mail_port'),
'MAIL_ENCRYPTION' => $request->input('encryption'),
'MAIL_HOST' => $request->input('mail_host'),
'MAIL_USERNAME' => $request->input('mail_username'),
'MAIL_FROM_NAME' => $request->input('mail_name'),
'MAIL_FROM_ADDRESS' => $request->input('mail_address'),
'MAIL_PASSWORD' => $request->input('mail_password'),
'NINJA_ENVIRONMENT' => 'selfhost',
'DB_CONNECTION' => 'db-ninja-01',
];
try {
foreach ($_ENV as $key => $val) {
if (is_array($val)) {
continue;
foreach ($env_values as $property => $value) {
$this->updateEnvironmentProperty($property, $value);
}
if (preg_match('/\s/', $val)) {
$val = "'{$val}'";
}
$config .= "{$key}={$val}\n";
}
/* Write the .env file */
$filePath = base_path() . '/.env';
$fp = fopen($filePath, 'w');
fwrite($fp, $config);
fclose($fp);
/* We need this in some environments that do not have STDIN defined */
define('STDIN', fopen('php://stdin', 'r'));

View File

@ -1,4 +1,5 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
@ -16,6 +17,7 @@ use App\Utils\Ninja;
use App\Utils\SystemHealth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
trait AppSetup
{
@ -112,4 +114,32 @@ trait AppSetup
Cache::forever($name, $data);
}
private function updateEnvironmentProperty(string $property, $value): void
{
$env = file(base_path('.env'));
$position = null;
foreach ((array) $env as $key => $variable) {
if (Str::startsWith($variable, $property)) {
$position = $key;
}
}
// This should never happen, but this is login just in case.
// Variables that will be replaced (updated with different content) should already be in .env file.
if (is_null($position)) {
$env[] = "{$property}=" . $value . "\n";
} else {
$env[$position] = "{$property}=" . $value . "\n";
}
try {
file_put_contents(base_path('.env'), $env);
} catch (\Exception $e) {
info($e->getMessage());
}
}
}

View File

@ -3227,7 +3227,7 @@ return [
'bank_account_not_linked' => 'To pay with bank account, first you have to add it as payment method.',
'application_settings_label' => 'Let\'s store basic information about your Invoice Ninja!',
'recommended_in_production' => 'Recommended in production',
'recommended_in_production' => 'Highly recommended in production',
'enable_only_for_development' => 'Enable only for development',
'test_pdf' => 'Test PDF',