From 09600cf5614699ea423b5d1e76f8b2abf610eeca Mon Sep 17 00:00:00 2001 From: Wisp X Date: Tue, 22 Feb 2022 15:26:26 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=94=B9=E8=BF=9B=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=AE=89=E8=A3=85=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Install.php | 21 ++++------------- app/Http/Controllers/Controller.php | 10 ++++++++ app/Http/Middleware/CheckIsInstalled.php | 22 ++++++++++++++++++ app/Providers/AppServiceProvider.php | 29 ++++++++++++++++-------- resources/views/install.blade.php | 3 +++ routes/web.php | 4 +++- 6 files changed, 63 insertions(+), 26 deletions(-) create mode 100644 app/Http/Middleware/CheckIsInstalled.php create mode 100644 resources/views/install.blade.php diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php index 732b64ff..0d97262c 100644 --- a/app/Console/Commands/Install.php +++ b/app/Console/Commands/Install.php @@ -48,8 +48,8 @@ class Install extends Command public function handle() { // 判断是否已经安装 - if (file_exists(base_path('.env'))) { - $this->warn('Already installed. if you want to reinstall, please remove .env file.'); + if (file_exists(base_path('installed.lock'))) { + $this->warn('Already installed. if you want to reinstall, please remove installed.lock file.'); return; } @@ -75,17 +75,13 @@ class Install extends Command Artisan::call('migrate:fresh', ['--force' => true]); // 填充数据 Artisan::call('db:seed', ['--force' => true, '--class' => 'InstallSeeder']); - // 创建 env 文件 - $replaces = collect($options)->transform(function ($item, $key) { - return ['DB_'.strtoupper($key) => $item]; - })->collapse(); + // 更新 env 文件 + $replaces = collect($options)->transform(fn ($item, $key) => ['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') + file_get_contents($this->laravel->environmentFilePath()) )); - // 生成 key - Artisan::call('key:generate'); } catch (\Throwable $e) { $this->warn("Installation error!\n"); $this->error($e->getMessage()); @@ -95,13 +91,6 @@ class Install extends Command $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, '/'); diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index b96fd9e9..d5acd1da 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -17,6 +17,7 @@ use Illuminate\Routing\Controller as BaseController; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; +use Illuminate\View\View; use League\Flysystem\FilesystemException; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -24,6 +25,15 @@ class Controller extends BaseController { use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Api; + public function install(Request $request): View|Response + { + if (file_exists(base_path('installed.lock'))) { + abort(404); + } + + return view('install'); + } + public function upload(Request $request, ImageService $service): Response { try { diff --git a/app/Http/Middleware/CheckIsInstalled.php b/app/Http/Middleware/CheckIsInstalled.php new file mode 100644 index 00000000..09189f2c --- /dev/null +++ b/app/Http/Middleware/CheckIsInstalled.php @@ -0,0 +1,22 @@ +getSchemeAndHttpHost()); - Config::set('mail', array_merge(\config('mail'), Utils::config(ConfigKey::Mail)->toArray())); + // 是否需要生成 env 文件 + if (! file_exists(base_path('.env'))) { + file_put_contents(base_path('.env'), file_get_contents(base_path('.env.example'))); + // 生成 key + Artisan::call('key:generate'); + } - // 初始化视图中的默认数据 - View::composer('*', function (\Illuminate\View\View $view) { - $configs = Auth::check() && Auth::user()->group ? Auth::user()->group->configs : Group::getGuestConfigs(); - $view->with('groupConfigs', $configs); - }); + // 如果已经安装程序,初始化一些配置 + if (file_exists(base_path('installed.lock'))) { + // 覆盖默认配置 + Config::set('app.name', Utils::config(ConfigKey::SiteName)); + Config::set('app.url', request()->getSchemeAndHttpHost()); + Config::set('mail', array_merge(\config('mail'), Utils::config(ConfigKey::Mail)->toArray())); + + // 初始化视图中的默认数据 + View::composer('*', function (\Illuminate\View\View $view) { + $configs = Auth::check() && Auth::user()->group ? Auth::user()->group->configs : Group::getGuestConfigs(); + $view->with('groupConfigs', $configs); + }); + } } } diff --git a/resources/views/install.blade.php b/resources/views/install.blade.php new file mode 100644 index 00000000..8590b2c6 --- /dev/null +++ b/resources/views/install.blade.php @@ -0,0 +1,3 @@ + + 1 + diff --git a/routes/web.php b/routes/web.php index 1831c9f5..e0c56869 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,6 +12,7 @@ */ use Illuminate\Support\Facades\Route; +use App\Http\Middleware\CheckIsInstalled; use App\Http\Controllers\Controller; use App\Http\Controllers\User\UserController; use App\Http\Controllers\User\ImageController; @@ -26,7 +27,8 @@ use App\Http\Controllers\Admin\UserController as AdminUserController; use App\Http\Controllers\Admin\SettingController as AdminSettingController; use App\Http\Controllers\Admin\ImageController as AdminImageController; -Route::get('/', fn () => view('welcome'))->name('/'); +Route::get('/', fn () => view('welcome'))->name('/')->middleware(CheckIsInstalled::class); +Route::any('install', [Controller::class, 'install'])->name('install'); Route::post('upload', [Controller::class, 'upload']); Route::group(['middleware' => ['auth']], function () { Route::get('dashboard', [UserController::class, 'dashboard'])->name('dashboard');