diff --git a/.env.example b/.env.example index 2181b928..3684d104 100644 --- a/.env.example +++ b/.env.example @@ -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= diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php new file mode 100644 index 00000000..2377faea --- /dev/null +++ b/app/Console/Commands/Install.php @@ -0,0 +1,111 @@ +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"; + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 4825c36d..24dc792a 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -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, + ]); } } diff --git a/database/seeders/InstallSeeder.php b/database/seeders/InstallSeeder.php new file mode 100644 index 00000000..3523917d --- /dev/null +++ b/database/seeders/InstallSeeder.php @@ -0,0 +1,30 @@ +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); + } +}