ldc-legaulois/app/Providers/AppServiceProvider.php
2026-09-25 16:25:31 +02:00

130 lines
4.2 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\Settings\SettingsType;
use App\Lib\Axepta\AxeptaApiGateway;
use App\Lib\Axepta\AxeptaConfig;
use App\Lib\Axepta\AxeptaPaymentService;
use App\Lib\Axepta\AxeptaSignatureVerifier;
use App\Lib\Axepta\AxeptaTokenManager;
use App\Models\GeneralSetting;
use App\Models\MenuNodeData;
use App\Providers\TelescopeServiceProvider as CustomTelescopeServiceProvider;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Laravel\Telescope\TelescopeServiceProvider;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
use Psr\Log\LoggerInterface;
use Snipe\BanBuilder\CensorWords;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Model::shouldBeStrict(! $this->app->environment('production'));
// * HTTPS FIX
URL::forceScheme('https');
// * HA PROXY FIX
App::singleton('clientIp', function () {
return (config('app.env') === 'local')
? request()->getClientIp()
: 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'));
// * SETTINGS
App::singleton('settings', fn () => GeneralSetting::getCached());
// * Menu
App::singleton('menuNodeList', fn () => App::get('settings')
->where('type', SettingsType::mainMenu)->firstOrFail());
App::singleton('menuNodeDataList', fn () => MenuNodeData::getCached());
// * Censor
App::singleton('censor', fn () => new CensorWords());
// * Axepta
$this->app->singleton(AxeptaPaymentService::class, function () {
$logger = resolve(LoggerInterface::class);
$config = new AxeptaConfig(
endpoint: Config::string('axepta.endpoint'),
merchantId: Config::string('axepta.merchantId'),
apiKey: Config::string('axepta.apiKey'),
hmac: Config::string('axepta.hmac')
);
$gateway = new AxeptaApiGateway($config);
$tokenManager = new AxeptaTokenManager($gateway, $config);
$signatureVerifier = new AxeptaSignatureVerifier($config, $logger);
return new AxeptaPaymentService(
$logger,
$config,
$gateway,
$tokenManager,
$signatureVerifier
);
});
$this->setPaymentCurrency();
// * BLADE DIRECTIVES
Blade::if('isExport', function () {
return request()->routeIs('fo.*.export');
});
}
/**
* Register any application services.
*/
public function register(): void
{
if ($this->app->environment('local')) {
$this->app->register(TelescopeServiceProvider::class);
$this->app->register(CustomTelescopeServiceProvider::class);
}
}
/**
* Set formatters and payment currency.
*/
private function setPaymentCurrency(): void
{
// * Php Money currency (This is used everywhere), default is Euro
App::singleton('phpMoneyCurrency', function () {
return new Currency(config('money.currency'));
});
// * Php Money formater (using app locale)
App::singleton('phpMoneyFormatter', function () {
// * Number format using app locale.
$currencies = new ISOCurrencies();
$numberFormatter = new \NumberFormatter(\sprintf(
'%s_%s',
config('app.locale'),
\strtoupper(config('app.locale'))
), \NumberFormatter::CURRENCY);
return new IntlMoneyFormatter($numberFormatter, $currencies);
});
}
}