FleetCart/app/Http/Controllers/InstallController.php

84 lines
2.4 KiB
PHP
Raw Normal View History

2023-06-11 12:14:03 +00:00
<?php
namespace FleetCart\Http\Controllers;
use Exception;
use FleetCart\Install\App;
use FleetCart\Install\Store;
2023-12-03 14:07:47 +00:00
use Illuminate\Http\Response;
2023-06-11 12:14:03 +00:00
use FleetCart\Install\Database;
2023-12-03 14:07:47 +00:00
use FleetCart\Install\Permission;
use Illuminate\Http\JsonResponse;
2023-06-11 12:14:03 +00:00
use FleetCart\Install\Requirement;
use Illuminate\Routing\Controller;
2023-12-03 14:07:47 +00:00
use Illuminate\Contracts\View\View;
2023-06-11 12:14:03 +00:00
use FleetCart\Install\AdminAccount;
2023-12-03 14:07:47 +00:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
2023-06-11 12:14:03 +00:00
use FleetCart\Http\Requests\InstallRequest;
use Jackiedo\DotenvEditor\Facades\DotenvEditor;
2023-12-03 14:07:47 +00:00
use Illuminate\Contracts\Foundation\Application;
2023-06-11 12:14:03 +00:00
use FleetCart\Http\Middleware\RedirectIfInstalled;
class InstallController extends Controller
{
public function __construct()
{
$this->middleware(RedirectIfInstalled::class);
}
2023-12-03 14:07:47 +00:00
public function installation(Requirement $requirement, Permission $permission): Factory|View|Application
2023-06-11 12:14:03 +00:00
{
2023-12-03 14:07:47 +00:00
return view('install.install', compact('requirement', 'permission'));
2023-06-11 12:14:03 +00:00
}
2023-12-03 14:07:47 +00:00
public function install(
2023-06-11 12:14:03 +00:00
InstallRequest $request,
2023-12-03 14:07:47 +00:00
Database $database,
AdminAccount $admin,
Store $store,
App $app
): JsonResponse
{
2023-06-11 12:14:03 +00:00
@set_time_limit(0);
try {
2023-12-03 14:07:47 +00:00
Artisan::call('optimize:clear');
$database->setup($request);
$admin->setup($request);
$store->setup($request);
2023-06-11 12:14:03 +00:00
$app->setup();
2023-12-03 14:07:47 +00:00
DotenvEditor::setKey('APP_INSTALLED', 'true')->save();
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
Artisan::call('key:generate', ['--force' => true]);
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
$success = true;
$message = "Congratulations! FleetCart installed successfully";
} catch (Exception $e) {
$success = false;
$message = $e->getMessage();
2023-06-11 12:14:03 +00:00
2023-12-03 14:07:47 +00:00
try {
if (Schema::hasTable('migrations')) {
Artisan::call('migrate:rollback', ['--force' => true]);
}
} catch (Exception $e) {
$message .= '<br><br>' . $e->getMessage();
}
} finally {
return response()->json(
[
'success' => $success,
'message' => $message,
],
$success ? Response::HTTP_OK : Response::HTTP_INTERNAL_SERVER_ERROR
);
}
2023-06-11 12:14:03 +00:00
}
}