-
-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathTenancyServiceProvider.php
More file actions
235 lines (196 loc) · 9.9 KB
/
TenancyServiceProvider.php
File metadata and controls
235 lines (196 loc) · 9.9 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare(strict_types=1);
namespace Stancl\Tenancy;
use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\DatabaseStore;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Console\Migrations\FreshCommand;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Listeners\ForgetTenantParameter;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
class TenancyServiceProvider extends ServiceProvider
{
public static Closure|null $configure = null;
public static bool $registerForgetTenantParameterListener = true;
public static bool $migrateFreshOverride = true;
/** @internal */
public static Closure|null $adjustCacheManagerUsing = null;
/* Register services. */
public function register(): void
{
if (static::$configure) {
(static::$configure)();
}
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
$this->app->singleton(Database\DatabaseManager::class);
// Make sure Tenancy is stateful.
$this->app->singleton(Tenancy::class);
// Make it possible to inject the current tenant by type hinting the Tenant contract.
$this->app->bind(Tenant::class, function ($app) {
return $app[Tenancy::class]->tenant;
});
$this->app->bind(Domain::class, function () {
return DomainTenantResolver::$currentDomain;
});
// Make sure bootstrappers are stateful (singletons).
foreach ($this->app['config']['tenancy.bootstrappers'] ?? [] as $bootstrapper) {
if (method_exists($bootstrapper, '__constructStatic')) {
$bootstrapper::__constructStatic($this->app);
}
$this->app->singleton($bootstrapper);
}
// Bind the class in the tenancy.models.id_generator config to the UniqueIdentifierGenerator abstract.
if (! is_null($this->app['config']['tenancy.models.id_generator'])) {
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.models.id_generator']);
}
$this->app->singleton(Commands\Migrate::class, function ($app) {
return new Commands\Migrate($app['migrator'], $app['events']);
});
$this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator']);
});
$this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db']);
});
$this->app->bind('globalCache', function ($app) {
// We create a separate CacheManager to be used for "global" cache -- cache that
// is always central, regardless of the current context.
//
// Importantly, we use a regular binding here, not a singleton. Thanks to that,
// any time we resolve this cache manager, we get a *fresh* instance -- an instance
// that was not affected by any scoping logic.
//
// This works great for cache stores that are *directly* scoped, like Redis or
// any other tagged or prefixed stores, but it doesn't work for the database driver.
//
// When DatabaseTenancyBootstrapper is used, it changes the default DB connection
// to 'tenant'. A freshly created CacheManager would therefore instantiate database
// stores with the tenant connection.
//
// For that reason, we adjust the relevant stores on this new CacheManager
// using the makeDatabaseCacheStoresCentral() method and the $adjustCacheManagerUsing callback below
// (set by DatabaseCacheBootstrapper).
$manager = new CacheManager($app);
// When DatabaseTenancyBootstrapper is used, database stores whose 'connection'
// config is null fall back to the default DB connection ('tenant'). Reset each
// such store to its explicitly configured connection, or fall back to central.
$this->makeDatabaseCacheStoresCentral($manager);
// DatabaseCacheBootstrapper explicitly writes 'tenant' into each store's 'connection'
// config. makeDatabaseCacheStoresCentral() above would then read 'tenant' as the
// configured value (not null) and use it directly, so the central connection fallback
// wouldn't be used.
//
// This callback is used to correct those connections back to central for globalCache.
if (static::$adjustCacheManagerUsing !== null) {
(static::$adjustCacheManagerUsing)($manager);
}
return $manager;
});
}
/**
* Ensure globalCache uses the central connection for database cache stores.
*
* A freshly built CacheManager creates database stores using the current default connection, which
* DatabaseTenancyBootstrapper switches to the tenant connection. Since global cache should always be
* central, reset those stores back to their configured connection, falling back to the central one.
*/
protected function makeDatabaseCacheStoresCentral(CacheManager $manager): void
{
$centralConnection = $this->app['config']['tenancy.database.central_connection'];
foreach ($this->app['config']['cache.stores'] ?? [] as $name => $store) {
$notAValidDatabaseStore = ! is_array($store) || ($store['driver'] ?? null) !== 'database';
if ($notAValidDatabaseStore) {
continue;
}
/** @var DatabaseStore $databaseStore */
$databaseStore = $manager->store($name)->getStore();
// If $store['connection'] is null, it defaults to the default DB connection (which may be tenant).
// Fall back to the central connection to keep the global cache central.
$databaseStore->setConnection(DB::connection($store['connection'] ?? $centralConnection));
$databaseStore->setLockConnection(DB::connection($store['lock_connection'] ?? $store['connection'] ?? $centralConnection));
}
}
/* Bootstrap services. */
public function boot(): void
{
$this->commands([
Commands\Up::class,
Commands\Run::class,
Commands\Down::class,
Commands\Link::class,
Commands\Seed::class,
Commands\Tinker::class,
Commands\Install::class,
Commands\Migrate::class,
Commands\Rollback::class,
Commands\TenantList::class,
Commands\TenantDump::class,
Commands\MigrateFresh::class,
Commands\ClearPendingTenants::class,
Commands\CreatePendingTenants::class,
Commands\PurgeImpersonationTokens::class,
Commands\CreateUserWithRLSPolicies::class,
]);
if (static::$migrateFreshOverride) {
$this->app->extend(FreshCommand::class, function ($_, $app) {
return new Commands\MigrateFreshOverride($app['migrator']);
});
}
$this->publishes([
__DIR__ . '/../assets/config.php' => config_path('tenancy.php'),
], 'config');
$this->publishes([
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
], 'migrations');
$this->publishes([
__DIR__ . '/../assets/impersonation-migrations/' => database_path('migrations'),
], 'impersonation-migrations');
$this->publishes([
__DIR__ . '/../assets/resource-syncing-migrations/' => database_path('migrations'),
], 'resource-syncing-migrations');
$this->publishes([
__DIR__ . '/../assets/tenant_routes.stub.php' => base_path('routes/tenant.php'),
], 'routes');
$this->publishes([
__DIR__ . '/../assets/TenancyServiceProvider.stub.php' => app_path('Providers/TenancyServiceProvider.php'),
], 'providers');
if (config('tenancy.routes', true)) {
$this->loadRoutesFrom(__DIR__ . '/../assets/routes.php');
}
$this->app->singleton('globalUrl', function (Container $app) {
if ($app->bound(FilesystemTenancyBootstrapper::class)) {
/** @var \Illuminate\Routing\UrlGenerator */
$instance = clone $app->make('url');
$instance->useAssetOrigin($app->make(FilesystemTenancyBootstrapper::class)->originalAssetUrl);
} else {
$instance = $app->make('url');
}
return $instance;
});
// Bootstrap features that are already enabled in the config.
// If more features are enabled at runtime, this method may be called
// multiple times, it keeps track of which features have already been bootstrapped.
$this->app->make(Tenancy::class)->bootstrapFeatures();
Route::middlewareGroup('clone', []);
Route::middlewareGroup('universal', []);
Route::middlewareGroup('tenant', []);
Route::middlewareGroup('central', []);
if (static::$registerForgetTenantParameterListener) {
// Ideally, this listener would only be registered when kernel-level
// path identification is used, however doing that check reliably
// at this point in the lifecycle isn't feasible. For that reason,
// rather than doing an "outer" check, we do an "inner" check within
// that listener. That also means the listener needs to be registered
// always. We allow for this to be controlled using a static property.
Event::listen(RouteMatched::class, ForgetTenantParameter::class);
}
}
}