mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 19:14:03 +00:00
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.
This commit is contained in:
parent
27e9fb4a69
commit
9c9b2da618
2 changed files with 30 additions and 4 deletions
|
|
@ -37,7 +37,10 @@ abstract class CachedTenantResolver implements TenantResolver
|
|||
$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 @@ abstract class CachedTenantResolver implements TenantResolver
|
|||
|
||||
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.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue