数据库安装指令

This commit is contained in:
Wisp X 2022-02-22 08:31:55 +08:00
parent 144f2650a2
commit 2d905a5550
4 changed files with 151 additions and 41 deletions

View File

@ -1,18 +1,18 @@
APP_NAME="Lsky Pro"
APP_ENV=local
APP_ENV=prod
APP_KEY=
APP_DEBUG=true
APP_DEBUG=false
APP_URL=http://localhost
LOG_CHANNEL=daily
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=lsky
DB_USERNAME=root
DB_CONNECTION=
DB_HOST=
DB_PORT=
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
BROADCAST_DRIVER=log
@ -28,21 +28,6 @@ REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

View File

@ -0,0 +1,111 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
class Install extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'lsky:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install Lsky Pro';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
$this->signature = implode(' ', [
'lsky:install',
'{--connection=mysql : Database type}',
'{--host=127.0.0.1 : Database connection address}',
'{--port=3306 : Database connection port}',
'{--database= : Database name}',
'{--username=root : Database connection user name}',
'{--password=root : Database connection password}',
]);
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
// TODO 判断是否已经安装
if (file_exists(base_path('.env'))) {
$this->warn('The program has been installed.');
return;
}
$driver = $this->option('connection');
$connection = "database.connections.{$driver}";
$options = [
'connection' => $this->option('connection'),
'host' => $this->option('host'),
'port' => $this->option('port'),
'database' => $this->option('database'),
'username' => $this->option('username'),
'password' => $this->option('password'),
];
$configs = array_intersect_key($options, config($connection));
// 覆盖默认配置
Config::set($connection, array_merge(config($connection), $configs));
// 设置默认数据库驱动
Config::set('database.default', $driver);
try {
// 执行数据库迁移
Artisan::call('migrate:fresh', ['--seed' => true, '--force' => true], outputBuffer: $this->getOutput());
// 创建 env 文件
$replaces = collect($options)->transform(function ($item, $key) {
return ['DB_'.strtoupper($key) => $item];
})->collapse();
file_put_contents($this->laravel->environmentFilePath(), preg_replace(
$replaces->map(fn ($item, $key) => $this->replacementPattern($key, env($key, '')))->values()->toArray(),
$replaces->map(fn ($item, $key) => "{$key}={$item}")->values()->toArray(),
file_get_contents($this->laravel->environmentFilePath().'.example')
));
// 生成 key
Artisan::call('key:generate');
} catch (\Throwable $e) {
$this->warn("Installation error!\n");
$this->error($e->getMessage());
return;
}
$this->info('Install success!');
}
/**
* Get a regex pattern that will match env APP_KEY with any random key.
*
* @param string $name
* @param string $value
* @return string
*/
protected function replacementPattern(string $name, string $value): string
{
$escaped = preg_quote('='.$value, '/');
return "/^{$name}{$escaped}/m";
}
}

View File

@ -2,16 +2,7 @@
namespace Database\Seeders;
use App\Enums\ConfigKey;
use App\Enums\GroupConfigKey;
use App\Enums\Mail\SmtpOption;
use App\Enums\Scan\AliyunOption;
use App\Enums\Watermark\FontOption;
use App\Enums\Watermark\ImageOption;
use App\Models\Group;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
@ -22,15 +13,8 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
$date = Carbon::now()->format('Y-m-d H:i:s');
$array = collect(config('convention.app'))->transform(function ($value, $key) use ($date) {
return [
'name' => $key,
'value' => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value,
'updated_at' => $date,
'created_at' => $date,
];
})->values()->toArray();
DB::table('configs')->insert($array);
$this->call([
InstallSeeder::class,
]);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class InstallSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// TODO 判断是否为安装动作
$date = Carbon::now()->format('Y-m-d H:i:s');
$array = collect(config('convention.app'))->transform(function ($value, $key) use ($date) {
return [
'name' => $key,
'value' => is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value,
'updated_at' => $date,
'created_at' => $date,
];
})->values()->toArray();
DB::table('configs')->insert($array);
}
}