ldc-cocotte/app/Providers/AppServiceProvider.php

179 lines
5.3 KiB
PHP
Raw Permalink Normal View History

2020-10-30 17:26:00 +01:00
<?php
2026-06-14 21:32:07 +02:00
declare(strict_types=1);
2020-10-30 17:26:00 +01:00
namespace App\Providers;
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
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;
2026-06-14 21:32:07 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Schema;
2022-09-08 13:02:16 +02:00
use Illuminate\Support\Facades\URL;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\ServiceProvider;
use Mews\Purifier\Purifier;
use Shared\DDD\Equatable;
2020-10-30 17:26:00 +01:00
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
2026-06-14 21:32:07 +02:00
#[\Override]
2020-10-30 17:26:00 +01:00
public function register()
{
2026-06-14 21:32:07 +02:00
$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();
2026-06-14 21:32:07 +02:00
});
$this->app->singleton(
PrivateS3Disk::class,
fn () => new PrivateS3Disk()
);
$this->app->singleton(
PublicS3Disk::class,
fn () => new PublicS3Disk(
Config::string('filesystems.disks.s3.url')
)
);
feat: implement pagination using Pagination value object - Updated DataTableService to accept a Pagination object for pagination parameters. - Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination. refactor: replace ActionExecutor with Executor in controller traits - Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor. feat: add PictureMetadata and Slug value objects - Introduced PictureMetadata for managing image metadata with validation. - Added Slug value object for slug generation and validation. feat: update breadcrumbs to support RangeDto - Modified breadcrumbs-bo.php to accept RangeDto in the edit route. test: add tests for RangesController - Added tests for accessing, creating, updating, and deleting ranges. - Included tests for toggling range status. test: add integration tests for range actions - Created tests for CreateRange, DeleteRange, and UpdateRange actions. - Validated input handling and database interactions. test: add unit tests for range input classes - Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints. test: add unit tests for value objects - Added tests for PictureMetadata and Slug to ensure proper validation and instantiation. test: add unit tests for RangeDto - Created tests to verify RangeDto mapping from Range model. test: add unit tests for RangePolicy - Implemented tests for RangePolicy to validate permissions based on user roles.
2026-09-29 16:59:07 +02:00
$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),
)
);
2020-10-30 17:26:00 +01:00
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
2026-06-14 21:32:07 +02:00
$this->laravelGlobalSetup();
$this->dateSetup();
$this->dateMacro();
$this->collectionMacro();
}
2021-06-07 16:12:13 +02:00
2026-06-14 21:32:07 +02:00
private function laravelGlobalSetup(): void
{
Model::shouldBeStrict(app()->environment('local'));
2021-03-18 08:40:20 +01:00
2026-06-14 21:32:07 +02:00
Vite::useCspNonce();
2026-06-14 21:32:07 +02:00
URL::forceScheme('https');
}
2026-06-14 21:32:07 +02:00
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'))
)
);
2022-09-12 14:26:42 +02:00
}
2026-06-14 21:32:07 +02:00
Date::setLocale(config()->string('app.locale'));
Date::setFallbackLocale(config()->string('app.fallback_locale'));
}
2021-05-06 17:18:25 +02:00
2026-06-14 21:32:07 +02:00
private function dateMacro(): void
{
Date::macro('toLocalInput', function ($date) {
if (! $date || (! $date instanceof CarbonInterface && ! $date instanceof \DateTimeInterface && ! is_string($date))) {
2026-06-14 21:32:07 +02:00
return null;
}
return $this->withLocalTimezone($date)->format('Y-m-d\TH:i');
2022-10-14 14:52:56 +02:00
});
2026-06-14 21:32:07 +02:00
Date::macro('forHumans', function ($date) {
if (! $date || (! $date instanceof CarbonInterface && ! $date instanceof \DateTimeInterface && ! is_string($date))) {
2026-06-14 21:32:07 +02:00
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');
});
2026-06-14 21:32:07 +02:00
}
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);
}
2026-06-14 21:32:07 +02:00
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;
}
2026-06-14 21:32:07 +02:00
return $item;
});
});
2020-10-30 17:26:00 +01:00
}
}