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; }); }); } }