mirror of
https://github.com/lsky-org/lsky-pro.git
synced 2025-01-09 04:19:32 +08:00
✨ 改进检测安装逻辑
This commit is contained in:
parent
53ae724620
commit
09600cf561
@ -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, '/');
|
||||
|
@ -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 {
|
||||
|
22
app/Http/Middleware/CheckIsInstalled.php
Normal file
22
app/Http/Middleware/CheckIsInstalled.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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,6 +30,15 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
// 是否需要生成 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');
|
||||
}
|
||||
|
||||
// 如果已经安装程序,初始化一些配置
|
||||
if (file_exists(base_path('installed.lock'))) {
|
||||
// 覆盖默认配置
|
||||
Config::set('app.name', Utils::config(ConfigKey::SiteName));
|
||||
Config::set('app.url', request()->getSchemeAndHttpHost());
|
||||
@ -40,4 +50,5 @@ class AppServiceProvider extends ServiceProvider
|
||||
$view->with('groupConfigs', $configs);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
3
resources/views/install.blade.php
Normal file
3
resources/views/install.blade.php
Normal file
@ -0,0 +1,3 @@
|
||||
<x-guest-layout>
|
||||
1
|
||||
</x-guest-layout>
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user