mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 15:34:04 +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. * 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>
69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
|
{
|
|
/**
|
|
* The model representing the domain that the tenant was identified on.
|
|
*
|
|
* @var Domain
|
|
*/
|
|
public static $currentDomain;
|
|
|
|
/** @var bool */
|
|
public static $shouldCache = false;
|
|
|
|
/** @var int */
|
|
public static $cacheTTL = 3600; // seconds
|
|
|
|
/** @var string|null */
|
|
public static $cacheStore = null; // default
|
|
|
|
public function resolveWithoutCache(...$args): Tenant
|
|
{
|
|
$domain = $args[0];
|
|
|
|
/** @var Tenant|null $tenant */
|
|
$tenant = config('tenancy.tenant_model')::query()
|
|
->whereHas('domains', function (Builder $query) use ($domain) {
|
|
$query->where('domain', $domain);
|
|
})
|
|
->with('domains')
|
|
->first();
|
|
|
|
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');
|
|
|
|
return $tenant->domains->map(function (Domain $domain) {
|
|
return [$domain->domain];
|
|
})->toArray();
|
|
}
|
|
}
|