fixed spaces

This commit is contained in:
denisdulici 2018-02-20 17:17:39 +03:00
parent e387548a37
commit 9aab7d6e1c
3 changed files with 275 additions and 275 deletions

View File

@ -8,21 +8,21 @@ use Illuminate\Console\Command;
class Install extends Command class Install extends Command
{ {
const CMD_SUCCESS = 0; const CMD_SUCCESS = 0;
const CMD_ERROR = 1; const CMD_ERROR = 1;
const OPT_DB_HOST = 'db-host'; const OPT_DB_HOST = 'db-host';
const OPT_DB_PORT = 'db-port'; const OPT_DB_PORT = 'db-port';
const OPT_DB_NAME = 'db-name'; const OPT_DB_NAME = 'db-name';
const OPT_DB_USERNAME = 'db-username'; const OPT_DB_USERNAME = 'db-username';
const OPT_DB_PASSWORD = 'db-password'; const OPT_DB_PASSWORD = 'db-password';
const OPT_COMPANY_NAME = 'company-name'; const OPT_COMPANY_NAME = 'company-name';
const OPT_COMPANY_EMAIL = 'company-email'; const OPT_COMPANY_EMAIL = 'company-email';
const OPT_ADMIN_EMAIL = 'admin-email'; const OPT_ADMIN_EMAIL = 'admin-email';
const OPT_ADMIN_PASSWORD = 'admin-password'; const OPT_ADMIN_PASSWORD = 'admin-password';
const OPT_LOCALE = 'locale'; const OPT_LOCALE = 'locale';
const OPT_NO_INTERACTION = 'no-interaction'; const OPT_NO_INTERACTION = 'no-interaction';
public $dbHost; public $dbHost;
public $dbPort; public $dbPort;
public $dbName; public $dbName;
public $dbUsername; public $dbUsername;
@ -36,12 +36,12 @@ class Install extends Command
public $locale; public $locale;
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'install protected $signature = 'install
{--db-host= : Database host} {--db-host= : Database host}
{--db-port=3306 : Port of the database host} {--db-port=3306 : Port of the database host}
{--db-name= : Name of the database} {--db-name= : Name of the database}
@ -54,179 +54,179 @@ class Install extends Command
{--locale=en-GB : Language used in the app} {--locale=en-GB : Language used in the app}
'; ';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Allows to install Akaunting directly through CLI'; protected $description = 'Allows to install Akaunting directly through CLI';
/** /**
* Create a new command instance. * Create a new command instance.
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
} }
/** /**
* Execute the console command. * Execute the console command.
* *
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$missingOptions = $this->checkOptions(); $missingOptions = $this->checkOptions();
if (!empty($missingOptions) && $this->option(self::OPT_NO_INTERACTION)) { 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('❌ 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('❌ php artisan help install');
$this->line('❌ Missing options are : ' . join(', ', $missingOptions)); $this->line('❌ Missing options are : ' . join(', ', $missingOptions));
return self::CMD_ERROR; return self::CMD_ERROR;
} }
$this->line('Setting locale ' . $this->locale); $this->line('Setting locale ' . $this->locale);
Session::put(self::OPT_LOCALE, $this->locale); Session::put(self::OPT_LOCALE, $this->locale);
$this->prompt(); $this->prompt();
// Create the .env file // Create the .env file
Installer::createDefaultEnvFile(); Installer::createDefaultEnvFile();
$this->line('Creating database tables'); $this->line('Creating database tables');
if (!$this->createDatabaseTables()) { if (!$this->createDatabaseTables()) {
return self::CMD_ERROR; return self::CMD_ERROR;
} }
$this->line('Creating company'); $this->line('Creating company');
Installer::createCompany($this->companyName, $this->companyEmail, $this->locale); Installer::createCompany($this->companyName, $this->companyEmail, $this->locale);
$this->line('Creating admin'); $this->line('Creating admin');
Installer::createUser($this->adminEmail, $this->adminPassword, $this->locale); Installer::createUser($this->adminEmail, $this->adminPassword, $this->locale);
$this->line('Applying the final touches'); $this->line('Applying the final touches');
Installer::finalTouches(); Installer::finalTouches();
return self::CMD_SUCCESS; return self::CMD_SUCCESS;
} }
/** /**
* Check that all options are presents. otherwise returns an array of the missing options * Check that all options are presents. otherwise returns an array of the missing options
*/ */
private function checkOptions() private function checkOptions()
{ {
$missingOptions = array(); $missingOptions = array();
$this->locale = $this->option(self::OPT_LOCALE); $this->locale = $this->option(self::OPT_LOCALE);
if (empty($this->locale)) { if (empty($this->locale)) {
$missingOptions[] = self::OPT_LOCALE; $missingOptions[] = self::OPT_LOCALE;
} }
$this->dbHost = $this->option(self::OPT_DB_HOST); $this->dbHost = $this->option(self::OPT_DB_HOST);
if (empty($this->dbHost)) { if (empty($this->dbHost)) {
$missingOptions[] = self::OPT_DB_HOST; $missingOptions[] = self::OPT_DB_HOST;
} }
$this->dbPort = $this->option(self::OPT_DB_PORT); $this->dbPort = $this->option(self::OPT_DB_PORT);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_DB_PORT; $missingOptions[] = self::OPT_DB_PORT;
} }
$this->dbName = $this->option(self::OPT_DB_NAME); $this->dbName = $this->option(self::OPT_DB_NAME);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_DB_NAME; $missingOptions[] = self::OPT_DB_NAME;
} }
$this->dbUsername = $this->option(self::OPT_DB_USERNAME); $this->dbUsername = $this->option(self::OPT_DB_USERNAME);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_DB_USERNAME; $missingOptions[] = self::OPT_DB_USERNAME;
} }
$this->dbPassword = $this->option(self::OPT_DB_PASSWORD); $this->dbPassword = $this->option(self::OPT_DB_PASSWORD);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_DB_PASSWORD; $missingOptions[] = self::OPT_DB_PASSWORD;
} }
$this->companyName = $this->option(self::OPT_COMPANY_NAME); $this->companyName = $this->option(self::OPT_COMPANY_NAME);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_COMPANY_NAME; $missingOptions[] = self::OPT_COMPANY_NAME;
} }
$this->companyEmail = $this->option(self::OPT_COMPANY_EMAIL); $this->companyEmail = $this->option(self::OPT_COMPANY_EMAIL);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_COMPANY_EMAIL; $missingOptions[] = self::OPT_COMPANY_EMAIL;
} }
$this->adminEmail = $this->option(self::OPT_ADMIN_EMAIL); $this->adminEmail = $this->option(self::OPT_ADMIN_EMAIL);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_ADMIN_EMAIL; $missingOptions[] = self::OPT_ADMIN_EMAIL;
} }
$this->adminPassword = $this->option(self::OPT_ADMIN_PASSWORD); $this->adminPassword = $this->option(self::OPT_ADMIN_PASSWORD);
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_ADMIN_PASSWORD; $missingOptions[] = self::OPT_ADMIN_PASSWORD;
} }
return $missingOptions; return $missingOptions;
} }
/** /**
* Ask the user for data if some options are missing. * Ask the user for data if some options are missing.
*/ */
private function prompt() private function prompt()
{ {
if (empty($this->dbHost)) { if (empty($this->dbHost)) {
$this->dbHost = $this->ask('What is the database host?', 'localhost'); $this->dbHost = $this->ask('What is the database host?', 'localhost');
} }
if (empty($this->dbPort)) { if (empty($this->dbPort)) {
$this->dbPort = $this->ask('What is the database port?', '3606'); $this->dbPort = $this->ask('What is the database port?', '3606');
} }
if (empty($this->dbName)) { if (empty($this->dbName)) {
$this->dbName = $this->ask('What is the database name?'); $this->dbName = $this->ask('What is the database name?');
} }
if (empty($this->dbUsername)) { if (empty($this->dbUsername)) {
$this->dbUsername = $this->ask('What is the database username?'); $this->dbUsername = $this->ask('What is the database username?');
} }
if (empty($this->dbPassword)) { if (empty($this->dbPassword)) {
$this->dbPassword = $this->secret('What is the database password?'); $this->dbPassword = $this->secret('What is the database password?');
} }
if (empty($this->companyName)) { if (empty($this->companyName)) {
$this->companyName = $this->ask('What is the company name?'); $this->companyName = $this->ask('What is the company name?');
} }
if (empty($this->companyEmail)) { if (empty($this->companyEmail)) {
$this->companyEmail = $this->ask('What is the company contact email?'); $this->companyEmail = $this->ask('What is the company contact email?');
} }
if (empty($this->adminEmail)) { if (empty($this->adminEmail)) {
$this->adminEmail = $this->ask('What is the admin email?', $this->companyEmail); $this->adminEmail = $this->ask('What is the admin email?', $this->companyEmail);
} }
if (empty($this->adminPassword)) { if (empty($this->adminPassword)) {
$this->adminPassword = $this->secret('What is the admin password?'); $this->adminPassword = $this->secret('What is the admin password?');
} }
} }
private function createDatabaseTables() { private function createDatabaseTables() {
$this->dbHost = $this->option(self::OPT_DB_HOST); $this->dbHost = $this->option(self::OPT_DB_HOST);
$this->dbPort = $this->option(self::OPT_DB_PORT); $this->dbPort = $this->option(self::OPT_DB_PORT);
$this->dbName = $this->option(self::OPT_DB_NAME); $this->dbName = $this->option(self::OPT_DB_NAME);
$this->dbUsername = $this->option(self::OPT_DB_USERNAME); $this->dbUsername = $this->option(self::OPT_DB_USERNAME);
$this->dbPassword = $this->option(self::OPT_DB_PASSWORD); $this->dbPassword = $this->option(self::OPT_DB_PASSWORD);
$this->line('Connecting to database ' . $this->dbName . '@' . $this->dbHost . ':' . $this->dbPort); $this->line('Connecting to database ' . $this->dbName . '@' . $this->dbHost . ':' . $this->dbPort);
if (!Installer::createDbTables($this->dbHost, $this->dbPort, $this->dbName, $this->dbUsername, $this->dbPassword)) { 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.'); $this->error('Error: Could not connect to the database! Please, make sure the details are correct.');
return false; return false;
} }
return true; return true;
} }
} }

