84 lines
3.1 KiB
PHP
84 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Domain\DomainContext;
|
|
use App\Services\DomainService;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
/**
|
|
* ## Vorschlag für einen überarbeiteten DomainServiceProvider
|
|
*
|
|
* In dieser Version wird die Middleware **nicht mehr** hier registriert.
|
|
* Die Registrierung der Middleware sollte zentral in `app/Http/Kernel.php`
|
|
* in der korrekten Reihenfolge erfolgen, um Session-Konflikte zu vermeiden.
|
|
*/
|
|
class DomainServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Registriert die Domain-Dienste im Service-Container.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
// 1. DomainService als Singleton registrieren.
|
|
// Diese Logik ist gut und bleibt unverändert.
|
|
$this->app->singleton(DomainService::class, function ($app) {
|
|
$domainService = new DomainService($app['config']['domains']);
|
|
|
|
// Validiere Konfiguration in der Development-Umgebung
|
|
if (config('app.debug')) {
|
|
$configErrors = $domainService->validateConfiguration();
|
|
if (!empty($configErrors)) {
|
|
\Log::channel('domain')->warning('Domain configuration errors detected', ['errors' => $configErrors]);
|
|
}
|
|
}
|
|
|
|
return $domainService;
|
|
});
|
|
|
|
// 2. DomainContext als Singleton registrieren.
|
|
// Die Logik hier wird nur einmal pro Anfrage ausgeführt (lazy-loaded),
|
|
// wenn der Context das erste Mal benötigt wird. Das ist effizient und korrekt.
|
|
$this->app->singleton(DomainContext::class, function ($app) {
|
|
/** @var DomainService $domainService */
|
|
$domainService = $app->make(DomainService::class);
|
|
$request = $app->make('request');
|
|
|
|
// Analysiere den Host der aktuellen Anfrage
|
|
$domainInfo = $domainService->parseDomain($request->getHost());
|
|
|
|
$userShop = null;
|
|
|
|
// Wenn es sich um eine User-Shop-Domain handelt, versuche das Shop-Objekt zu finden.
|
|
if ($domainInfo['type'] === 'user-shop' && $domainInfo['subdomain']) {
|
|
$userShop = $domainService->getUserShop($domainInfo['subdomain']);
|
|
// Wenn der Shop ungültig ist, wird der Typ auf 'unknown' gesetzt.
|
|
if (!$userShop) {
|
|
$domainInfo['type'] = 'unknown';
|
|
}
|
|
}
|
|
|
|
// Wenn es sich um die Haupt-Shop-Domain handelt (z.B. mivita.shop),
|
|
// lade den konfigurierten Fallback-Shop.
|
|
if ($domainInfo['type'] === 'main-shop' && !empty($domainInfo['default_user_shop'])) {
|
|
$userShop = $domainService->getUserShop($domainInfo['default_user_shop']);
|
|
}
|
|
|
|
return DomainContext::fromArray($domainInfo, $userShop);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Führt Aktionen nach der Registrierung aller Provider aus.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
// Die Middleware-Registrierung wird von hier entfernt.
|
|
// $kernel = $this->app->make(\Illuminate\Contracts\Http\Kernel::class);
|
|
// $kernel->prependMiddlewareToGroup('web', DomainResolver::class);
|
|
}
|
|
}
|