mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +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:
parent
27e9fb4a69
commit
c21beabd3e
2 changed files with 32 additions and 6 deletions
|
|
@ -37,7 +37,11 @@ abstract class CachedTenantResolver implements TenantResolver
|
||||||
$key = $this->getCacheKey(...$args);
|
$key = $this->getCacheKey(...$args);
|
||||||
|
|
||||||
if ($this->cache->has($key)) {
|
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);
|
$tenant = $this->resolveWithoutCache(...$args);
|
||||||
|
|
@ -64,6 +68,10 @@ abstract class CachedTenantResolver implements TenantResolver
|
||||||
|
|
||||||
abstract public function resolveWithoutCache(...$args): Tenant;
|
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.
|
* Get all the arg combinations for resolve() that can be used to find this tenant.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Resolvers;
|
namespace Stancl\Tenancy\Resolvers;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Stancl\Tenancy\Contracts\Domain;
|
use Stancl\Tenancy\Contracts\Domain;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||||
|
|
@ -28,18 +29,35 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||||
|
|
||||||
public function resolveWithoutCache(...$args): Tenant
|
public function resolveWithoutCache(...$args): Tenant
|
||||||
{
|
{
|
||||||
/** @var Domain $domain */
|
$domain = $args[0];
|
||||||
$domain = config('tenancy.domain_model')::where('domain', $args[0])->first();
|
|
||||||
|
|
||||||
if ($domain) {
|
/** @var Tenant|null $tenant */
|
||||||
static::$currentDomain = $domain;
|
$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]);
|
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
|
public function getArgsForTenant(Tenant $tenant): array
|
||||||
{
|
{
|
||||||
$tenant->unsetRelation('domains');
|
$tenant->unsetRelation('domains');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue