mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Refactor to a simpler style
This commit is contained in:
@@ -27,9 +27,9 @@ namespace App\Http\Controllers\Install;
|
||||
|
||||
use LibreNMS\Validations\Php;
|
||||
|
||||
class ChecksController extends \App\Http\Controllers\Controller
|
||||
class ChecksController extends InstallationController
|
||||
{
|
||||
public function __invoke()
|
||||
public function index()
|
||||
{
|
||||
$results = [];
|
||||
$php_ok = version_compare(PHP_VERSION, Php::PHP_MIN_VERSION, '>=');
|
||||
@@ -43,12 +43,12 @@ class ChecksController extends \App\Http\Controllers\Controller
|
||||
session(['install.checks' => true]);
|
||||
}
|
||||
|
||||
return view('install.checks', [
|
||||
return view('install.checks', $this->formatData([
|
||||
'php_version' => PHP_VERSION,
|
||||
'php_required' => Php::PHP_MIN_VERSION,
|
||||
'php_ok' => $php_ok,
|
||||
'modules' => $results
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
private function checkPhpModule(&$results, $module)
|
||||
|
||||
@@ -25,22 +25,20 @@
|
||||
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use LibreNMS\DB\Eloquent;
|
||||
|
||||
class DatabaseController extends Controller
|
||||
class DatabaseController extends InstallationController
|
||||
{
|
||||
const KEYS = ['host', 'username', 'password', 'database', 'port', 'unix_socket'];
|
||||
|
||||
public function __invoke(Request $request)
|
||||
public function index(Request $request)
|
||||
{
|
||||
$data = Arr::only(session()->get('db') ?: [], self::KEYS);
|
||||
$data['stage'] = 2;
|
||||
$data['status'] = session('install.database');
|
||||
|
||||
return view('install.database', $data);
|
||||
return view('install.database', $this->formatData($data));
|
||||
}
|
||||
|
||||
public function test(Request $request)
|
||||
|
||||
@@ -25,23 +25,22 @@
|
||||
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\StreamedOutput;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class DatabaseMigrationController extends Controller
|
||||
class DatabaseMigrationController extends InstallationController
|
||||
{
|
||||
use UsesDatabase;
|
||||
|
||||
public function __invoke()
|
||||
public function index()
|
||||
{
|
||||
return view('install.migrate-database');
|
||||
return view('install.migrate-database', $this->formatData());
|
||||
}
|
||||
|
||||
public function migrate(Request $request)
|
||||
{
|
||||
$this->configureDatabase();
|
||||
if (!self::enabled()) {
|
||||
return redirect()->route('install.database');
|
||||
}
|
||||
|
||||
$response = new StreamedResponse(function () use ($request) {
|
||||
try {
|
||||
|
||||
@@ -28,10 +28,14 @@ namespace App\Http\Controllers\Install;
|
||||
use Exception;
|
||||
use LibreNMS\Util\EnvHelper;
|
||||
|
||||
class FinalizeController extends \App\Http\Controllers\Controller
|
||||
class FinalizeController extends InstallationController
|
||||
{
|
||||
public function __invoke()
|
||||
public function index()
|
||||
{
|
||||
if (!self::enabled($this->steps)) {
|
||||
return redirect()->route('install');
|
||||
}
|
||||
|
||||
$env = '';
|
||||
$config_file = base_path('config.php');
|
||||
$config = $this->getConfigFileContents();
|
||||
@@ -63,13 +67,13 @@ class FinalizeController extends \App\Http\Controllers\Controller
|
||||
// session()->forget('db');
|
||||
}
|
||||
|
||||
return view('install.finish', [
|
||||
return view('install.finish', $this->formatData([
|
||||
'env' => $env,
|
||||
'config' => $config,
|
||||
'messages' => $messages,
|
||||
'success' => $success,
|
||||
'config_message' => $config_message,
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
private function writeEnvFile()
|
||||
|
||||
@@ -26,11 +26,57 @@
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use LibreNMS\DB\Eloquent;
|
||||
|
||||
class InstallationController extends Controller
|
||||
{
|
||||
protected $connection = 'setup';
|
||||
protected $steps = [
|
||||
'checks' => \App\Http\Controllers\Install\ChecksController::class,
|
||||
'database' => \App\Http\Controllers\Install\DatabaseController::class,
|
||||
'migrate' => \App\Http\Controllers\Install\DatabaseMigrationController::class,
|
||||
'user' => \App\Http\Controllers\Install\MakeUserController::class,
|
||||
'finish' => \App\Http\Controllers\Install\FinalizeController::class,
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (is_string(config('librenms.install'))) {
|
||||
$this->steps = array_intersect_key($this->steps, array_flip(explode(',', config('librenms.install'))));
|
||||
}
|
||||
$this->configureDatabase();
|
||||
}
|
||||
|
||||
public function baseIndex()
|
||||
{
|
||||
return redirect()->route('install.checks');
|
||||
}
|
||||
|
||||
public function invalid()
|
||||
{
|
||||
abort(404);
|
||||
}
|
||||
|
||||
final protected function formatData($data = [])
|
||||
{
|
||||
$data['steps'] = $this->steps;
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function configureDatabase()
|
||||
{
|
||||
$db = session('db');
|
||||
if (!empty($db)) {
|
||||
Eloquent::setConnection(
|
||||
$this->connection,
|
||||
$db['host'] ?? 'localhost',
|
||||
$db['username'] ?? 'librenms',
|
||||
$db['password'] ?? null,
|
||||
$db['database'] ?? 'librenms',
|
||||
$db['port'] ?? 3306,
|
||||
$db['socket'] ?? null,
|
||||
);
|
||||
config(['database.default', $this->connection]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,27 +29,28 @@ use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class MakeUserController extends \App\Http\Controllers\Controller
|
||||
class MakeUserController extends InstallationController
|
||||
{
|
||||
use UsesDatabase;
|
||||
|
||||
public function __invoke(Request $request)
|
||||
public function index(Request $request)
|
||||
{
|
||||
if (!self::enabled()) {
|
||||
return redirect()->route('install');
|
||||
}
|
||||
|
||||
if (session('install.database')) {
|
||||
$this->configureDatabase();
|
||||
$user = User::first();
|
||||
}
|
||||
|
||||
if (isset($user)) {
|
||||
session(['install.user' => true]);
|
||||
return view('install.user-created', [
|
||||
return view('install.user-created', $this->formatData([
|
||||
'user' => $user,
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
return view('install.make-user', [
|
||||
return view('install.make-user', $this->formatData([
|
||||
'messages' => Arr::wrap(session('message'))
|
||||
]);
|
||||
]));
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
@@ -60,7 +61,6 @@ class MakeUserController extends \App\Http\Controllers\Controller
|
||||
]);
|
||||
|
||||
try {
|
||||
$this->configureDatabase();
|
||||
$user = new User($request->only(['username', 'password', 'email']));
|
||||
$user->level = 10;
|
||||
$user->setPassword($request->get('password'));
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* UsesDatabase.php
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2020 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers\Install;
|
||||
|
||||
use LibreNMS\DB\Eloquent;
|
||||
|
||||
trait UsesDatabase
|
||||
{
|
||||
protected $connection = 'setup';
|
||||
|
||||
protected function configureDatabase()
|
||||
{
|
||||
Eloquent::setConnection(
|
||||
$this->connection,
|
||||
session('db.host', 'localhost'),
|
||||
session('db.username', 'librenms'),
|
||||
session('db.password'),
|
||||
session('db.database', 'librenms'),
|
||||
session('db.port', 3306),
|
||||
session('db.socket')
|
||||
);
|
||||
config(['database.default', $this->connection]);
|
||||
|
||||
return \DB::connection($this->connection);
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* InstallMenuComposer.php
|
||||
*
|
||||
* -Description-
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2020 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
namespace App\Http\ViewComposers;
|
||||
|
||||
use Illuminate\View\View;
|
||||
|
||||
class InstallMenuComposer
|
||||
{
|
||||
// TODO port to Laravel 7 View Component
|
||||
private $steps = [
|
||||
'checks' => \App\Http\Controllers\Install\ChecksController::class,
|
||||
'database' => \App\Http\Controllers\Install\DatabaseController::class,
|
||||
'migrate' => \App\Http\Controllers\Install\DatabaseMigrationController::class,
|
||||
'user' => \App\Http\Controllers\Install\MakeUserController::class,
|
||||
'finish' => \App\Http\Controllers\Install\FinalizeController::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Bind data to the view.
|
||||
*
|
||||
* @param View $view
|
||||
* @return void
|
||||
*/
|
||||
public function compose(View $view)
|
||||
{
|
||||
$steps = $this->steps;
|
||||
if (is_string(config('librenms.install'))) {
|
||||
$steps = array_intersect_key($steps, array_flip(explode(',', config('librenms.install'))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
$view->with(['steps' => $steps]);
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ class ComposerServiceProvider extends ServiceProvider
|
||||
{
|
||||
View::composer('layouts.librenmsv1', \App\Http\ViewComposers\LayoutComposer::class);
|
||||
View::composer('layouts.menu', \App\Http\ViewComposers\MenuComposer::class);
|
||||
View::composer('components.install_menu', \App\Http\ViewComposers\InstallMenuComposer::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,5 +54,11 @@ return [
|
||||
'config_exists' => 'config.php file exists',
|
||||
'config_written' => 'config.php file written',
|
||||
'config_not_written' => 'Could not write config.php',
|
||||
'not_finished' => 'You have not quite finished yet!',
|
||||
'validate' => 'First, you need to :validate and fix any issues.',
|
||||
'validate_link' => 'validate your install',
|
||||
'thanks' => 'Thank you for setting up LibreNMS.',
|
||||
'statistics' => 'It would be great if you would consider contributing to our statistics, you can do this on the :about and check the box under Statistics.',
|
||||
'statistics_link' => 'About LibreNMS Page',
|
||||
]
|
||||
];
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
<div class="d-flex flex-row justify-content-around">
|
||||
@foreach($steps as $step => $controller)
|
||||
<div>
|
||||
<a href="{{ route('install.' . $step) }}"
|
||||
id="install-{{ $step }}-button"
|
||||
class="btn btn-primary btn-circle @if(!$controller::enabled($steps)) disabled @endif"
|
||||
title="@lang("install.$step.title")"
|
||||
>
|
||||
<i class="fa fa-lg {{ $controller::icon() }}"></i>
|
||||
</a>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
@@ -3,7 +3,7 @@
|
||||
@section('title', trans('install.finish.title'))
|
||||
|
||||
@section('content')
|
||||
<div class="card mb-1">
|
||||
<div class="card mb-2">
|
||||
<div class="card-header" data-toggle="collapse" data-target="#env-file-text" aria-expanded="false">
|
||||
@lang('install.finish.env_written')
|
||||
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i>
|
||||
@@ -12,7 +12,7 @@
|
||||
<pre class="card bg-light p-3">{{ $env }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card mb-1">
|
||||
<div class="card mb-2">
|
||||
<div class="card-header" data-toggle="collapse" data-target="#config-file-text" aria-expanded="false">
|
||||
{{ $config_message }}
|
||||
<i class="fa fa-chevron-up rotate-if-collapsed pull-right"></i>
|
||||
@@ -21,6 +21,22 @@
|
||||
<pre class="card bg-light p-3">{{ $config }}</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="alert alert-danger">
|
||||
<p>@lang('install.finish.not_finished')</p>
|
||||
<p>@lang('install.finish.validate', ['about' => '<a href="' . url('validate') . '">' . __('install.finish.validate_link') . '</a>'])</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<div class="alert alert-success">
|
||||
<p>@lang('install.finish.thanks')</p>
|
||||
<p>@lang('install.finish.statistics', ['about' => '<a href="' . url('about') . '">' . __('install.finish.statistics_link') . '</a>'])</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@section('style')
|
||||
|
||||
@@ -46,7 +46,19 @@
|
||||
<span class="h2">@yield('title')</span>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
@include('components.install_menu')
|
||||
<div class="d-flex flex-row justify-content-around">
|
||||
@foreach($steps as $step => $controller)
|
||||
<div>
|
||||
<a href="{{ route('install.' . $step) }}"
|
||||
id="install-{{ $step }}-button"
|
||||
class="btn btn-primary btn-circle @if(!$controller::enabled($steps)) disabled @endif"
|
||||
title="@lang("install.$step.title")"
|
||||
>
|
||||
<i class="fa fa-lg {{ $controller::icon() }}"></i>
|
||||
</a>
|
||||
</div>
|
||||
@endforeach
|
||||
</div>
|
||||
<div class="content-divider"></div>
|
||||
<div class="row">
|
||||
<div id="error-box" class="col-12">
|
||||
|
||||
+6
-6
@@ -150,12 +150,12 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () {
|
||||
|
||||
// installation routes
|
||||
Route::group(['prefix' => 'install', 'namespace' => 'Install'], function () {
|
||||
Route::redirect('/', '/install/checks')->name('install');
|
||||
Route::get('/checks', 'ChecksController')->name('install.checks');
|
||||
Route::get('/database', 'DatabaseController')->name('install.database');
|
||||
Route::get('/database/migrate', 'DatabaseMigrationController')->name('install.migrate');
|
||||
Route::get('/user', 'MakeUserController')->name('install.user');
|
||||
Route::get('/finish', 'FinalizeController')->name('install.finish');
|
||||
Route::get('/', 'InstallationController@baseIndex')->name('install');
|
||||
Route::get('/checks', 'ChecksController@index')->name('install.checks');
|
||||
Route::get('/database', 'DatabaseController@index')->name('install.database');
|
||||
Route::get('/database/migrate', 'DatabaseMigrationController@index')->name('install.migrate');
|
||||
Route::get('/user', 'MakeUserController@index')->name('install.user');
|
||||
Route::get('/finish', 'FinalizeController@index')->name('install.finish');
|
||||
|
||||
Route::post('/user/create', 'MakeUserController@create')->name('install.action.user');
|
||||
Route::post('/database/test', 'DatabaseController@test')->name('install.acton.test-database');
|
||||
|
||||
Reference in New Issue
Block a user