mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
add Tenancy::$findWith
This commit is contained in:
parent
5ef7604e17
commit
ac5948dfd1
5 changed files with 24 additions and 13 deletions
|
|
@ -6,7 +6,6 @@ namespace Stancl\Tenancy\Database\Concerns;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
|
|
||||||
use Stancl\Tenancy\Tenancy;
|
use Stancl\Tenancy\Tenancy;
|
||||||
|
|
||||||
trait InvalidatesResolverCache
|
trait InvalidatesResolverCache
|
||||||
|
|
@ -14,12 +13,7 @@ trait InvalidatesResolverCache
|
||||||
public static function bootInvalidatesResolverCache(): void
|
public static function bootInvalidatesResolverCache(): void
|
||||||
{
|
{
|
||||||
static::saved(function (Tenant&Model $tenant) {
|
static::saved(function (Tenant&Model $tenant) {
|
||||||
foreach (Tenancy::cachedResolvers() as $resolver) {
|
Tenancy::invalidateResolverCache($tenant);
|
||||||
/** @var CachedTenantResolver $resolver */
|
|
||||||
$resolver = app($resolver);
|
|
||||||
|
|
||||||
$resolver->invalidateCache($tenant);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts\Domain;
|
||||||
use Stancl\Tenancy\Contracts\SingleDomainTenant;
|
use Stancl\Tenancy\Contracts\SingleDomainTenant;
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
use Stancl\Tenancy\Contracts\Tenant;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
|
||||||
|
use Stancl\Tenancy\Tenancy;
|
||||||
|
|
||||||
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||||
{
|
{
|
||||||
|
|
@ -21,15 +22,16 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
|
||||||
$domain = $args[0];
|
$domain = $args[0];
|
||||||
|
|
||||||
/** @var Tenant&Model $tenantModel */
|
/** @var Tenant&Model $tenantModel */
|
||||||
$tenantModel = config('tenancy.models.tenant')::make();
|
$tenantModel = tenancy()->model();
|
||||||
|
|
||||||
if ($tenantModel instanceof SingleDomainTenant) {
|
if ($tenantModel instanceof SingleDomainTenant) {
|
||||||
$tenant = $tenantModel->newQuery()
|
$tenant = $tenantModel->newQuery()
|
||||||
|
->with(Tenancy::$findWith)
|
||||||
->firstWhere('domain', $domain);
|
->firstWhere('domain', $domain);
|
||||||
} else {
|
} else {
|
||||||
$tenant = $tenantModel->newQuery()
|
$tenant = $tenantModel->newQuery()
|
||||||
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
|
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
|
||||||
->with('domains')
|
->with(array_unique(array_merge(Tenancy::$findWith, ['domains'])))
|
||||||
->first();
|
->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class PathTenantResolver extends Contracts\CachedTenantResolver
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($key) {
|
if ($key) {
|
||||||
if ($tenant = tenancy()->find($key, $column)) {
|
if ($tenant = tenancy()->find($key, $column, withRelations: true)) {
|
||||||
/** @var Tenant $tenant */
|
/** @var Tenant $tenant */
|
||||||
return $tenant;
|
return $tenant;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ class RequestDataTenantResolver extends Contracts\CachedTenantResolver
|
||||||
{
|
{
|
||||||
$payload = (string) $args[0];
|
$payload = (string) $args[0];
|
||||||
|
|
||||||
if ($payload && $tenant = tenancy()->find($payload)) {
|
if ($payload && $tenant = tenancy()->find($payload, withRelations: true)) {
|
||||||
return $tenant;
|
return $tenant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,11 @@ class Tenancy
|
||||||
/** Is tenancy fully initialized? */
|
/** Is tenancy fully initialized? */
|
||||||
public bool $initialized = false; // todo@docs document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method)
|
public bool $initialized = false; // todo@docs document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of relations to eager load when fetching a tenant via tenancy()->find().
|
||||||
|
*/
|
||||||
|
public static array $findWith = [];
|
||||||
|
|
||||||
/** Initialize tenancy for the passed tenant. */
|
/** Initialize tenancy for the passed tenant. */
|
||||||
public function initialize(Tenant|int|string $tenant): void
|
public function initialize(Tenant|int|string $tenant): void
|
||||||
{
|
{
|
||||||
|
|
@ -136,10 +141,10 @@ class Tenancy
|
||||||
/**
|
/**
|
||||||
* Try to find a tenant using an ID.
|
* Try to find a tenant using an ID.
|
||||||
*/
|
*/
|
||||||
public static function find(int|string $id, string $column = null): (Tenant&Model)|null
|
public static function find(int|string $id, string $column = null, bool $withRelations = false): (Tenant&Model)|null
|
||||||
{
|
{
|
||||||
/** @var (Tenant&Model)|null $tenant */
|
/** @var (Tenant&Model)|null $tenant */
|
||||||
$tenant = static::model()->firstWhere($column ?? static::model()->getTenantKeyName(), $id);
|
$tenant = static::model()->with($withRelations ? static::$findWith : [])->firstWhere($column ?? static::model()->getTenantKeyName(), $id);
|
||||||
|
|
||||||
return $tenant;
|
return $tenant;
|
||||||
}
|
}
|
||||||
|
|
@ -221,6 +226,16 @@ class Tenancy
|
||||||
return array_keys($cachedResolvers);
|
return array_keys($cachedResolvers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function invalidateResolverCache(Tenant&Model $tenant): void
|
||||||
|
{
|
||||||
|
foreach (static::cachedResolvers() as $resolver) {
|
||||||
|
/** @var Resolvers\Contracts\CachedTenantResolver $resolver */
|
||||||
|
$resolver = app($resolver);
|
||||||
|
|
||||||
|
$resolver->invalidateCache($tenant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tenant identification middleware used by the package.
|
* Tenant identification middleware used by the package.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue