From a654fac21b194db7d0d1bbba15d0d4509b552d82 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Thu, 31 Dec 2020 11:46:04 +0530 Subject: [PATCH] Runtime Handled --- .../Core/src/Console/Commands/Install.php | 96 ++++++++++++++----- 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/packages/Webkul/Core/src/Console/Commands/Install.php b/packages/Webkul/Core/src/Console/Commands/Install.php index fa532e21f..b9c480389 100644 --- a/packages/Webkul/Core/src/Console/Commands/Install.php +++ b/packages/Webkul/Core/src/Console/Commands/Install.php @@ -3,8 +3,8 @@ namespace Webkul\Core\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; -use Illuminate\Support\Facades\Artisan; class Install extends Command { @@ -33,20 +33,15 @@ class Install extends Command } /** - * Install and configure bagisto + * Install and configure bagisto. */ public function handle() { + // check for .env $this->checkForEnvFile(); - // cached new changes - $this->warn('Step: Caching new changes...'); - $cached = $this->call('config:cache'); - $this->info($cached); - - // waiting for 2 seconds - $this->warn('Please wait...'); - sleep(2); + // loading values at runtime + $this->loadEnvConfigAtRuntime(); // running `php artisan migrate` $this->warn('Step: Migrating all tables into database...'); @@ -54,25 +49,31 @@ class Install extends Command $this->info($migrate); // running `php artisan db:seed` - $this->warn('Step: seeding basic data for bagisto kickstart...'); + $this->warn('Step: Seeding basic data for Bagisto kickstart...'); $result = $this->call('db:seed'); $this->info($result); // running `php artisan vendor:publish --all` - $this->warn('Step: Publishing Assets and Configurations...'); + $this->warn('Step: Publishing assets and configurations...'); $result = $this->call('vendor:publish', ['--all']); $this->info($result); // running `php artisan storage:link` - $this->warn('Step: Linking Storage directory...'); + $this->warn('Step: Linking storage directory...'); $result = $this->call('storage:link'); $this->info($result); + // optimizing stuffs + $this->warn('Step: Optimizing...'); + $result = $this->call('optimize'); + $this->info($result); + // running `composer dump-autoload` - $this->warn('Step: Composer Autoload...'); + $this->warn('Step: Composer autoload...'); $result = shell_exec('composer dump-autoload'); $this->info($result); + // final information $this->info('-----------------------------'); $this->info('Congratulations!'); $this->info('The installation has been finished and you can now use Bagisto.'); @@ -85,28 +86,30 @@ class Install extends Command /** * Checking .env file and if not found then create .env file. * Then ask for database name, password & username to set - * On .env file so that we can easily migrate to our db + * On .env file so that we can easily migrate to our db. */ - public function checkForEnvFile() + protected function checkForEnvFile() { $envExists = File::exists(base_path() . '/.env'); + if (! $envExists) { $this->info('Creating the environment configuration file.'); $this->createEnvFile(); } else { - $this->call('key:generate'); $this->info('Great! your environment configuration file already exists.'); } + + $this->call('key:generate'); } /** * Create a new .env file. */ - public function createEnvFile() + protected function createEnvFile() { try { File::copy('.env.example', '.env'); - Artisan::call('key:generate'); + $default_app_url = 'http://localhost:8000'; $input_app_url = $this->ask('Please Enter the APP URL : '); $this->envUpdate('APP_URL=', $input_app_url ? $input_app_url : $default_app_url ); @@ -125,7 +128,6 @@ class Install extends Command $currency = $this->choice('Please enter the default currency', ['USD', 'EUR'], 'USD'); $this->envUpdate('APP_CURRENCY=', $currency); - $this->addDatabaseDetails(); } catch (\Exception $e) { $this->error('Error in creating .env file, please create it manually and then run `php artisan migrate` again.'); @@ -135,21 +137,65 @@ class Install extends Command /** * Add the database credentials to the .env file. */ - public function addDatabaseDetails() + protected function addDatabaseDetails() { $dbName = $this->ask('What is the database name to be used by bagisto?'); - $dbUser = $this->anticipate('What is your database username?', ['root']); - $dbPass = $this->secret('What is your database password?'); - $this->envUpdate('DB_DATABASE=', $dbName); + + $dbUser = $this->anticipate('What is your database username?', ['root']); $this->envUpdate('DB_USERNAME=', $dbUser); + + $dbPass = $this->secret('What is your database password?'); $this->envUpdate('DB_PASSWORD=', $dbPass); } + /** + * Load `.env` config at runtime. + */ + protected function loadEnvConfigAtRuntime() + { + $this->warn('Loading configs...'); + + /* environment directly checked from `.env` so changing in config won't reflect */ + app()['env'] = $this->getEnvAtRuntime('APP_ENV'); + + /* setting for the first time and then `.env` values will be incharged */ + config(['database.connections.mysql.database' => $this->getEnvAtRuntime('DB_DATABASE')]); + config(['database.connections.mysql.username' => $this->getEnvAtRuntime('DB_USERNAME')]); + config(['database.connections.mysql.password' => $this->getEnvAtRuntime('DB_PASSWORD')]); + DB::purge('mysql'); + + $this->info('Configuration loaded..'); + } + + /** + * Check key in `.env` file because it will help to find values at runtime. + */ + protected static function getEnvAtRuntime($key) + { + $path = base_path() . '/.env'; + $data = file($path); + + if ($data) { + foreach ($data as $line) { + $line = preg_replace('/\s+/', '', $line); + $rowValues = explode('=', $line); + + if (strlen($line) !== 0) { + if (strpos($key, $rowValues[0]) !== false) { + return $rowValues[1]; + } + } + } + } + + return false; + } + /** * Update the .env values. */ - public static function envUpdate($key, $value) + protected static function envUpdate($key, $value) { $path = base_path() . '/.env'; $data = file($path);