178 lines
5.3 KiB
PHP
178 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Contracts\Executor;
|
|
use App\Lib\PrivateS3Disk;
|
|
use App\Lib\PublicS3Disk;
|
|
use App\Services\ActionExecutorService;
|
|
use App\Services\DataTableService;
|
|
use App\Services\IcsGeneratorService;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Contracts\Session\Session;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Facades\Vite;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Mews\Purifier\Purifier;
|
|
use Shared\DDD\Equatable;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
#[\Override]
|
|
public function register()
|
|
{
|
|
$this->app->singleton(
|
|
Purifier::class,
|
|
fn () => resolve('purifier')
|
|
);
|
|
|
|
$this->app->singleton('clientIp', function () {
|
|
$realIp = request()->header('X-Real-Ip');
|
|
|
|
if (\is_string($realIp) && \filter_var($realIp, \FILTER_VALIDATE_IP) !== false) {
|
|
return $realIp;
|
|
}
|
|
|
|
return request()->ip() ?? request()->getClientIp();
|
|
});
|
|
|
|
$this->app->singleton(
|
|
PrivateS3Disk::class,
|
|
fn () => new PrivateS3Disk()
|
|
);
|
|
$this->app->singleton(
|
|
PublicS3Disk::class,
|
|
fn () => new PublicS3Disk(
|
|
Config::string('filesystems.disks.s3.url')
|
|
)
|
|
);
|
|
|
|
$this->app->bind(Executor::class, ActionExecutorService::class);
|
|
|
|
$this->app->singleton(
|
|
IcsGeneratorService::class,
|
|
fn (): IcsGeneratorService => new IcsGeneratorService(
|
|
Config::string('app.name', 'La Boutique de Cocotte'),
|
|
)
|
|
);
|
|
|
|
$this->app->bind(
|
|
DataTableService::class,
|
|
fn (Application $app): DataTableService => new DataTableService(
|
|
request: $app->make(Request::class),
|
|
session: $app->make(Session::class),
|
|
columnListingResolver: static fn (string $table): array => Schema::getColumnListing($table),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
$this->laravelGlobalSetup();
|
|
$this->dateSetup();
|
|
$this->dateMacro();
|
|
$this->collectionMacro();
|
|
}
|
|
|
|
private function laravelGlobalSetup(): void
|
|
{
|
|
Model::shouldBeStrict(app()->environment('local'));
|
|
|
|
Vite::useCspNonce();
|
|
|
|
URL::forceScheme('https');
|
|
}
|
|
|
|
private function dateSetup(): void
|
|
{
|
|
foreach ([\LC_TIME, \LC_MESSAGES, \LC_COLLATE, \LC_CTYPE, \LC_MONETARY] as $constant) {
|
|
setlocale(
|
|
$constant,
|
|
sprintf(
|
|
'%s_%s.UTF-8',
|
|
config()->string('app.locale'),
|
|
strtoupper(config()->string('app.locale'))
|
|
)
|
|
);
|
|
}
|
|
Date::setLocale(config()->string('app.locale'));
|
|
Date::setFallbackLocale(config()->string('app.fallback_locale'));
|
|
}
|
|
|
|
private function dateMacro(): void
|
|
{
|
|
Date::macro('toLocalInput', function ($date) {
|
|
if (! $date || (! $date instanceof CarbonInterface && ! $date instanceof \DateTimeInterface && ! is_string($date))) {
|
|
return null;
|
|
}
|
|
|
|
return $this->withLocalTimezone($date)->format('Y-m-d\TH:i');
|
|
});
|
|
|
|
Date::macro('forHumans', function ($date) {
|
|
if (! $date || (! $date instanceof CarbonInterface && ! $date instanceof \DateTimeInterface && ! is_string($date))) {
|
|
return null;
|
|
}
|
|
|
|
return $this->withLocalTimezone($date)->isoFormat('ddd DD/MM/Y \à HH:mm');
|
|
});
|
|
|
|
Date::macro('forHumansDate', function ($date) {
|
|
if (! $date || (! $date instanceof CarbonInterface && ! $date instanceof \DateTimeInterface && ! is_string($date))) {
|
|
return null;
|
|
}
|
|
|
|
$parsed = $date instanceof \DateTimeInterface
|
|
? Date::instance($date)
|
|
: Date::parse($date, 'UTC');
|
|
|
|
return $parsed->isoFormat('ddd DD/MM/Y');
|
|
});
|
|
}
|
|
|
|
private function withLocalTimezone(CarbonInterface | \DateTimeInterface | string $date): CarbonInterface
|
|
{
|
|
$timezone = request()->string('client_timezone', 'Europe/Paris')->value();
|
|
|
|
if (! in_array($timezone, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC), true)) {
|
|
$timezone = 'Europe/Paris';
|
|
}
|
|
|
|
return Date::parse($date)->setTimezone($timezone);
|
|
}
|
|
|
|
private function collectionMacro(): void
|
|
{
|
|
Collection::macro('uniqueValues', function () {
|
|
return $this->unique(function ($item) {
|
|
if ($item instanceof Equatable) {
|
|
return $item instanceof \Stringable ? (string) $item : json_encode($item);
|
|
}
|
|
if ($item instanceof \Stringable) {
|
|
return (string) $item;
|
|
}
|
|
|
|
return $item;
|
|
});
|
|
});
|
|
}
|
|
}
|