Rabbi_Gerzi/backend/app/Providers/AppServiceProvider.php

77 lines
2 KiB
PHP

<?php
namespace App\Providers;
use App\Auth\AuthCookieSettings;
use App\Auth\BcryptPasswordHasher;
use App\Auth\Clock;
use App\Auth\PasswordHasher;
use App\Auth\RandomTokenGenerator;
use App\Auth\SystemClock;
use App\Auth\TokenGenerator;
use App\Shared\Files\Filesystem;
use App\Shared\Files\LaravelFilesystem;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->bind(
PasswordHasher::class,
BcryptPasswordHasher::class,
);
$this->app->bind(
TokenGenerator::class,
RandomTokenGenerator::class,
);
$this->app->bind(
Clock::class,
SystemClock::class,
);
$this->app->bind(
Filesystem::class,
LaravelFilesystem::class,
);
$this->app->bind(
AuthCookieSettings::class,
function (): AuthCookieSettings {
$secureCookie = config('session.secure');
$sessionDomain = config('session.domain');
$sameSite = config('session.same_site');
return new AuthCookieSettings(
secure: $this->booleanConfig($secureCookie),
domain: $this->nullableStringConfig($sessionDomain),
sameSite: is_string($sameSite) ? $sameSite : 'lax',
);
}
);
}
private function booleanConfig(mixed $value): bool
{
if (is_bool($value)) {
return $value;
}
if (is_int($value)) {
return $value === 1;
}
if (is_string($value)) {
return in_array(strtolower($value), ['1', 'true'], true);
}
return false;
}
private function nullableStringConfig(mixed $value): ?string
{
if (! is_string($value) || trim($value) === '') {
return null;
}
return $value;
}
}