blessing-skin-server/app/Providers/BootServiceProvider.php

101 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Providers;
use View;
2016-11-21 21:50:24 +08:00
use Utils;
2016-12-17 22:56:00 +08:00
use App\Services\Database;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\PrettyPageException;
use App\Http\Controllers\SetupController;
2016-12-17 22:56:00 +08:00
use App\Services\Repositories\OptionRepository;
class BootServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Request $request)
{
2017-01-18 22:31:35 +08:00
// detect current locale
$this->app->call('App\Http\Middleware\DetectLanguagePrefer@detect');
2017-01-18 22:31:35 +08:00
$this->checkFilePermissions();
$this->checkDatabaseConnection();
2017-01-08 15:20:50 +08:00
2017-01-18 22:31:35 +08:00
// skip the installation check when setup or under CLI
if (!$request->is('setup*') && PHP_SAPI != "cli") {
$this->checkInstallation();
}
}
protected function checkFilePermissions()
{
// check dotenv
2017-11-09 16:32:18 +08:00
if (!file_exists(app()->environmentFile())) {
throw new PrettyPageException(trans('setup.file.no-dot-env'), -1);
}
2016-12-27 22:31:52 +08:00
// check permissions of storage path
if (!is_writable(storage_path())) {
throw new PrettyPageException(trans('setup.permissions.storage'), -1);
}
2017-01-18 22:31:35 +08:00
if (!SetupController::checkDirectories()) {
throw new PrettyPageException(trans('setup.file.permission-error'), -1);
}
}
protected function checkDatabaseConnection()
{
try {
// check database config
Database::prepareConnection();
} catch (\Exception $e) {
2018-02-14 13:45:17 +08:00
if ($this->app->runningInConsole()) {
2017-11-09 16:22:23 +08:00
// dump some useful information for debugging
dump([
'APP_ENV' => app()->environment(),
'DOTENV_FILE' => app()->environmentFile(),
'DB_CONNECTION' => config('database.connections.mysql')
]);
}
throw new PrettyPageException(
trans('setup.database.connection-error', ['msg' => $e->getMessage()]),
$e->getCode()
);
}
}
protected function checkInstallation()
{
// redirect to setup wizard
if (!SetupController::checkTablesExist()) {
return redirect('/setup')->send();
}
2017-01-02 16:23:31 +08:00
if (Utils::versionCompare(config('app.version'), option('version', ''), '>')) {
return redirect('/setup/update')->send();
}
return true;
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
2017-01-18 22:31:35 +08:00
View::addExtension('tpl', 'blade');
2016-12-17 22:56:00 +08:00
$this->app->singleton('options', OptionRepository::class);
$this->app->singleton('database', Database::class);
}
}