ldc-legaulois/app/Providers/AppServiceProvider.php

131 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2020-07-23 10:29:36 +02:00
<?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;
2026-09-25 16:25:31 +02:00
use App\Lib\Axepta\AxeptaTokenManager;
use App\Models\GeneralSetting;
2024-04-18 12:05:10 +02:00
use App\Models\MenuNodeData;
use App\Providers\TelescopeServiceProvider as CustomTelescopeServiceProvider;
2026-09-25 16:25:31 +02:00
use Illuminate\Database\Eloquent\Model;
2024-01-10 14:49:46 +01:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
2026-09-25 16:25:31 +02:00
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Laravel\Telescope\TelescopeServiceProvider;
2024-06-28 10:06:04 +02:00
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
use Psr\Log\LoggerInterface;
2024-07-12 16:33:26 +02:00
use Snipe\BanBuilder\CensorWords;
2020-07-23 10:29:36 +02:00
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
2020-07-23 10:29:36 +02:00
{
2026-09-25 16:25:31 +02:00
Model::shouldBeStrict(! $this->app->environment('production'));
2024-01-10 14:49:46 +01:00
// * HTTPS FIX
2023-01-10 09:21:53 +01:00
URL::forceScheme('https');
2024-01-10 14:49:46 +01:00
// * HA PROXY FIX
App::singleton('clientIp', function () {
return (config('app.env') === 'local')
? request()->getClientIp()
: request()->server('HTTP_X_FORWARDED_FOR') ?? request()->getClientIp();
});
2024-01-10 14:49:46 +01:00
2022-04-01 11:40:12 +02:00
// * LOCAL FORCE eg: for Carbon
2024-01-10 14:49:46 +01:00
setlocale(LC_ALL, \sprintf(
'%s_%s.UTF-8',
config('app.locale'),
\strtoupper(config('app.locale'))
));
Carbon::setLocale(config('app.locale'));
// * SETTINGS
2026-09-25 16:25:31 +02:00
App::singleton('settings', fn () => GeneralSetting::getCached());
// * Menu
2026-09-25 16:25:31 +02:00
App::singleton('menuNodeList', fn () => App::get('settings')
2024-04-18 12:05:10 +02:00
->where('type', SettingsType::mainMenu)->firstOrFail());
2026-09-25 16:25:31 +02:00
App::singleton('menuNodeDataList', fn () => MenuNodeData::getCached());
2024-06-28 10:06:04 +02:00
2024-07-12 16:33:26 +02:00
// * Censor
2026-09-25 16:25:31 +02:00
App::singleton('censor', fn () => new CensorWords());
2024-07-12 16:33:26 +02:00
// * 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')
);
2026-09-25 16:25:31 +02:00
$gateway = new AxeptaApiGateway($config);
$tokenManager = new AxeptaTokenManager($gateway, $config);
$signatureVerifier = new AxeptaSignatureVerifier($config, $logger);
return new AxeptaPaymentService(
$logger,
$config,
$gateway,
$tokenManager,
$signatureVerifier
);
});
2024-06-28 10:06:04 +02:00
$this->setPaymentCurrency();
// * BLADE DIRECTIVES
Blade::if('isExport', function () {
return request()->routeIs('fo.*.export');
});
2024-06-28 10:06:04 +02:00
}
/**
* Register any application services.
*/
public function register(): void
{
if ($this->app->environment('local')) {
$this->app->register(TelescopeServiceProvider::class);
$this->app->register(CustomTelescopeServiceProvider::class);
}
}
2024-06-28 10:06:04 +02:00
/**
* Set formatters and payment currency.
2024-06-28 10:06:04 +02:00
*/
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);
2026-09-25 16:25:31 +02:00
2024-06-28 10:06:04 +02:00
return new IntlMoneyFormatter($numberFormatter, $currencies);
});
2020-07-23 10:29:36 +02:00
}
}