View File

@ -9,40 +9,40 @@ use Illuminate\Routing\Controller;
class Database extends Controller class Database extends Controller
{ {
/** /**
* Show the form for creating a new resource. * Show the form for creating a new resource.
* *
* @return Response * @return Response
*/ */
public function create() public function create()
{ {
return view( 'install.database.create' ); return view( 'install.database.create' );
} }
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
* *
* @param Request $request * @param Request $request
* *
* @return Response * @return Response
*/ */
public function store(Request $request) public function store(Request $request)
{ {
$host = $request['hostname']; $host = $request['hostname'];
$port = env( 'DB_PORT', '3306' ); $port = env( 'DB_PORT', '3306' );
$database = $request['database']; $database = $request['database'];
$username = $request['username']; $username = $request['username'];
$password = $request['password']; $password = $request['password'];
// Check database connection // Check database connection
if (!Installer::createDbTables($host, $port, $database, $username, $password)) { if (!Installer::createDbTables($host, $port, $database, $username, $password)) {
$message = trans('install.error.connection'); $message = trans('install.error.connection');
flash( $message )->error()->important(); flash( $message )->error()->important();
return redirect( 'install/database' )->withInput(); return redirect( 'install/database' )->withInput();
} }
return redirect('install/settings' ); return redirect('install/settings' );
} }
} }

