From 9c9b2da6182d5ce98303cc0127946751d43f0d54 Mon Sep 17 00:00:00 2001 From: Ralfs Date: Mon, 12 Apr 2021 12:22:34 +0300 Subject: [PATCH 1/8] Instead of querying Domain model, find Tenant and eager load it's domain via Tenant model. Fixed cached lookup issue - when caching Tenant, also include the current Domain, so it can be later accessed via $tenant->domains->first() (even, when using multiple domains per tenant). Added tenantIdentifiedFromCache method in CachedTenantResolver.php, which can be used to set custom properties in resolvers after Tenant is loaded from cache. --- .../Contracts/CachedTenantResolver.php | 9 ++++++- src/Resolvers/DomainTenantResolver.php | 25 ++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/Contracts/CachedTenantResolver.php b/src/Resolvers/Contracts/CachedTenantResolver.php index f25802aa6..986f38328 100644 --- a/src/Resolvers/Contracts/CachedTenantResolver.php +++ b/src/Resolvers/Contracts/CachedTenantResolver.php @@ -37,7 +37,10 @@ public function resolve(...$args): Tenant $key = $this->getCacheKey(...$args); if ($this->cache->has($key)) { - return $this->cache->get($key); + $tenant = $this->cache->get($key); + $this->tenantIdentifiedFromCache($tenant, ...$args); + + return $tenant; } $tenant = $this->resolveWithoutCache(...$args); @@ -64,6 +67,10 @@ public function getCacheKey(...$args): string abstract public function resolveWithoutCache(...$args): Tenant; + public function tenantIdentifiedFromCache(Tenant $tenant, ...$args): void + { + } + /** * Get all the arg combinations for resolve() that can be used to find this tenant. * diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 8e3857f37..6a3bcadb5 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -28,18 +28,37 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver public function resolveWithoutCache(...$args): Tenant { - /** @var Domain $domain */ - $domain = config('tenancy.domain_model')::where('domain', $args[0])->first(); + $domain = $args[0]; + + /** @var Tenant|null $tenant */ + $tenant = config('tenancy.tenant_model')::query() + ->whereHas('domains', function ($query) use ($domain) { + $query->select(['tenant_id', 'domain'])->where('domain', $domain); + }) + ->with([ + 'domains' => function ($query) use ($domain) { + $query->where('domain', $domain); + }, + ]) + ->first(); + + /** @var Domain|null $domain */ + $domain = $tenant?->domains->first(); if ($domain) { static::$currentDomain = $domain; - return $domain->tenant; + return $tenant; } throw new TenantCouldNotBeIdentifiedOnDomainException($args[0]); } + public function tenantIdentifiedFromCache(Tenant $tenant, ...$args): void + { + static::$currentDomain = $tenant->domains->first(); + } + public function getArgsForTenant(Tenant $tenant): array { $tenant->unsetRelation('domains'); From 3145d8330d7c50162650fc06b830f635263ad4ba Mon Sep 17 00:00:00 2001 From: Ralfs Date: Mon, 12 Apr 2021 12:50:09 +0300 Subject: [PATCH 2/8] StlyeCi Fix - removed PHP 8 nullsafe operator for compatibility with older PHP versions, replaced with inline if --- src/Resolvers/DomainTenantResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 6a3bcadb5..2cc9479a4 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -43,7 +43,7 @@ public function resolveWithoutCache(...$args): Tenant ->first(); /** @var Domain|null $domain */ - $domain = $tenant?->domains->first(); + $domain = $tenant ? $tenant->domains->first() : null; if ($domain) { static::$currentDomain = $domain; From 1d3bb6dc270f80da8d70d2cb3aaa8eb7ec4e9a5c Mon Sep 17 00:00:00 2001 From: Ralfs Date: Mon, 12 Apr 2021 15:39:21 +0300 Subject: [PATCH 3/8] Redundant variable '$domain', because $tenant is not null, only, when current domain is found and relationship is loaded (with only one domain). --- src/Resolvers/DomainTenantResolver.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 2cc9479a4..c4bfd382a 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -42,11 +42,8 @@ public function resolveWithoutCache(...$args): Tenant ]) ->first(); - /** @var Domain|null $domain */ - $domain = $tenant ? $tenant->domains->first() : null; - - if ($domain) { - static::$currentDomain = $domain; + if ($tenant) { + static::$currentDomain = $tenant->domains->first(); return $tenant; } From e4a3211b3404a30ae2cc57befa779fa78c538696 Mon Sep 17 00:00:00 2001 From: Ralfs Date: Mon, 12 Apr 2021 23:53:15 +0300 Subject: [PATCH 4/8] Fixed tenant()->domains showing incorrect data. Renamed tenantIdentifiedFromCache() method and removed duplicate code, when setting current domain. --- src/Resolvers/Contracts/CachedTenantResolver.php | 4 ++-- src/Resolvers/DomainTenantResolver.php | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Resolvers/Contracts/CachedTenantResolver.php b/src/Resolvers/Contracts/CachedTenantResolver.php index 986f38328..675625ce3 100644 --- a/src/Resolvers/Contracts/CachedTenantResolver.php +++ b/src/Resolvers/Contracts/CachedTenantResolver.php @@ -38,7 +38,7 @@ public function resolve(...$args): Tenant if ($this->cache->has($key)) { $tenant = $this->cache->get($key); - $this->tenantIdentifiedFromCache($tenant, ...$args); + $this->tenantIdentified($tenant, ...$args); return $tenant; } @@ -67,7 +67,7 @@ public function getCacheKey(...$args): string abstract public function resolveWithoutCache(...$args): Tenant; - public function tenantIdentifiedFromCache(Tenant $tenant, ...$args): void + public function tenantIdentified(Tenant $tenant, ...$args): void { } diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index c4bfd382a..524acf27d 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -35,15 +35,11 @@ public function resolveWithoutCache(...$args): Tenant ->whereHas('domains', function ($query) use ($domain) { $query->select(['tenant_id', 'domain'])->where('domain', $domain); }) - ->with([ - 'domains' => function ($query) use ($domain) { - $query->where('domain', $domain); - }, - ]) + ->with('domains') ->first(); if ($tenant) { - static::$currentDomain = $tenant->domains->first(); + $this->tenantIdentified($tenant, ...$args); return $tenant; } @@ -51,9 +47,9 @@ public function resolveWithoutCache(...$args): Tenant throw new TenantCouldNotBeIdentifiedOnDomainException($args[0]); } - public function tenantIdentifiedFromCache(Tenant $tenant, ...$args): void + public function tenantIdentified(Tenant $tenant, ...$args): void { - static::$currentDomain = $tenant->domains->first(); + static::$currentDomain = $tenant->domains->where('domain', $args[0])->first(); } public function getArgsForTenant(Tenant $tenant): array From 643a64b5c855fb972de463de277d9bd1eb3446a7 Mon Sep 17 00:00:00 2001 From: Ralfs Date: Tue, 13 Apr 2021 22:18:53 +0300 Subject: [PATCH 5/8] Removed select() for better flexibility, added new method setCurrentDomain(), refactored the usage of tenantIdentified(). --- src/Resolvers/DomainTenantResolver.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 524acf27d..82ddcbeee 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -33,13 +33,13 @@ public function resolveWithoutCache(...$args): Tenant /** @var Tenant|null $tenant */ $tenant = config('tenancy.tenant_model')::query() ->whereHas('domains', function ($query) use ($domain) { - $query->select(['tenant_id', 'domain'])->where('domain', $domain); + $query->where('domain', $domain); }) ->with('domains') ->first(); if ($tenant) { - $this->tenantIdentified($tenant, ...$args); + $this->setCurrentDomain($tenant, ...$args); return $tenant; } @@ -48,6 +48,11 @@ public function resolveWithoutCache(...$args): Tenant } public function tenantIdentified(Tenant $tenant, ...$args): void + { + $this->setCurrentDomain($tenant, ...$args); + } + + protected function setCurrentDomain(Tenant $tenant, ...$args): void { static::$currentDomain = $tenant->domains->where('domain', $args[0])->first(); } From 2aec8417e9dcadd6ebc47bc38e2e03a3ee122053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 13 Apr 2021 22:58:48 +0200 Subject: [PATCH 6/8] rename method to resolved() --- src/Resolvers/Contracts/CachedTenantResolver.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Resolvers/Contracts/CachedTenantResolver.php b/src/Resolvers/Contracts/CachedTenantResolver.php index 675625ce3..968ac7942 100644 --- a/src/Resolvers/Contracts/CachedTenantResolver.php +++ b/src/Resolvers/Contracts/CachedTenantResolver.php @@ -38,7 +38,8 @@ public function resolve(...$args): Tenant if ($this->cache->has($key)) { $tenant = $this->cache->get($key); - $this->tenantIdentified($tenant, ...$args); + + $this->resolved($tenant, ...$args); return $tenant; } @@ -67,7 +68,7 @@ public function getCacheKey(...$args): string abstract public function resolveWithoutCache(...$args): Tenant; - public function tenantIdentified(Tenant $tenant, ...$args): void + public function resolved(Tenant $tenant, ...$args): void { } From c436b06cba709a391a8aa37f6eb44ce15512f699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 13 Apr 2021 23:00:26 +0200 Subject: [PATCH 7/8] clean up code --- src/Resolvers/DomainTenantResolver.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 82ddcbeee..6ba72f1f2 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -7,6 +7,7 @@ use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; +use Illuminate\Database\Eloquent\Builder; class DomainTenantResolver extends Contracts\CachedTenantResolver { @@ -32,14 +33,14 @@ public function resolveWithoutCache(...$args): Tenant /** @var Tenant|null $tenant */ $tenant = config('tenancy.tenant_model')::query() - ->whereHas('domains', function ($query) use ($domain) { + ->whereHas('domains', function (Builder $query) use ($domain) { $query->where('domain', $domain); }) ->with('domains') ->first(); if ($tenant) { - $this->setCurrentDomain($tenant, ...$args); + $this->setCurrentDomain($tenant, $domain); return $tenant; } @@ -47,14 +48,14 @@ public function resolveWithoutCache(...$args): Tenant throw new TenantCouldNotBeIdentifiedOnDomainException($args[0]); } - public function tenantIdentified(Tenant $tenant, ...$args): void + public function resolved(Tenant $tenant, ...$args): void { - $this->setCurrentDomain($tenant, ...$args); + $this->setCurrentDomain($tenant, $args[0]); } - protected function setCurrentDomain(Tenant $tenant, ...$args): void + protected function setCurrentDomain(Tenant $tenant, string $domain): void { - static::$currentDomain = $tenant->domains->where('domain', $args[0])->first(); + static::$currentDomain = $tenant->domains->where('domain', $domain)->first(); } public function getArgsForTenant(Tenant $tenant): array From 597eebd693d4aae0246f24c573a5aef7e99577e1 Mon Sep 17 00:00:00 2001 From: Ralfs Date: Wed, 14 Apr 2021 08:42:44 +0300 Subject: [PATCH 8/8] StyleCi Fix --- src/Resolvers/DomainTenantResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/DomainTenantResolver.php b/src/Resolvers/DomainTenantResolver.php index 6ba72f1f2..13eeb4482 100644 --- a/src/Resolvers/DomainTenantResolver.php +++ b/src/Resolvers/DomainTenantResolver.php @@ -4,10 +4,10 @@ namespace Stancl\Tenancy\Resolvers; +use Illuminate\Database\Eloquent\Builder; use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException; -use Illuminate\Database\Eloquent\Builder; class DomainTenantResolver extends Contracts\CachedTenantResolver {