step status refactor

This commit is contained in:
Tony Murray
2020-06-21 01:07:56 -05:00
parent d1ceb14b9a
commit e267ec1cab
6 changed files with 72 additions and 27 deletions

View File

@@ -31,11 +31,14 @@ use LibreNMS\Validations\Php;
class ChecksController extends InstallationController implements InstallerStep
{
const MODULES = ['pdo_mysql', 'mysqlnd', 'gd'];
protected $step = 'checks';
public function index()
{
$this->initInstallStep();
if ($this->complete()) {
$this->markStepComplete('checks');
$this->markStepComplete();
}
return view('install.checks', $this->formatData([

View File

@@ -36,9 +36,14 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
class DatabaseController extends InstallationController implements InstallerStep
{
const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket'];
protected $step = 'database';
public function index(Request $request)
{
if (!$this->initInstallStep()) {
return $this->redirectToIncomplete();
}
$data = Arr::only(session()->get('db') ?: [], self::KEYS);
$data['valid_credentials'] = Eloquent::isConnected();
$data['migrated'] = session('install.database');
@@ -88,7 +93,7 @@ class DatabaseController extends InstallationController implements InstallerStep
throw new \RuntimeException('Migration failed');
}
echo "\n\nSuccess!";
$this->markStepComplete('database');
$this->markStepComplete();
} catch (\Exception $e) {
echo $e->getMessage() . "\n\nError!";
}
@@ -108,7 +113,7 @@ class DatabaseController extends InstallationController implements InstallerStep
$this->configureDatabase();
if (Eloquent::isConnected() && Schema::isCurrent()) {
$this->markStepComplete('database');
$this->markStepComplete();
return true;
}

View File

@@ -31,10 +31,12 @@ use LibreNMS\Util\EnvHelper;
class FinalizeController extends InstallationController implements InstallerStep
{
protected $step = 'finish';
public function index()
{
if (!$this->enabled()) {
return redirect()->route('install');
if (!$this->initInstallStep()) {
return $this->redirectToIncomplete();
}
$env = '';

View File

@@ -31,6 +31,7 @@ use LibreNMS\DB\Eloquent;
class InstallationController extends Controller
{
protected $connection = 'setup';
protected $step;
protected $steps = [
'checks' => \App\Http\Controllers\Install\ChecksController::class,
'database' => \App\Http\Controllers\Install\DatabaseController::class,
@@ -38,18 +39,15 @@ class InstallationController extends Controller
'finish' => \App\Http\Controllers\Install\FinalizeController::class,
];
public function __construct()
public function redirectToIncomplete()
{
if (is_string(config('librenms.install'))) {
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install'))));
foreach ($this->stepStatus() as $step => $complete) {
if (!$complete) {
return redirect()->route("install.$step");
}
}
$this->configureDatabase();
}
public function baseIndex()
{
$initial = key($this->steps) ?: 'checks';
return redirect()->route("install.$initial");
return redirect()->route('install.checks');
}
public function invalid()
@@ -59,23 +57,43 @@ class InstallationController extends Controller
public function stepsCompleted()
{
return response()->json(array_map(function ($class) {
$controller = app()->make($class);
return $controller->complete();
}, $this->steps));
return response()->json($this->stepStatus());
}
final protected function markStepComplete($step)
/**
* Init step info and return false if previous steps have not been completed.
*
* @return bool
*/
final protected function initInstallStep()
{
session(["install.$step" => true]);
if (is_string(config('librenms.install'))) {
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install'))));
}
$this->configureDatabase();
foreach ($this->stepStatus() as $step => $completed) {
if ($step == $this->step) {
return true;
}
if (!$completed) {
return false;
}
}
return false;
}
final protected function markStepComplete()
{
session(["install.$this->step" => true]);
session()->save();
}
final protected function formatData($data = [])
{
$data['steps'] = array_map(function ($class) {
return app()->make($class);
}, $this->steps);
$data['steps'] = $this->hydrateControllers();
return $data;
}
@@ -95,4 +113,19 @@ class InstallationController extends Controller
config(['database.default', $this->connection]);
}
}
private function hydrateControllers()
{
$this->steps = array_map(function ($class) {
return is_object($class) ? $class : app()->make($class);
}, $this->steps);
}
private function stepStatus()
{
$this->hydrateControllers();
return array_map(function ($controller) {
return $controller->complete();
}, $this->steps);
}
}

View File

@@ -33,10 +33,12 @@ use LibreNMS\Interfaces\InstallerStep;
class MakeUserController extends InstallationController implements InstallerStep
{
protected $step = 'user';
public function index(Request $request)
{
if (!self::enabled()) {
return redirect()->route('install');
if (!$this->initInstallStep()) {
return $this->redirectToIncomplete();
}
if (session('install.database')) {
@@ -44,7 +46,7 @@ class MakeUserController extends InstallationController implements InstallerStep
}
if (isset($user)) {
$this->markStepComplete('user');
$this->markStepComplete();
return view('install.user-created', $this->formatData([
'user' => $user,
]));

View File

@@ -150,7 +150,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
// installation routes
Route::group(['prefix' => 'install', 'namespace' => 'Install'], function () {
Route::get('/', 'InstallationController@baseIndex')->name('install');
Route::get('/', 'InstallationController@redirectToIncomplete')->name('install');
Route::get('/checks', 'ChecksController@index')->name('install.checks');
Route::get('/database', 'DatabaseController@index')->name('install.database');
Route::get('/user', 'MakeUserController@index')->name('install.user');