2016-08-28 18:33:47 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2016-09-10 19:06:54 +08:00
|
|
|
use View;
|
2016-11-21 21:50:24 +08:00
|
|
|
use Utils;
|
2016-12-17 22:56:00 +08:00
|
|
|
use App\Services\Database;
|
2016-11-18 16:46:58 +08:00
|
|
|
use Illuminate\Http\Request;
|
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
|
|
|
{
|
2017-01-18 22:31:35 +08:00
|
|
|
// detect current locale
|
|
|
|
$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
|
|
|
|
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()
|
|
|
|
{
|
2016-12-18 17:32:46 +08:00
|
|
|
// check dotenv
|
2017-11-09 16:32:18 +08:00
|
|
|
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
|
|
|
|
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()
|
|
|
|
{
|
2016-12-18 17:32:46 +08:00
|
|
|
try {
|
|
|
|
// check database config
|
|
|
|
Database::prepareConnection();
|
|
|
|
} catch (\Exception $e) {
|
2017-11-09 16:22:23 +08:00
|
|
|
if (PHP_SAPI == "cli") {
|
|
|
|
// dump some useful information for debugging
|
|
|
|
dump([
|
|
|
|
'APP_ENV' => app()->environment(),
|
|
|
|
'DOTENV_FILE' => app()->environmentFile(),
|
|
|
|
'DB_CONNECTION' => config('database.connections.mysql')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:46:58 +08:00
|
|
|
throw new PrettyPageException(
|
2016-12-18 17:32:46 +08:00
|
|
|
trans('setup.database.connection-error', ['msg' => $e->getMessage()]),
|
|
|
|
$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()
|
|
|
|
{
|
2016-11-18 16:46:58 +08:00
|
|
|
// redirect to setup wizard
|
2016-12-18 17:32:46 +08:00
|
|
|
if (!SetupController::checkTablesExist()) {
|
2016-11-18 16:46:58 +08:00
|
|
|
return redirect('/setup')->send();
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
|
2017-01-02 16:23:31 +08:00
|
|
|
if (Utils::versionCompare(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);
|
|
|
|
$this->app->singleton('database', Database::class);
|
2016-08-28 18:33:47 +08:00
|
|
|
}
|
|
|
|
}
|