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

Add identification section to config, refactor static properties

This commit is contained in:
Samuel Štancl 2022-10-01 20:01:18 +02:00
parent e5bc8ddb77
commit ccaba05272
17 changed files with 153 additions and 97 deletions

View file

@ -11,23 +11,17 @@ use Stancl\Tenancy\Contracts\TenantResolver;
abstract class CachedTenantResolver implements TenantResolver
{
public static bool $shouldCache = false; // todo docblocks for these
public static int $cacheTTL = 3600; // seconds
public static string|null $cacheStore = null; // default
/** @var Repository */
protected $cache;
public function __construct(Factory $cache)
{
$this->cache = $cache->store(static::$cacheStore);
$this->cache = $cache->store(static::cacheStore());
}
public function resolve(mixed ...$args): Tenant
{
if (! static::$shouldCache) {
if (! static::shouldCache()) {
return $this->resolveWithoutCache(...$args);
}
@ -42,14 +36,14 @@ abstract class CachedTenantResolver implements TenantResolver
}
$tenant = $this->resolveWithoutCache(...$args);
$this->cache->put($key, $tenant, static::$cacheTTL);
$this->cache->put($key, $tenant, static::cacheTTL());
return $tenant;
}
public function invalidateCache(Tenant $tenant): void
{
if (! static::$shouldCache) {
if (! static::shouldCache()) {
return;
}
@ -75,4 +69,19 @@ abstract class CachedTenantResolver implements TenantResolver
* @return array[]
*/
abstract public function getArgsForTenant(Tenant $tenant): array;
public static function shouldCache(): bool
{
return config('tenancy.identification.resolvers.' . static::class . '.cache') ?? false;
}
public static function cacheTTL(): int
{
return config('tenancy.identification.resolvers.' . static::class . '.cache_ttl') ?? 3600;
}
public static function cacheStore(): string|null
{
return config('tenancy.identification.resolvers.' . static::class . '.cache_store');
}
}

View file

@ -14,12 +14,6 @@ class DomainTenantResolver extends Contracts\CachedTenantResolver
/** The model representing the domain that the tenant was identified on. */
public static Domain $currentDomain; // todo |null?
public static bool $shouldCache = false;
public static int $cacheTTL = 3600; // seconds
public static string|null $cacheStore = null; // default
public function resolveWithoutCache(mixed ...$args): Tenant
{
$domain = $args[0];

View file

@ -10,21 +10,13 @@ use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;
class PathTenantResolver extends Contracts\CachedTenantResolver
{
public static string $tenantParameterName = 'tenant';
public static bool $shouldCache = false;
public static int $cacheTTL = 3600; // seconds
public static string|null $cacheStore = null; // default
public function resolveWithoutCache(mixed ...$args): Tenant
{
/** @var Route $route */
$route = $args[0];
if ($id = (string) $route->parameter(static::$tenantParameterName)) {
$route->forgetParameter(static::$tenantParameterName);
if ($id = (string) $route->parameter(static::tenantParameterName())) {
$route->forgetParameter(static::tenantParameterName());
if ($tenant = tenancy()->find($id)) {
return $tenant;
@ -40,4 +32,9 @@ class PathTenantResolver extends Contracts\CachedTenantResolver
[$tenant->getTenantKey()],
];
}
public static function tenantParameterName(): string
{
return config('tenancy.identification.resolvers.' . static::class . '.tenant_parameter_name') ?? 'tenant';
}
}