ldc-boutique-cnc/app/Providers/AppServiceProvider.php

263 lines
9.1 KiB
PHP
Raw Normal View History

2020-10-30 17:26:00 +01:00
<?php
namespace App\Providers;
use App\Enums\Order\PaymentModeEnum;
2023-04-19 12:55:33 +02:00
use App\Enums\StaticPageType;
use App\Models\Customer\Customer;
use App\Models\Merchandise\Product;
2023-03-01 11:02:42 +01:00
use App\Models\Shop\DeliveryMode;
use App\Models\Site\GeneralConfig;
use App\Models\Shop\PaymentMode;
use App\Models\Merchandise\Range;
use App\Models\Merchandise\Tag;
2023-04-19 12:55:33 +02:00
use App\Models\Site\StaticPage;
2022-11-23 11:10:40 +01:00
use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\View;
2020-10-30 17:26:00 +01:00
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
2022-10-03 17:13:44 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
2022-09-03 16:56:16 +02:00
use Illuminate\Support\Facades\URL;
2023-03-09 16:52:41 +01:00
use Illuminate\Support\Str;
use Laravel\Cashier\Cashier;
use Money\Currencies\ISOCurrencies;
2023-03-09 16:52:41 +01:00
use Money\Currency;
use Money\Formatter\IntlMoneyFormatter;
2020-10-30 17:26:00 +01:00
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
Cashier::ignoreMigrations();
}
2020-10-30 17:26:00 +01:00
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Prevent lazy loading, but only when the app is not in production.
Model::preventLazyLoading(config('app.env') !== 'production');
// * Morph aliases
Relation::morphMap([
'range' => Range::class,
'product' => Product::class,
]);
// Cashier shall use customers.
Cashier::useCustomerModel(Customer::class);
2022-10-03 17:13:44 +02:00
// * HTTPS FIX
2022-09-03 16:56:16 +02:00
URL::forceScheme('https');
2021-06-07 16:12:13 +02:00
2022-10-03 17:13:44 +02:00
// * 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();
}
});
2021-03-18 08:40:20 +01:00
// * Basket singleton
App::singleton('basket', function () {
return Cart::instance('basket');
});
$this->setAppLocaleAndPaymentCurrency();
2023-04-05 10:47:23 +02:00
$this->setCorsAllowedPaymentEndpoints();
$this->sharetoView();
}
/**
* Set app locale, formatters and payment currency
*
* @return void
*/
2023-04-05 10:47:23 +02:00
private function setAppLocaleAndPaymentCurrency(): void
{
2022-10-03 17:13:44 +02:00
// * 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'));
2022-09-03 16:56:16 +02:00
// * Php Money currency (This is used everywhere), default is Euro
2023-03-09 16:52:41 +01:00
App::singleton('phpMoneyCurrency', function () {
return new Currency(Str::upper('eur'));
2023-03-09 16:52:41 +01:00
});
// * 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);
});
// * Cashier currency locale
\config()->set('cashier.key', \sprintf(
'%s_%s',
config('app.locale'),
\strtoupper(config('app.locale'))
));
2022-11-23 11:10:40 +01:00
$this->setCashierStripeEnvVars();
}
2023-04-05 10:47:23 +02:00
/**
* Set cors allowed allowed_origins_patterns
* ! This is for payment mode
*
* @return void
*/
private function setCorsAllowedPaymentEndpoints(): void
{
$paymentModeList = \collect(PaymentModeEnum::toArray())
->map(function (PaymentModeEnum $paymentMode) {
return 'https:\/\/[a-z0-9\-\.]*\.' . \str_replace('.', '\.', $paymentMode->endpoint);
});
$allowed_origins_patterns = \collect(\config('cors.allowed_origins_patterns', []))->concat($paymentModeList);
\config()->set('cors.allowed_origins_patterns', $allowed_origins_patterns->all());
}
/**
* Inject cashier Stripe env vars
*
* @return void
*/
private function setCashierStripeEnvVars(): void
{
// * Set Cashier env variables from config.
/** @var \App\Models\Shop\PaymentMode|null $paymentModeSettings */
2023-03-17 17:32:18 +01:00
$paymentModeSettings = PaymentMode::getCached()->where('mode', PaymentModeEnum::stripe())->first();
if (!$paymentModeSettings) {
$message = 'Missing Stripe payment mode config';
Log::warning($message);
$previousMessage = session()->get('error', '');
session()->now('error', $message . " {$previousMessage}");
return;
}
$stripePublicKey = \strval($paymentModeSettings->data->payment_mode_stripe_key->value ?? '');
$stripeSecretKey = \strval($paymentModeSettings->data->payment_mode_stripe_secret->value ?? '');
$stripeSecretKeyWebhook = \strval($paymentModeSettings->data->payment_mode_stripe_webhook_secret->value ?? '');
if ($paymentModeSettings->status and empty($stripePublicKey)) {
$message = 'Missing Stripe payment public key config';
Log::warning($message);
$previousMessage = session()->get('error', '');
session()->now('error', $message . " {$previousMessage}");
return;
}
if ($paymentModeSettings->status and empty($stripeSecretKey)) {
$message = 'Missing Stripe payment private key config';
Log::warning($message);
$previousMessage = session()->get('error', '');
session()->now('error', $message . " {$previousMessage}");
return;
}
if ($paymentModeSettings->status and empty($stripeSecretKeyWebhook)) {
$message = 'Missing Stripe payment private webhook key config';
Log::warning($message);
$previousMessage = session()->get('error', '');
session()->now('error', $message . " {$previousMessage}");
return;
}
if (empty($stripePublicKey) or empty($stripeSecretKey) or empty($stripeSecretKeyWebhook)) {
return;
}
\config()->set('cashier.key', $stripePublicKey);
\config()->set('cashier.secret', $stripeSecretKey);
\config()->set('cashier.webhook.secret', $stripeSecretKeyWebhook);
2022-10-03 17:13:44 +02:00
}
/**
* Share Vars to view
*
* @return void
*/
2023-04-05 10:47:23 +02:00
private function sharetoView(): void
2022-10-03 17:13:44 +02:00
{
// * View shares
2023-02-24 09:31:39 +01:00
// ! Add view file pathes (also needed for console xml job).
View::addNamespace('back', resource_path('views'));
View::addNamespace('theme', resource_path('theme/views'));
2023-02-08 12:09:37 +01:00
2023-02-24 09:31:39 +01:00
if (!app()->runningInConsole()) {
2023-02-08 12:09:37 +01:00
$appInfos = Cache::remember(
'composer',
360,
function () {
return json_decode(File::get(\app_path('../composer.json')));
}
);
2022-10-03 17:13:44 +02:00
// Share composer.json infos .
2023-02-17 09:36:31 +01:00
View::share('cpsr_name', $appInfos->name);
View::share('cpsr_version', $appInfos->version);
View::share('cpsr_license', $appInfos->license);
View::share('cpsr_time', now()->parse($appInfos->time)->isoFormat('Y'));
2022-10-03 17:13:44 +02:00
try {
2023-03-17 17:32:18 +01:00
View::share('GeneralConfigGlobal', GeneralConfig::getCached());
2022-10-03 17:13:44 +02:00
} catch (\Exception $e) {
report($e);
View::share('GeneralConfigGlobal', collect());
} //end try
2023-02-08 12:09:37 +01:00
// * FORCE BOOTSTRAP PAGINATOR (or custom if in front)
// Early boot, request wont be filled as expected.
if (collect(explode('/', \request()->getPathInfo()))->get(1) === 'bo') {
Paginator::useBootstrap();
} else {
2023-02-15 09:09:06 +01:00
Paginator::defaultview('theme::modules.pagination');
2023-02-08 12:09:37 +01:00
}
// * Payment Modes
2023-03-17 17:32:18 +01:00
View::share('PaymentModesGlobal', PaymentMode::getCached());
\app()->singleton('PaymentModesGlobal', function () {
2023-03-17 17:32:18 +01:00
return PaymentMode::getCached();
});
2023-02-08 12:09:37 +01:00
// * Delivery Mode
2023-03-17 17:32:18 +01:00
View::share('DeliveryModesGlobal', DeliveryMode::getCached());
2023-02-08 12:09:37 +01:00
\app()->singleton('DeliveryModesGlobal', function () {
2023-03-17 17:32:18 +01:00
return DeliveryMode::getCached();
2023-02-08 12:09:37 +01:00
});
// * TAGS
2023-03-17 17:32:18 +01:00
View::share('PublishedTagsGlobal', Tag::getCached());
2023-02-08 12:09:37 +01:00
// * RANGES
2023-03-17 17:32:18 +01:00
View::share('PublishedRangesGlobal', Range::getCached());
2023-04-19 12:55:33 +02:00
// * CRUD STATIC PAGES
2023-04-26 15:25:34 +02:00
View::share('CrudStaticPagesGlobal', StaticPage::getCached()->filter(function (StaticPage $staticPage) {
return \in_array($staticPage->type, [
StaticPageType::crud(),
StaticPageType::contact()
]);
})->sortBy('type', \SORT_REGULAR, true));
2022-10-03 17:13:44 +02:00
} //end if
2020-10-30 17:26:00 +01:00
}
}