1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 02:54:03 +00:00

Changed to Illuminate request instead of helper

This commit is contained in:
Jesper Jacobsen 2019-10-27 11:40:25 +01:00
parent 432dd8fd1d
commit b8d2c0f4db

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Middleware; namespace Stancl\Tenancy\Middleware;
use Closure; use Closure;
use Illuminate\Http\Request;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
class InitializeTenancyByRequestData class InitializeTenancyByRequestData
@ -28,9 +29,9 @@ class InitializeTenancyByRequestData
*/ */
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
if (request()->method() !== 'OPTIONS') { if ($request->method() !== 'OPTIONS') {
try { try {
$this->parseTenant(); $this->parseTenant($request);
} catch (TenantCouldNotBeIdentifiedException $e) { } catch (TenantCouldNotBeIdentifiedException $e) {
($this->onFail)($e); ($this->onFail)($e);
} }
@ -39,23 +40,23 @@ class InitializeTenancyByRequestData
return $next($request); return $next($request);
} }
protected function parseTenant() protected function parseTenant(Request $request)
{ {
if (tenancy()->initialized) { if (tenancy()->initialized) {
return; return;
} }
$tenant = null; $tenant = null;
if (request()->hasHeader('X-Tenant')) { if ($request->hasHeader('X-Tenant')) {
$tenant = request()->header('X-Tenant'); $tenant = $request->header('X-Tenant');
} elseif (request()->has('_tenant')) { } elseif ($request->has('_tenant')) {
$tenant = request()->get('_tenant'); $tenant = $request->get('_tenant');
} elseif (! in_array(request()->getHost(), config('tenancy.exempt_domains', []), true)) { } elseif (! in_array($request->getHost(), config('tenancy.exempt_domains', []), true)) {
$tenant = explode('.', request()->getHost())[0]; $tenant = explode('.', $request->getHost())[0];
} }
if (! $tenant) { if (! $tenant) {
throw new TenantCouldNotBeIdentifiedException(request()->getHost()); throw new TenantCouldNotBeIdentifiedException($request->getHost());
} }
tenancy()->initialize(tenancy()->find($tenant)); tenancy()->initialize(tenancy()->find($tenant));