44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Models\Business;
|
|
use App\Models\BusinessDomain;
|
|
use App\Services\Tenant\TenantContext;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Throwable;
|
|
|
|
class ResolveTenant
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$hostname = strtolower(rtrim($request->getHost(), '.'));
|
|
try {
|
|
$business = BusinessDomain::query()
|
|
->where('hostname', $hostname)
|
|
->where('isActive', true)
|
|
->with('business')
|
|
->first()?->business;
|
|
|
|
if (! $business && app()->environment('local')) {
|
|
$slug = config('app.default_business_slug');
|
|
$business = $slug
|
|
? Business::query()->where('slug', $slug)->first()
|
|
: null;
|
|
}
|
|
} catch (Throwable) {
|
|
// The install page and framework smoke tests must remain
|
|
// reachable before the tenant schema has been created.
|
|
$business = null;
|
|
}
|
|
|
|
if ($business) {
|
|
app(TenantContext::class)->set($business);
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|