202 lines
6.5 KiB
PHP
Raw Normal View History

2018-02-20 17:05:58 +03:00
<?php
namespace App\Console\Commands;
use App\Utilities\Installer;
use Illuminate\Console\Command;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
2018-02-20 17:05:58 +03:00
class Install extends Command
{
2018-02-20 17:17:39 +03:00
const CMD_SUCCESS = 0;
const CMD_ERROR = 1;
const OPT_DB_HOST = 'db-host';
const OPT_DB_PORT = 'db-port';
const OPT_DB_NAME = 'db-name';
const OPT_DB_USERNAME = 'db-username';
const OPT_DB_PASSWORD = 'db-password';
const OPT_DB_PREFIX = 'db-prefix';
2018-02-20 17:17:39 +03:00
const OPT_COMPANY_NAME = 'company-name';
const OPT_COMPANY_EMAIL = 'company-email';
const OPT_ADMIN_EMAIL = 'admin-email';
const OPT_ADMIN_PASSWORD = 'admin-password';
const OPT_LOCALE = 'locale';
const OPT_NO_INTERACTION = 'no-interaction';
/**
* The name and signature of the console command.
*
* @var string
*/
2020-02-03 18:57:31 +03:00
protected $signature = 'install
2019-11-21 19:36:35 +03:00
{--db-host=localhost : Database host}
2018-02-20 17:05:58 +03:00
{--db-port=3306 : Port of the database host}
{--db-name= : Name of the database}
2019-11-21 19:41:33 +03:00
{--db-username=root : Username to use to access the database}
2018-02-20 17:05:58 +03:00
{--db-password= : Password to use to access the database}
{--db-prefix= : Table name prefix}
2019-11-21 19:36:35 +03:00
{--company-name=My Company : Name of the company}
{--company-email=my@company.com : Email of the company}
2018-02-20 17:05:58 +03:00
{--admin-email= : Admin user email}
{--admin-password= : Admin user password}
{--locale=en-GB : Language used in the app}
';
2018-02-20 17:17:39 +03:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Allows to install Akaunting directly through CLI';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
2018-02-20 17:05:58 +03:00
{
2019-11-21 19:36:35 +03:00
if (($missing_options = $this->getMissingOptions()) && $this->option(self::OPT_NO_INTERACTION)) {
2020-06-17 16:07:47 +03:00
$this->line('❌ Some options are missing and --no-interaction is present. Please run the following command for more information :');
2019-11-21 19:36:35 +03:00
$this->line('❌ php artisan help install');
$this->line('❌ Missing options are : ' . implode(', ', $missing_options));
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
return self::CMD_ERROR;
}
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
$this->line('Setting locale ' . $this->locale);
Session::put(self::OPT_LOCALE, $this->locale);
2020-02-03 18:57:31 +03:00
app()->setLocale($this->locale);
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
$this->prompt();
2018-02-20 17:05:58 +03:00
// Create the .env file
Installer::createDefaultEnvFile();
2018-02-20 17:17:39 +03:00
$this->line('Creating database tables');
2022-06-01 10:15:55 +03:00
if (! $this->createDatabaseTables()) {
2018-02-20 17:17:39 +03:00
return self::CMD_ERROR;
}
2018-02-20 17:05:58 +03:00
2022-06-01 10:15:55 +03:00
DB::transaction(function () {
$this->line('Creating company');
Installer::createCompany($this->company_name, $this->company_email, $this->locale);
2018-02-20 17:05:58 +03:00
2022-06-01 10:15:55 +03:00
$this->line('Creating admin');
Installer::createUser($this->admin_email, $this->admin_password, $this->locale);
});
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
$this->line('Applying the final touches');
Installer::finalTouches();
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
return self::CMD_SUCCESS;
}
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
/**
* Check that all options are presents. otherwise returns an array of the missing options
*/
2019-11-21 19:36:35 +03:00
private function getMissingOptions()
2018-02-20 17:05:58 +03:00
{
2019-11-21 19:36:35 +03:00
$missing_options = [];
2018-02-20 17:17:39 +03:00
2020-10-16 12:09:53 +03:00
$constants = [
2019-11-21 19:36:35 +03:00
'OPT_LOCALE',
'OPT_DB_PORT',
'OPT_DB_HOST',
'OPT_DB_NAME',
'OPT_DB_USERNAME',
'OPT_DB_PASSWORD',
'OPT_DB_PREFIX',
2019-11-21 19:36:35 +03:00
'OPT_COMPANY_NAME',
'OPT_COMPANY_EMAIL',
'OPT_ADMIN_EMAIL',
'OPT_ADMIN_PASSWORD'
2019-11-21 19:36:35 +03:00
];
2018-02-20 17:17:39 +03:00
2020-06-17 16:07:47 +03:00
$allowed_empty = ['db_password', 'db_prefix'];
2020-10-16 12:09:53 +03:00
foreach ($constants as $const) {
2019-11-21 19:36:35 +03:00
$option = constant("self::$const");
2018-02-20 17:17:39 +03:00
2019-11-21 19:36:35 +03:00
$property = str_replace('-', '_', $option);
2018-02-20 17:17:39 +03:00
2019-11-21 19:36:35 +03:00
$this->$property = $this->option($option);
2018-02-20 17:17:39 +03:00
2020-06-17 16:07:47 +03:00
if (!empty($this->$property)) {
continue;
}
2018-02-20 17:17:39 +03:00
2020-06-17 16:07:47 +03:00
if (in_array($property, $allowed_empty)) {
continue;
2019-11-21 19:36:35 +03:00
}
2020-06-17 16:07:47 +03:00
$missing_options[] = $option;
2018-02-20 17:17:39 +03:00
}
2019-11-21 19:36:35 +03:00
return $missing_options;
2018-02-20 17:17:39 +03:00
}
/**
* Ask the user for data if some options are missing.
*/
private function prompt()
2018-02-20 17:05:58 +03:00
{
2019-11-21 19:36:35 +03:00
if (empty($this->db_host)) {
$this->db_host = $this->ask('What is the database host?', 'localhost');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->db_port)) {
$this->db_port = $this->ask('What is the database port?', '3306');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->db_name)) {
$this->db_name = $this->ask('What is the database name?');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->db_username)) {
2019-11-21 19:41:33 +03:00
$this->db_username = $this->ask('What is the database username?', 'root');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
if (!isset($this->db_password)) {
2019-11-21 19:36:35 +03:00
$this->db_password = $this->secret('What is the database password?', '');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->company_name)) {
$this->company_name = $this->ask('What is the company name?', 'My Company');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->company_email)) {
$this->company_email = $this->ask('What is the company contact email?', 'my@company.com');
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->admin_email)) {
$this->admin_email = $this->ask('What is the admin email?', $this->company_email);
2018-02-20 17:17:39 +03:00
}
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
if (empty($this->admin_password)) {
$this->admin_password = $this->secret('What is the admin password?');
2018-02-20 17:17:39 +03:00
}
}
2018-02-20 17:05:58 +03:00
2019-11-16 10:21:14 +03:00
private function createDatabaseTables()
{
2019-11-21 19:36:35 +03:00
$this->db_host = $this->option(self::OPT_DB_HOST);
$this->db_port = $this->option(self::OPT_DB_PORT);
$this->db_name = $this->option(self::OPT_DB_NAME);
$this->db_username = $this->option(self::OPT_DB_USERNAME);
$this->db_password = $this->option(self::OPT_DB_PASSWORD);
$this->db_prefix = $this->option(self::OPT_DB_PREFIX);
2018-02-20 17:05:58 +03:00
2019-11-21 19:36:35 +03:00
$this->line('Connecting to database ' . $this->db_name . '@' . $this->db_host . ':' . $this->db_port);
2018-02-20 17:05:58 +03:00
if (!Installer::createDbTables($this->db_host, $this->db_port, $this->db_name, $this->db_username, $this->db_password, $this->db_prefix)) {
2018-02-20 17:17:39 +03:00
$this->error('Error: Could not connect to the database! Please, make sure the details are correct.');
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
return false;
}
2018-02-20 17:05:58 +03:00
2018-02-20 17:17:39 +03:00
return true;
}
2018-02-20 17:05:58 +03:00
}