1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:14:04 +00:00

Fix issue 632: cached lookup (#633)

* 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.

* StlyeCi Fix - removed PHP 8 nullsafe operator for compatibility with older PHP versions, replaced with inline if

* Redundant variable '$domain', because $tenant is not null, only, when current domain is found and relationship is loaded (with only one domain).

* Fixed tenant()->domains showing incorrect data. Renamed tenantIdentifiedFromCache() method and removed duplicate code, when setting current domain.

* Removed select() for better flexibility, added new method setCurrentDomain(), refactored the usage of tenantIdentified().

* rename method to resolved()

* clean up code

* StyleCi Fix

Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
Ralfs 2021-04-16 20:57:41 +03:00 committed by GitHub
parent 27e9fb4a69
commit c21beabd3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View file

@ -37,7 +37,11 @@ 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->resolved($tenant, ...$args);
return $tenant;
}
$tenant = $this->resolveWithoutCache(...$args);
@ -64,6 +68,10 @@ abstract class CachedTenantResolver implements TenantResolver
abstract public function resolveWithoutCache(...$args): Tenant;
public function resolved(Tenant $tenant, ...$args): void
{
}
/**
* Get all the arg combinations for resolve() that can be used to find this tenant.
*

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Database\Eloquent\Builder;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
@ -28,18 +29,35 @@ 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];
if ($domain) {
static::$currentDomain = $domain;
/** @var Tenant|null $tenant */
$tenant = config('tenancy.tenant_model')::query()
->whereHas('domains', function (Builder $query) use ($domain) {
$query->where('domain', $domain);
})
->with('domains')
->first();
return $domain->tenant;
if ($tenant) {
$this->setCurrentDomain($tenant, $domain);
return $tenant;
}
throw new TenantCouldNotBeIdentifiedOnDomainException($args[0]);
}
public function resolved(Tenant $tenant, ...$args): void
{
$this->setCurrentDomain($tenant, $args[0]);
}
protected function setCurrentDomain(Tenant $tenant, string $domain): void
{
static::$currentDomain = $tenant->domains->where('domain', $domain)->first();
}
public function getArgsForTenant(Tenant $tenant): array
{
$tenant->unsetRelation('domains');