ldc-gen-game-web/app/Providers/AppServiceProvider.php

116 lines
3.8 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Prize;
use App\Models\Settings;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\ValidationException;
use LDC\LDCCRM\Enums\Country;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
* @throws \ErrorException If MAIL_ADMIN is not set in .env .
*/
public function boot()
{
// * LOCAL HTTPS FIX.
if (!in_array(config('app.env'), ['local', 'testing'])) {
URL::forceScheme('https');
}
// * HA PROXY FIX.
App::singleton('clientIp', function () {
if (
config('app.env') === 'local'
) {
return request()->getClientIp();
} else {
return request()->server('HTTP_X_FORWARDED_FOR') ??
request()->getClientIp();
}
});
// * LOCAL FORCE eg: for Carbon.
setlocale(LC_ALL, \sprintf(
'%s_%s.UTF-8',
config('app.locale'),
\strtoupper(config('app.locale'))
));
Carbon::setLocale(config('app.locale'));
// * FORCE BOOTSTRAP PAGINATOR.
if (!app()->runningInConsole()) {
Paginator::useBootstrap();
}
app()->singleton(Settings::class, function () {
if (!($settings = Settings::find(1))) {
throw new \ErrorException('Settings not found in DB');
}
return $settings;
});
// * AvailableCountries
View::share('availableCountries', [
Country::france(),
Country::belgium(),
Country::luxembourg(),
Country::guadeloupe(),
Country::martinique(),
Country::frenchGuiana(),
Country::reunion(),
Country::saintPierreAndMiquelon(),
Country::mayotte(),
Country::saintBarthelemy(),
Country::saintMartinFrenchPart(),
Country::wallisAndFutuna(),
Country::frenchPolynesia(),
Country::newCaledonia()
]);
if (!app()->runningInConsole()) {
// ! MAIL_ADMIN est obligatoire dans le .env
try {
Validator::validate(['mail' => config('mail.MAIL_ADMIN')], [
'mail' => 'required|email:rfc,strict,spoof'
]);
} catch (ValidationException $e) {
throw new \ErrorException(
'La valeur mail doit contenir une adresse email valide dans la configuration.'
);
}
// On ramène le nom du jeu et le nom de la société sur toute les pages.
$settingsGlobal = Cache::get('settingsGlobal');
if (!$settingsGlobal) {
$settingsGlobal = app(Settings::class)
->select('name', 'company', 'analytics', 'start_date', 'end_date')
->first();
Cache::put('settingsGlobal', $settingsGlobal);
}
View::share('settingsGlobal', $settingsGlobal);
// On ramène aussi tout les prix car on en a besoin sur le header.
View::share('prizesGlobal', Cache::remember('prizesGlobal', 60, function () {
return Prize::all();
}));
Blade::if('superadmin', function () {
/** @var \App\Models\User|null $user */
$user = auth('web')->user();
return $user and $user->isAdmin();
});
}//end if
}
}