header = $header; $this->queryParameter = $queryParameter; $this->onFail = $onFail ?? function ($e) { throw $e; }; } /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next, TenantManager $tenantManager) { if ($request->method() !== 'OPTIONS') { try { $this->initializeTenancy($request, $tenantManager); } catch (TenantCouldNotBeIdentifiedException $e) { ($this->onFail)($e); } } return $next($request); } protected function initializeTenancy(Request $request, TenantManager $tenancy) { if ($tenancy->initialized) { return; } $tenant = null; if ($this->header && $request->hasHeader($this->header)) { $tenant = $request->header($this->header); } elseif ($this->queryParameter && $request->has($this->queryParameter)) { $tenant = $request->get($this->queryParameter); } if (! $tenant) { throw new TenantCouldNotBeIdentifiedException($request->getHost()); } $tenancy->initialize($tenancy->find($tenant)); } }