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

103 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Providers;
2018-02-22 20:41:09 +08:00
use DB;
use View;
2016-11-21 21:50:24 +08:00
use Utils;
use Illuminate\Http\Request;
use Composer\Semver\Comparator;
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)
{
2018-02-16 17:31:04 +08:00
// Detect current locale
2017-01-18 22:31:35 +08:00
$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
2018-02-16 17:31:04 +08:00
// Skip the installation check when setup or under CLI
if (! $request->is('setup*') && PHP_SAPI != "cli") {
2017-01-18 22:31:35 +08:00
$this->checkInstallation();
}
}
protected function checkFilePermissions()
{
2018-02-16 17:31:04 +08:00
// Check dotenv file
if (! file_exists(app()->environmentFile())) {
throw new PrettyPageException(trans('setup.file.no-dot-env'), -1);
}
2018-02-16 17:31:04 +08:00
// Check permissions of storage path
if (! is_writable(storage_path())) {
2016-12-27 22:31:52 +08:00
throw new PrettyPageException(trans('setup.permissions.storage'), -1);
}
2018-02-16 17:31:04 +08:00
if (! SetupController::checkDirectories()) {
2017-01-18 22:31:35 +08:00
throw new PrettyPageException(trans('setup.file.permission-error'), -1);
}
}
protected function checkDatabaseConnection()
{
try {
2018-02-22 20:41:09 +08:00
DB::connection()->getPdo();
} catch (\Exception $e) {
2018-02-14 13:45:17 +08:00
if ($this->app->runningInConsole()) {
2018-02-16 17:31:04 +08:00
// Dump some useful information for debugging
2017-11-09 16:22:23 +08:00
dump([
'APP_ENV' => app()->environment(),
'DOTENV_FILE' => app()->environmentFile(),
2018-02-22 21:26:23 +08:00
'DB_CONNECTION' => get_db_config()
2017-11-09 16:22:23 +08:00
]);
}
2018-02-22 21:26:23 +08:00
$msg = iconv('gbk', 'utf-8', $e->getMessage());
$type = get_db_type();
2018-02-22 20:41:09 +08:00
throw new PrettyPageException(
2018-02-22 21:26:23 +08:00
trans('setup.database.connection-error', compact('msg', 'type')),
$e->getCode()
);
}
}
protected function checkInstallation()
{
2018-02-16 17:31:04 +08:00
// Redirect to setup wizard
if (! SetupController::checkTablesExist()) {
return redirect('/setup')->send();
}
if (Comparator::greaterThan(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);
}
}