From e387548a37f238842db6d55da70b088adb41035f Mon Sep 17 00:00:00 2001 From: denisdulici Date: Tue, 20 Feb 2018 17:05:58 +0300 Subject: [PATCH] installer refactoring --- app/Console/Commands/ConfigureApp.php | 238 ------------------ app/Console/Commands/Install.php | 232 +++++++++++++++++ app/Console/Kernel.php | 2 +- app/Http/Controllers/Install/Database.php | 30 +-- app/Http/Controllers/Install/Requirements.php | 6 +- app/Http/Controllers/Install/Settings.php | 14 +- .../{AppConfigurer.php => Installer.php} | 67 +++-- 7 files changed, 293 insertions(+), 296 deletions(-) delete mode 100644 app/Console/Commands/ConfigureApp.php create mode 100644 app/Console/Commands/Install.php rename app/Utilities/{AppConfigurer.php => Installer.php} (80%) diff --git a/app/Console/Commands/ConfigureApp.php b/app/Console/Commands/ConfigureApp.php deleted file mode 100644 index fcedb1819..000000000 --- a/app/Console/Commands/ConfigureApp.php +++ /dev/null @@ -1,238 +0,0 @@ -checkOptions(); - if ( ! empty( $missingOptions ) && $this->option( self::OPT_NO_INTERACTION ) ) { - $this->line( '❌ Some options are missing and --no-interaction is present. Please run the following command for more informations :' ); - $this->line( '❌ php artisan help app:configure' ); - $this->line( '❌ Missing options are : ' . join( ', ', $missingOptions ) ); - - return self::CMD_ERROR; - } - - $this->line( 'Setting locale ' . $this->locale ); - Session::put( self::OPT_LOCALE, $this->locale ); - - $this->prompt(); - - $this->line( 'Configuring database' ); - if ( ! $this->configureDatabase() ) { - return self::CMD_ERROR; - } - - $this->line( 'Configuring company' ); - AppConfigurer::createCompany( $this->companyName, $this->companyEmail, $this->locale ); - - $this->line( 'Creating Admin user' ); - AppConfigurer::createUser( $this->adminEmail, $this->adminPassword, $this->locale ); - - $this->line( 'Applying the final touches' ); - AppConfigurer::finalTouches(); - - return self::CMD_SUCCESS; - } - - /** - * Check that all options are presents. otherwise returns an array of the missing options - */ - private function checkOptions() { - $missingOptions = array(); - - $this->locale = $this->option( self::OPT_LOCALE ); - if ( empty( $this->locale ) ) { - $missingOptions[] = self::OPT_LOCALE; - } - - $this->dbHost = $this->option( self::OPT_DB_HOST ); - if ( empty( $this->dbHost ) ) { - $missingOptions[] = self::OPT_DB_HOST; - } - - $this->dbPort = $this->option( self::OPT_DB_PORT ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_DB_PORT; - } - - $this->dbName = $this->option( self::OPT_DB_NAME ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_DB_NAME; - } - - $this->dbUsername = $this->option( self::OPT_DB_USERNAME ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_DB_USERNAME; - } - - $this->dbPassword = $this->option( self::OPT_DB_PASSWORD ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_DB_PASSWORD; - } - - $this->companyName = $this->option( self::OPT_COMPANY_NAME ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_COMPANY_NAME; - } - - $this->companyEmail = $this->option( self::OPT_COMPANY_EMAIL ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_COMPANY_EMAIL; - } - - $this->adminEmail = $this->option( self::OPT_ADMIN_EMAIL ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_ADMIN_EMAIL; - } - - $this->adminPassword = $this->option( self::OPT_ADMIN_PASSWORD ); - if ( empty( $this->dbPort ) ) { - $missingOptions[] = self::OPT_ADMIN_PASSWORD; - } - - return $missingOptions; - } - - /** - * Ask the user for data if some options are missing. - */ - private function prompt() { - if ( empty( $this->dbHost ) ) { - $this->dbHost = $this->ask( 'What is the database host?', 'localhost' ); - } - - if ( empty( $this->dbPort ) ) { - $this->dbPort = $this->ask( 'What is the database port?', '3606' ); - } - - if ( empty( $this->dbName ) ) { - $this->dbName = $this->ask( 'What is the database name?' ); - } - - if ( empty( $this->dbUsername ) ) { - $this->dbUsername = $this->ask( 'What is the database username?' ); - } - - if ( empty( $this->dbPassword ) ) { - $this->dbPassword = $this->secret( 'What is the database password?' ); - } - - if ( empty( $this->companyName ) ) { - $this->companyName = $this->ask( 'What is the company name?' ); - } - - if ( empty( $this->companyEmail ) ) { - $this->companyEmail = $this->ask( 'What is the company contact email?' ); - } - - if ( empty( $this->adminEmail ) ) { - $this->adminEmail = $this->ask( 'What is the admin email?', $this->companyEmail ); - } - - if ( empty( $this->adminPassword ) ) { - $this->adminPassword = $this->secret( 'What is the admin password?' ); - } - } - - private function configureDatabase() { - $this->dbHost = $this->option( self::OPT_DB_HOST ); - $this->dbPort = $this->option( self::OPT_DB_PORT ); - $this->dbName = $this->option( self::OPT_DB_NAME ); - $this->dbUsername = $this->option( self::OPT_DB_USERNAME ); - $this->dbPassword = $this->option( self::OPT_DB_PASSWORD ); - - $this->line( 'Connecting to database ' . $this->dbName . '@' . $this->dbHost . ':' . $this->dbPort ); - $isDbValid = AppConfigurer::isDbValid( $this->dbHost, $this->dbPort, $this->dbName, $this->dbUsername, $this->dbPassword ); - if ( ! $isDbValid ) { - $this->error( 'Error: Could not connect to the database! Please, make sure the details are correct.' ); - - return false; - } - - AppConfigurer::saveDbVariables( $this->dbHost, $this->dbPort, $this->dbName, $this->dbUsername, $this->dbPassword ); - - // Try to increase the maximum execution time - set_time_limit( 300 ); // 5 minutes - - // Create tables - Artisan::call( 'migrate', [ '--force' => true ] ); - - // Create Roles - Artisan::call( 'db:seed', [ '--class' => 'Database\Seeds\Roles', '--force' => true ] ); - - return true; - } -} diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php new file mode 100644 index 000000000..206018ab7 --- /dev/null +++ b/app/Console/Commands/Install.php @@ -0,0 +1,232 @@ +checkOptions(); + if (!empty($missingOptions) && $this->option(self::OPT_NO_INTERACTION)) { + $this->line('❌ Some options are missing and --no-interaction is present. Please run the following command for more informations :'); + $this->line('❌ php artisan help install'); + $this->line('❌ Missing options are : ' . join(', ', $missingOptions)); + + return self::CMD_ERROR; + } + + $this->line('Setting locale ' . $this->locale); + Session::put(self::OPT_LOCALE, $this->locale); + + $this->prompt(); + + // Create the .env file + Installer::createDefaultEnvFile(); + + $this->line('Creating database tables'); + if (!$this->createDatabaseTables()) { + return self::CMD_ERROR; + } + + $this->line('Creating company'); + Installer::createCompany($this->companyName, $this->companyEmail, $this->locale); + + $this->line('Creating admin'); + Installer::createUser($this->adminEmail, $this->adminPassword, $this->locale); + + $this->line('Applying the final touches'); + Installer::finalTouches(); + + return self::CMD_SUCCESS; + } + + /** + * Check that all options are presents. otherwise returns an array of the missing options + */ + private function checkOptions() + { + $missingOptions = array(); + + $this->locale = $this->option(self::OPT_LOCALE); + if (empty($this->locale)) { + $missingOptions[] = self::OPT_LOCALE; + } + + $this->dbHost = $this->option(self::OPT_DB_HOST); + if (empty($this->dbHost)) { + $missingOptions[] = self::OPT_DB_HOST; + } + + $this->dbPort = $this->option(self::OPT_DB_PORT); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_DB_PORT; + } + + $this->dbName = $this->option(self::OPT_DB_NAME); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_DB_NAME; + } + + $this->dbUsername = $this->option(self::OPT_DB_USERNAME); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_DB_USERNAME; + } + + $this->dbPassword = $this->option(self::OPT_DB_PASSWORD); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_DB_PASSWORD; + } + + $this->companyName = $this->option(self::OPT_COMPANY_NAME); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_COMPANY_NAME; + } + + $this->companyEmail = $this->option(self::OPT_COMPANY_EMAIL); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_COMPANY_EMAIL; + } + + $this->adminEmail = $this->option(self::OPT_ADMIN_EMAIL); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_ADMIN_EMAIL; + } + + $this->adminPassword = $this->option(self::OPT_ADMIN_PASSWORD); + if (empty($this->dbPort)) { + $missingOptions[] = self::OPT_ADMIN_PASSWORD; + } + + return $missingOptions; + } + + /** + * Ask the user for data if some options are missing. + */ + private function prompt() + { + if (empty($this->dbHost)) { + $this->dbHost = $this->ask('What is the database host?', 'localhost'); + } + + if (empty($this->dbPort)) { + $this->dbPort = $this->ask('What is the database port?', '3606'); + } + + if (empty($this->dbName)) { + $this->dbName = $this->ask('What is the database name?'); + } + + if (empty($this->dbUsername)) { + $this->dbUsername = $this->ask('What is the database username?'); + } + + if (empty($this->dbPassword)) { + $this->dbPassword = $this->secret('What is the database password?'); + } + + if (empty($this->companyName)) { + $this->companyName = $this->ask('What is the company name?'); + } + + if (empty($this->companyEmail)) { + $this->companyEmail = $this->ask('What is the company contact email?'); + } + + if (empty($this->adminEmail)) { + $this->adminEmail = $this->ask('What is the admin email?', $this->companyEmail); + } + + if (empty($this->adminPassword)) { + $this->adminPassword = $this->secret('What is the admin password?'); + } + } + + private function createDatabaseTables() { + $this->dbHost = $this->option(self::OPT_DB_HOST); + $this->dbPort = $this->option(self::OPT_DB_PORT); + $this->dbName = $this->option(self::OPT_DB_NAME); + $this->dbUsername = $this->option(self::OPT_DB_USERNAME); + $this->dbPassword = $this->option(self::OPT_DB_PASSWORD); + + $this->line('Connecting to database ' . $this->dbName . '@' . $this->dbHost . ':' . $this->dbPort); + + if (!Installer::createDbTables($this->dbHost, $this->dbPort, $this->dbName, $this->dbUsername, $this->dbPassword)) { + $this->error('Error: Could not connect to the database! Please, make sure the details are correct.'); + + return false; + } + + return true; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 3e78423d2..2698c72d2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,9 +15,9 @@ class Kernel extends ConsoleKernel protected $commands = [ Commands\CompanySeed::class, Commands\BillReminder::class, + Commands\Install::class, Commands\InvoiceReminder::class, Commands\ModuleInstall::class, - Commands\ConfigureApp::class, ]; /** diff --git a/app/Http/Controllers/Install/Database.php b/app/Http/Controllers/Install/Database.php index bcaf03b9e..40afcae21 100644 --- a/app/Http/Controllers/Install/Database.php +++ b/app/Http/Controllers/Install/Database.php @@ -4,16 +4,18 @@ namespace App\Http\Controllers\Install; use Artisan; use App\Http\Requests\Install\Database as Request; -use App\Utilities\AppConfigurer; +use App\Utilities\Installer; use Illuminate\Routing\Controller; -class Database extends Controller { +class Database extends Controller +{ /** * Show the form for creating a new resource. * * @return Response */ - public function create() { + public function create() + { return view( 'install.database.create' ); } @@ -24,7 +26,8 @@ class Database extends Controller { * * @return Response */ - public function store( Request $request ) { + public function store(Request $request) + { $host = $request['hostname']; $port = env( 'DB_PORT', '3306' ); $database = $request['database']; @@ -32,27 +35,14 @@ class Database extends Controller { $password = $request['password']; // Check database connection - if ( ! AppConfigurer::isDbValid($host,$port,$database,$username,$password) ) { - $message = trans( 'install.error.connection' ); + if (!Installer::createDbTables($host, $port, $database, $username, $password)) { + $message = trans('install.error.connection'); flash( $message )->error()->important(); return redirect( 'install/database' )->withInput(); } - // Set database details - AppConfigurer::saveDbVariables($host, $port, $database, $username, $password); - - // Try to increase the maximum execution time - set_time_limit( 300 ); // 5 minutes - - // Create tables - Artisan::call( 'migrate', [ '--force' => true ] ); - - // Create Roles - Artisan::call( 'db:seed', [ '--class' => 'Database\Seeds\Roles', '--force' => true ] ); - - return redirect( 'install/settings' ); + return redirect('install/settings' ); } - } diff --git a/app/Http/Controllers/Install/Requirements.php b/app/Http/Controllers/Install/Requirements.php index a09bfe706..064f18b4f 100644 --- a/app/Http/Controllers/Install/Requirements.php +++ b/app/Http/Controllers/Install/Requirements.php @@ -2,8 +2,8 @@ namespace App\Http\Controllers\Install; +use App\Utilities\Installer; use File; -use App\Utilities\AppConfigurer; use Illuminate\Routing\Controller; class Requirements extends Controller @@ -16,12 +16,12 @@ class Requirements extends Controller public function show() { // Check requirements - $requirements = AppConfigurer::checkServerRequirements(); + $requirements = Installer::checkServerRequirements(); if (empty($requirements)) { // Create the .env file if (!File::exists(base_path('.env'))) { - AppConfigurer::createDefaultEnvFile(); + Installer::createDefaultEnvFile(); } redirect('install/language')->send(); diff --git a/app/Http/Controllers/Install/Settings.php b/app/Http/Controllers/Install/Settings.php index 80f80e4ae..416256256 100644 --- a/app/Http/Controllers/Install/Settings.php +++ b/app/Http/Controllers/Install/Settings.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers\Install; use App\Http\Requests\Install\Setting as Request; -use App\Utilities\AppConfigurer; +use App\Utilities\Installer; use Illuminate\Routing\Controller; class Settings extends Controller @@ -28,19 +28,13 @@ class Settings extends Controller public function store(Request $request) { // Create company - $companyName = $request['company_name']; - $companyEmail= $request['company_email']; - $locale= session('locale'); - AppConfigurer::createCompany($companyName, $companyEmail, $locale); + Installer::createCompany($request->get('company_name'), $request->get('company_email'), session('locale')); // Create user - $adminEmail = $request['user_email']; - $adminPassword = $request['user_password']; - $locale= session('locale'); - AppConfigurer::createUser($adminEmail, $adminPassword, $locale); + Installer::createUser($request->get('user_email'), $request->get('user_password'), session('locale')); // Make the final touches - AppConfigurer::finalTouches(); + Installer::finalTouches(); // Redirect to dashboard return redirect('auth/login'); diff --git a/app/Utilities/AppConfigurer.php b/app/Utilities/Installer.php similarity index 80% rename from app/Utilities/AppConfigurer.php rename to app/Utilities/Installer.php index c3b9d279b..7428c93db 100644 --- a/app/Utilities/AppConfigurer.php +++ b/app/Utilities/Installer.php @@ -1,28 +1,23 @@ 'base64:'.base64_encode(random_bytes(32)), 'APP_URL' => url('/'), ]); } + public static function createDbTables($host, $port, $database, $username, $password) + { + if (!static::isDbValid($host, $port, $database, $username, $password)) { + return false; + } + + // Set database details + static::saveDbVariables($host, $port, $database, $username, $password); + + // Try to increase the maximum execution time + set_time_limit(300); // 5 minutes + + // Create tables + Artisan::call('migrate', ['--force' => true]); + + // Create Roles + Artisan::call('db:seed', ['--class' => 'Database\Seeds\Roles', '--force' => true]); + + return true; + } + /** * Check if the database exists and is accessible. * @@ -121,7 +139,8 @@ class AppConfigurer { * * @return bool */ - public static function isDbValid($host, $port, $database, $username, $password){ + public static function isDbValid($host, $port, $database, $username, $password) + { Config::set('database.connections.install_test', [ 'host' => $host, 'port' => $port, @@ -149,7 +168,7 @@ class AppConfigurer { $prefix = strtolower(str_random(3) . '_'); // Update .env file - self::updateEnv([ + static::updateEnv([ 'DB_HOST' => $host, 'DB_PORT' => $port, 'DB_DATABASE' => $database, @@ -175,7 +194,7 @@ class AppConfigurer { DB::reconnect($con); } - public static function createCompany($companyName, $companyEmail, $locale) + public static function createCompany($name, $email, $locale) { // Create company $company = Company::create([ @@ -183,14 +202,14 @@ class AppConfigurer { ]); // Set settings - Setting::set([ - 'general.company_name' => $companyName, - 'general.company_email' => $companyEmail, + setting()->set([ + 'general.company_name' => $name, + 'general.company_email' => $email, 'general.default_currency' => 'USD', 'general.default_locale' => $locale, ]); - Setting::setExtraColumns(['company_id' => $company->id]); - Setting::save(); + setting()->setExtraColumns(['company_id' => $company->id]); + setting()->save(); } public static function createUser($email, $password, $locale) @@ -213,7 +232,7 @@ class AppConfigurer { public static function finalTouches() { // Update .env file - self::updateEnv([ + static::updateEnv([ 'APP_LOCALE' => session('locale'), 'APP_INSTALLED' => 'true', 'APP_DEBUG' => 'false', @@ -229,7 +248,7 @@ class AppConfigurer { public static function updateEnv($data) { - if (empty($data) || !is_array($data)) { + if (empty($data) || !is_array($data) || !is_file(base_path('.env'))) { return false; }