2016-08-28 18:33:47 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2018-02-22 20:41:09 +08:00
|
|
|
use DB;
|
2016-09-10 19:06:54 +08:00
|
|
|
use View;
|
2016-11-21 21:50:24 +08:00
|
|
|
use Utils;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-06-30 16:31:39 +08:00
|
|
|
use Composer\Semver\Comparator;
|
2016-08-28 18:33:47 +08:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2016-08-29 19:47:30 +08:00
|
|
|
use App\Exceptions\PrettyPageException;
|
2016-12-18 17:32:46 +08:00
|
|
|
use App\Http\Controllers\SetupController;
|
2016-12-17 22:56:00 +08:00
|
|
|
use App\Services\Repositories\OptionRepository;
|
2016-08-28 18:33:47 +08:00
|
|
|
|
|
|
|
class BootServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-11-18 16:46:58 +08:00
|
|
|
public function boot(Request $request)
|
2016-08-28 18:33:47 +08:00
|
|
|
{
|
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');
|
2016-09-10 19:06:54 +08:00
|
|
|
|
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())) {
|
2016-11-18 16:46:58 +08:00
|
|
|
throw new PrettyPageException(trans('setup.file.no-dot-env'), -1);
|
2016-08-29 19:47:30 +08:00
|
|
|
}
|
2016-08-28 18:33:47 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2016-12-18 17:32:46 +08:00
|
|
|
try {
|
2018-02-22 20:41:09 +08:00
|
|
|
DB::connection()->getPdo();
|
2016-12-18 17:32:46 +08:00
|
|
|
} 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
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
throw new PrettyPageException(
|
2018-02-22 21:26:23 +08:00
|
|
|
trans('setup.database.connection-error', compact('msg', 'type')),
|
2016-12-18 17:32:46 +08:00
|
|
|
$e->getCode()
|
2016-11-18 16:46:58 +08:00
|
|
|
);
|
2016-12-18 17:32:46 +08:00
|
|
|
}
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function checkInstallation()
|
|
|
|
{
|
2018-02-16 17:31:04 +08:00
|
|
|
// Redirect to setup wizard
|
|
|
|
if (! SetupController::checkTablesExist()) {
|
2016-11-18 16:46:58 +08:00
|
|
|
return redirect('/setup')->send();
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:31:39 +08:00
|
|
|
if (Comparator::greaterThan(config('app.version'), option('version'))) {
|
2016-11-18 16:46:58 +08:00
|
|
|
return redirect('/setup/update')->send();
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
}
|