View File

@ -86,26 +86,26 @@ class Installer
return $requirements; return $requirements;
} }
/** /**
* Create a default .env file. * Create a default .env file.
* *
* @return void * @return void
*/ */
public static function createDefaultEnvFile() public static function createDefaultEnvFile()
{ {
// Rename file // Rename file
if (is_file(base_path('.env.example'))) { if (is_file(base_path('.env.example'))) {
File::move(base_path('.env.example'), base_path('.env')); File::move(base_path('.env.example'), base_path('.env'));
} }
// Update .env file // Update .env file
static::updateEnv([ static::updateEnv([
'APP_KEY' => 'base64:'.base64_encode(random_bytes(32)), 'APP_KEY' => 'base64:'.base64_encode(random_bytes(32)),
'APP_URL' => url('/'), 'APP_URL' => url('/'),
]); ]);
} }
public static function createDbTables($host, $port, $database, $username, $password) public static function createDbTables($host, $port, $database, $username, $password)
{ {
if (!static::isDbValid($host, $port, $database, $username, $password)) { if (!static::isDbValid($host, $port, $database, $username, $password)) {
return false; return false;
@ -126,46 +126,46 @@ class Installer
return true; return true;
} }
/** /**
* Check if the database exists and is accessible. * Check if the database exists and is accessible.
* *
* @param $host * @param $host
* @param $port * @param $port
* @param $database * @param $database
* @param $host * @param $host
* @param $database * @param $database
* @param $username * @param $username
* @param $password * @param $password
* *
* @return bool * @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', [ Config::set('database.connections.install_test', [
'host' => $host, 'host' => $host,
'port' => $port, 'port' => $port,
'database' => $database, 'database' => $database,
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
'driver' => env('DB_CONNECTION', 'mysql'), 'driver' => env('DB_CONNECTION', 'mysql'),
'charset' => env('DB_CHARSET', 'utf8mb4'), 'charset' => env('DB_CHARSET', 'utf8mb4'),
]); ]);
try { try {
DB::connection('install_test')->getPdo(); DB::connection('install_test')->getPdo();
} catch (\Exception $e) {; } catch (\Exception $e) {;
return false; return false;
} }
// Purge test connection // Purge test connection
DB::purge('install_test'); DB::purge('install_test');
return true; return true;
} }
public static function saveDbVariables($host, $port, $database, $username, $password) public static function saveDbVariables($host, $port, $database, $username, $password)
{ {
$prefix = strtolower(str_random(3) . '_'); $prefix = strtolower(str_random(3) . '_');
// Update .env file // Update .env file
static::updateEnv([ static::updateEnv([
@ -177,74 +177,74 @@ class Installer
'DB_PREFIX' => $prefix, 'DB_PREFIX' => $prefix,
]); ]);
$con = env('DB_CONNECTION', 'mysql'); $con = env('DB_CONNECTION', 'mysql');
// Change current connection // Change current connection
$db = Config::get('database.connections.' . $con); $db = Config::get('database.connections.' . $con);
$db['host'] = $host; $db['host'] = $host;
$db['database'] = $database; $db['database'] = $database;
$db['username'] = $username; $db['username'] = $username;
$db['password'] = $password; $db['password'] = $password;
$db['prefix'] = $prefix; $db['prefix'] = $prefix;
Config::set('database.connections.' . $con, $db); Config::set('database.connections.' . $con, $db);
DB::purge($con); DB::purge($con);
DB::reconnect($con); DB::reconnect($con);
} }
public static function createCompany($name, $email, $locale) public static function createCompany($name, $email, $locale)
{ {
// Create company // Create company
$company = Company::create([ $company = Company::create([
'domain' => '', 'domain' => '',
]); ]);
// Set settings // Set settings
setting()->set([ setting()->set([
'general.company_name' => $name, 'general.company_name' => $name,
'general.company_email' => $email, 'general.company_email' => $email,
'general.default_currency' => 'USD', 'general.default_currency' => 'USD',
'general.default_locale' => $locale, 'general.default_locale' => $locale,
]); ]);
setting()->setExtraColumns(['company_id' => $company->id]); setting()->setExtraColumns(['company_id' => $company->id]);
setting()->save(); setting()->save();
} }
public static function createUser($email, $password, $locale) public static function createUser($email, $password, $locale)
{ {
// Create the user // Create the user
$user = User::create([ $user = User::create([
'name' => '', 'name' => '',
'email' => $email, 'email' => $email,
'password' => $password, 'password' => $password,
'locale' => $locale, 'locale' => $locale,
]); ]);
// Attach admin role // Attach admin role
$user->roles()->attach('1'); $user->roles()->attach('1');
// Attach company // Attach company
$user->companies()->attach('1'); $user->companies()->attach('1');
} }
public static function finalTouches() public static function finalTouches()
{ {
// Update .env file // Update .env file
static::updateEnv([ static::updateEnv([
'APP_LOCALE' => session('locale'), 'APP_LOCALE' => session('locale'),
'APP_INSTALLED' => 'true', 'APP_INSTALLED' => 'true',
'APP_DEBUG' => 'false', 'APP_DEBUG' => 'false',
]); ]);
// Rename the robots.txt file // Rename the robots.txt file
try { try {
File::move(base_path('robots.txt.dist'), base_path('robots.txt')); File::move(base_path('robots.txt.dist'), base_path('robots.txt'));
} catch (\Exception $e) { } catch (\Exception $e) {
// nothing to do // nothing to do
} }
} }
public static function updateEnv($data) public static function updateEnv($data)
{ {
@ -275,4 +275,4 @@ class Installer
return true; return true;
} }
} }