From 242d48cd1a10749e330cc07a863c049cff76c9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 24 Nov 2020 11:45:27 +0100 Subject: [PATCH] update variables for .env instead of writing completely new file --- app/Http/Controllers/SetupController.php | 62 ++++++--------- app/Utils/Traits/AppSetup.php | 98 ++++++++++++++++-------- resources/lang/en/texts.php | 2 +- 3 files changed, 90 insertions(+), 72 deletions(-) diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 0747884464..31258b594c 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -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,48 +69,38 @@ class SetupController extends Controller $url = $request->input('url'); - if (substr($url, -1) != '/') + if (substr($url, -1) != '/') { $url = $url . '/'; + } + + $env_values = [ + 'APP_URL' => $url, + 'REQUIRE_HTTPS' => $request->input('https') ? 'true' : 'false', + 'APP_DEBUG' => $request->input('debug') ? 'true' : 'false', - $_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'; + 'DB_HOST1' => $request->input('host'), + 'DB_DATABASE1' => $request->input('database'), + 'DB_USERNAME1' => $request->input('db_username'), + 'DB_PASSWORD1' => $request->input('db_password'), - $config = ''; + '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; - } - if (preg_match('/\s/', $val)) { - $val = "'{$val}'"; - } - $config .= "{$key}={$val}\n"; + foreach ($env_values as $property => $value) { + $this->updateEnvironmentProperty($property, $value); } - /* 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')); diff --git a/app/Utils/Traits/AppSetup.php b/app/Utils/Traits/AppSetup.php index cd8560b550..280d2a9453 100644 --- a/app/Utils/Traits/AppSetup.php +++ b/app/Utils/Traits/AppSetup.php @@ -1,4 +1,5 @@ $class) { - if (request()->has('clear_cache') || ! Cache::has($name) || $force) { + if (request()->has('clear_cache') || !Cache::has($name) || $force) { // check that the table exists in case the migration is pending - if (! Schema::hasTable((new $class())->getTable())) { + if (!Schema::hasTable((new $class())->getTable())) { continue; } if ($name == 'payment_terms') { @@ -58,7 +60,7 @@ trait AppSetup } /*Build template cache*/ - if (request()->has('clear_cache') || ! Cache::has('templates')) + if (request()->has('clear_cache') || !Cache::has('templates')) $this->buildTemplates(); } @@ -67,49 +69,77 @@ trait AppSetup { $data = [ - 'invoice' => [ - 'subject' => EmailTemplateDefaults::emailInvoiceSubject(), - 'body' => EmailTemplateDefaults::emailInvoiceTemplate(), + 'invoice' => [ + 'subject' => EmailTemplateDefaults::emailInvoiceSubject(), + 'body' => EmailTemplateDefaults::emailInvoiceTemplate(), ], - - 'quote' => [ - 'subject' => EmailTemplateDefaults::emailQuoteSubject(), - 'body' => EmailTemplateDefaults::emailQuoteTemplate(), + + 'quote' => [ + 'subject' => EmailTemplateDefaults::emailQuoteSubject(), + 'body' => EmailTemplateDefaults::emailQuoteTemplate(), ], - 'payment' => [ - 'subject' => EmailTemplateDefaults::emailPaymentSubject(), - 'body' => EmailTemplateDefaults::emailPaymentTemplate(), + 'payment' => [ + 'subject' => EmailTemplateDefaults::emailPaymentSubject(), + 'body' => EmailTemplateDefaults::emailPaymentTemplate(), ], - 'payment_partial' => [ - 'subject' => EmailTemplateDefaults::emailPaymentPartialSubject(), - 'body' => EmailTemplateDefaults::emailPaymentPartialTemplate(), + 'payment_partial' => [ + 'subject' => EmailTemplateDefaults::emailPaymentPartialSubject(), + 'body' => EmailTemplateDefaults::emailPaymentPartialTemplate(), ], - 'reminder1' => [ - 'subject' => EmailTemplateDefaults::emailReminder1Subject(), - 'body' => EmailTemplateDefaults::emailReminder1Template(), + 'reminder1' => [ + 'subject' => EmailTemplateDefaults::emailReminder1Subject(), + 'body' => EmailTemplateDefaults::emailReminder1Template(), ], - 'reminder2' => [ - 'subject' => EmailTemplateDefaults::emailReminder2Subject(), - 'body' => EmailTemplateDefaults::emailReminder2Template(), + 'reminder2' => [ + 'subject' => EmailTemplateDefaults::emailReminder2Subject(), + 'body' => EmailTemplateDefaults::emailReminder2Template(), ], - 'reminder3' => [ - 'subject' => EmailTemplateDefaults::emailReminder3Subject(), - 'body' => EmailTemplateDefaults::emailReminder3Template(), + 'reminder3' => [ + 'subject' => EmailTemplateDefaults::emailReminder3Subject(), + 'body' => EmailTemplateDefaults::emailReminder3Template(), ], - 'reminder_endless' => [ - 'subject' => EmailTemplateDefaults::emailReminderEndlessSubject(), - 'body' => EmailTemplateDefaults::emailReminderEndlessTemplate(), + 'reminder_endless' => [ + 'subject' => EmailTemplateDefaults::emailReminderEndlessSubject(), + 'body' => EmailTemplateDefaults::emailReminderEndlessTemplate(), ], - 'statement' => [ - 'subject' => EmailTemplateDefaults::emailStatementSubject(), - 'body' => EmailTemplateDefaults::emailStatementTemplate(), + 'statement' => [ + 'subject' => EmailTemplateDefaults::emailStatementSubject(), + 'body' => EmailTemplateDefaults::emailStatementTemplate(), ], - 'credit' => [ - 'subject' => EmailTemplateDefaults::emailCreditSubject(), - 'body' => EmailTemplateDefaults::emailCreditTemplate(), + 'credit' => [ + 'subject' => EmailTemplateDefaults::emailCreditSubject(), + 'body' => EmailTemplateDefaults::emailCreditTemplate(), ], ]; 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()); + } + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index bcf7aff5f9..c5d0a8e792 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -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',