-
-
Notifications
You must be signed in to change notification settings - Fork 485
Expand file tree
/
Copy pathDomainTenantResolver.php
More file actions
109 lines (86 loc) · 3.35 KB
/
DomainTenantResolver.php
File metadata and controls
109 lines (86 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\SingleDomainTenant;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedOnDomainException;
use Stancl\Tenancy\Tenancy;
class DomainTenantResolver extends Contracts\CachedTenantResolver
{
/** The model representing the domain that the tenant was identified on. */
public static Domain|null $currentDomain = null;
public function resolveWithoutCache(mixed ...$args): Tenant
{
$domain = $args[0];
$tenant = static::findTenantByDomain($domain);
/** @var (Tenant&Model)|null $tenant */
if ($tenant) {
static::setCurrentDomain($tenant, $domain);
return $tenant;
}
throw new TenantCouldNotBeIdentifiedOnDomainException($domain);
}
public static function findTenantByDomain(string $domain): (Tenant&Model)|null
{
/** @var Tenant&Model $tenantModel */
$tenantModel = tenancy()->model();
if ($tenantModel instanceof SingleDomainTenant) {
$tenant = $tenantModel->newQuery()
->with(Tenancy::$findWith)
->firstWhere('domain', $domain);
} else {
$tenant = $tenantModel->newQuery()
->whereHas('domains', fn (Builder $query) => $query->where('domain', $domain))
->with(array_unique(array_merge(Tenancy::$findWith, ['domains'])))
->first();
}
/** @var (Tenant&Model)|null $tenant */
return $tenant;
}
public static function isSubdomain(string $domain): bool
{
$centralDomains = Arr::wrap(config('tenancy.identification.central_domains'));
foreach ($centralDomains as $centralDomain) {
if ($domain === $centralDomain) {
return false;
}
if (Str::endsWith($domain, '.' . $centralDomain)) {
return true;
}
}
return false;
}
public function resolved(Tenant $tenant, mixed ...$args): void
{
$this->setCurrentDomain($tenant, $args[0]);
}
protected static function setCurrentDomain(Tenant $tenant, string $domain): void
{
/** @var Tenant&Model $tenant */
if (! $tenant instanceof SingleDomainTenant) {
static::$currentDomain = $tenant->domains->where('domain', $domain)->first();
}
}
public function getPossibleCacheKeys(Tenant&Model $tenant): array
{
$domains = [];
if ($tenant instanceof SingleDomainTenant) {
$domains = array_filter([
$tenant->getOriginal('domain'), // Previous domain
$tenant->domain, // Current domain
]);
} elseif (method_exists($tenant, 'domains') && $tenant->domains() instanceof Relation) {
$tenant->unsetRelation('domains');
$domains = $tenant->domains->map(function (Domain&Model $domain) {
return $domain->domain;
})->toArray();
}
return array_map(fn (string $domain) => $this->formatCacheKey($domain), $domains);
}
}