forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenancy.php
169 lines (130 loc) · 4.25 KB
/
Tenancy.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Stancl\Tenancy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById;
class Tenancy
{
use Macroable;
/** @var Tenant|Model|null */
public $tenant;
/** @var callable|null */
public $getBootstrappersUsing = null;
/** @var bool */
public $initialized = false;
/**
* Initializes the tenant.
* @param Tenant|int|string $tenant
* @return void
*/
public function initialize($tenant): void
{
if (! is_object($tenant)) {
$tenantId = $tenant;
$tenant = $this->find($tenantId);
if (! $tenant) {
throw new TenantCouldNotBeIdentifiedById($tenantId);
}
}
if ($this->initialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) {
return;
}
// TODO: Remove this (so that runForMultiple() is still performant) and make the FS bootstrapper work either way
if ($this->initialized) {
$this->end();
}
$this->tenant = $tenant;
event(new Events\InitializingTenancy($this));
$this->initialized = true;
event(new Events\TenancyInitialized($this));
}
public function end(): void
{
event(new Events\EndingTenancy($this));
if (! $this->initialized) {
return;
}
$this->initialized = false;
event(new Events\TenancyEnded($this));
$this->tenant = null;
}
/** @return TenancyBootstrapper[] */
public function getBootstrappers(): array
{
// If no callback for getting bootstrappers is set, we just return all of them.
$resolve = $this->getBootstrappersUsing ?? function (Tenant $tenant) {
return config('tenancy.bootstrappers');
};
// Here We instantiate the bootstrappers and return them.
return array_map('app', $resolve($this->tenant));
}
public function query(): Builder
{
return $this->model()->query();
}
/** @return Tenant|Model */
public function model()
{
$class = config('tenancy.tenant_model');
return new $class;
}
public function find($id): ?Tenant
{
return $this->model()->where($this->model()->getTenantKeyName(), $id)->first();
}
/**
* Run a callback in the central context.
* Atomic, safely reverts to previous context.
*
* @param callable $callback
* @return mixed
*/
public function central(callable $callback)
{
$previousTenant = $this->tenant;
$this->end();
// This callback will usually not accept arguments, but the previous
// Tenant is the only value that can be useful here, so we pass that.
$result = $callback($previousTenant);
if ($previousTenant) {
$this->initialize($previousTenant);
}
return $result;
}
/**
* Run a callback for multiple tenants.
* More performant than running $tenant->run() one by one.
*
* @param Tenant[]|\Traversable|string[]|null $tenants
* @param callable $callback
* @return void
*/
public function runForMultiple($tenants, callable $callback)
{
// Convert null to all tenants
$tenants = is_null($tenants) ? $this->model()->cursor() : $tenants;
// Convert incrementing int ids to strings
$tenants = is_int($tenants) ? (string) $tenants : $tenants;
// Wrap string in array
$tenants = is_string($tenants) ? [$tenants] : $tenants;
// Use all tenants if $tenants is falsey
$tenants = $tenants ?: $this->model()->cursor();
$originalTenant = $this->tenant;
foreach ($tenants as $tenant) {
if (! $tenant instanceof Tenant) {
$tenant = $this->find($tenant);
}
$this->initialize($tenant);
$callback($tenant);
}
if ($originalTenant) {
$this->initialize($originalTenant);
} else {
$this->end();
}
}
}