改进检测安装逻辑

This commit is contained in:
Wisp X 2022-02-22 15:26:26 +08:00
parent 53ae724620
commit 09600cf561
6 changed files with 63 additions and 26 deletions

View File

@ -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, '/');

View File

@ -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 {

View File

@ -0,0 +1,22 @@
<?php
namespace App\Http\Middleware;
use App\Http\Api;
use Closure;
use Illuminate\Http\Request;
class CheckIsInstalled
{
use Api;
public function handle(Request $request, Closure $next)
{
// 检测程序是否安装
if (! file_exists(base_path('installed.lock'))) {
return redirect('install');
}
return $next($request);
}
}

View File

@ -5,6 +5,7 @@ namespace App\Providers;
use App\Enums\ConfigKey;
use App\Models\Group;
use App\Utils;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
@ -29,15 +30,25 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
// 覆盖默认配置
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()));
// 是否需要生成 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);
});
}
}
}

View File

@ -0,0 +1,3 @@
<x-guest-layout>
1
</x-guest-layout>

View File

@ -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');