163 lines
5.4 KiB
PHP
163 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Stancl\JobPipeline\JobPipeline;
|
|
use Stancl\Tenancy\Events;
|
|
use Stancl\Tenancy\Jobs;
|
|
use Stancl\Tenancy\Listeners;
|
|
use Stancl\Tenancy\Middleware;
|
|
|
|
class TenancyServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* By default, no namespace is used to support the callable array syntax.
|
|
*
|
|
* @var string
|
|
*/
|
|
public static string $controllerNamespace = '';
|
|
|
|
/** @return array */
|
|
public function events(): array
|
|
{
|
|
return [
|
|
// Tenant events.
|
|
Events\CreatingTenant::class => [],
|
|
Events\TenantCreated::class => [
|
|
JobPipeline::make([
|
|
Jobs\CreateDatabase::class,
|
|
Jobs\MigrateDatabase::class,
|
|
// Jobs\SeedDatabase::class,
|
|
// Your own jobs to prepare the tenant.
|
|
// Provision API keys, create S3 buckets, anything you want !
|
|
])
|
|
->send(function (Events\TenantCreated $event) {
|
|
return $event->tenant;
|
|
})
|
|
// `false` by default, but you probably want to make this `true` for production.
|
|
->shouldBeQueued(false),
|
|
],
|
|
Events\SavingTenant::class => [],
|
|
Events\TenantSaved::class => [],
|
|
Events\UpdatingTenant::class => [],
|
|
Events\TenantUpdated::class => [],
|
|
Events\DeletingTenant::class => [],
|
|
Events\TenantDeleted::class => [
|
|
JobPipeline::make([
|
|
Jobs\DeleteDatabase::class,
|
|
])
|
|
->send(function (Events\TenantDeleted $event) {
|
|
return $event->tenant;
|
|
})
|
|
// `false` by default, but you probably want to make this `true` for production.
|
|
->shouldBeQueued(false),
|
|
],
|
|
|
|
// Domain events.
|
|
Events\CreatingDomain::class => [],
|
|
Events\DomainCreated::class => [],
|
|
Events\SavingDomain::class => [],
|
|
Events\DomainSaved::class => [],
|
|
Events\UpdatingDomain::class => [],
|
|
Events\DomainUpdated::class => [],
|
|
Events\DeletingDomain::class => [],
|
|
Events\DomainDeleted::class => [],
|
|
|
|
// Database events.
|
|
Events\DatabaseCreated::class => [],
|
|
Events\DatabaseMigrated::class => [],
|
|
Events\DatabaseSeeded::class => [],
|
|
Events\DatabaseRolledBack::class => [],
|
|
Events\DatabaseDeleted::class => [],
|
|
|
|
// Tenancy events.
|
|
Events\InitializingTenancy::class => [],
|
|
Events\TenancyInitialized::class => [
|
|
Listeners\BootstrapTenancy::class,
|
|
],
|
|
|
|
Events\EndingTenancy::class => [],
|
|
Events\TenancyEnded::class => [
|
|
Listeners\RevertToCentralContext::class,
|
|
],
|
|
|
|
Events\BootstrappingTenancy::class => [],
|
|
Events\TenancyBootstrapped::class => [],
|
|
Events\RevertingToCentralContext::class => [],
|
|
Events\RevertedToCentralContext::class => [],
|
|
|
|
// Resource syncing.
|
|
Events\SyncedResourceSaved::class => [
|
|
Listeners\UpdateSyncedResource::class,
|
|
],
|
|
|
|
// Fired only when a synced resource is changed in a different DB
|
|
// than the origin DB (to avoid infinite loops).
|
|
Events\SyncedResourceChangedInForeignDatabase::class => [],
|
|
];
|
|
}
|
|
|
|
/** @return void */
|
|
public function register(): void
|
|
{
|
|
// Silence is golden.
|
|
}
|
|
|
|
/** @return void */
|
|
public function boot(): void
|
|
{
|
|
$this->bootEvents();
|
|
$this->mapRoutes();
|
|
|
|
$this->makeTenancyMiddlewareHighestPriority();
|
|
}
|
|
|
|
/** @return void */
|
|
protected function bootEvents(): void
|
|
{
|
|
foreach ($this->events() as $event => $listeners) {
|
|
foreach ($listeners as $listener) {
|
|
if ($listener instanceof JobPipeline) {
|
|
$listener = $listener->toListener();
|
|
}
|
|
|
|
Event::listen($event, $listener);
|
|
}
|
|
}
|
|
}
|
|
|
|
/** @return void */
|
|
protected function mapRoutes(): void
|
|
{
|
|
$this->app->booted(function () {
|
|
if (file_exists(base_path('routes/tenant.php'))) {
|
|
Route::namespace(static::$controllerNamespace)
|
|
->group(base_path('routes/tenant.php'));
|
|
}
|
|
});
|
|
}
|
|
|
|
/** @return void */
|
|
protected function makeTenancyMiddlewareHighestPriority(): void
|
|
{
|
|
$tenancyMiddleware = [
|
|
// Even higher priority than the initialization middleware.
|
|
Middleware\PreventAccessFromCentralDomains::class,
|
|
|
|
Middleware\InitializeTenancyByDomain::class,
|
|
Middleware\InitializeTenancyBySubdomain::class,
|
|
Middleware\InitializeTenancyByDomainOrSubdomain::class,
|
|
Middleware\InitializeTenancyByPath::class,
|
|
Middleware\InitializeTenancyByRequestData::class,
|
|
];
|
|
|
|
foreach (array_reverse($tenancyMiddleware) as $middleware) {
|
|
$this->app[\Illuminate\Contracts\Http\Kernel::class]->prependToMiddlewarePriority($middleware);
|
|
}
|
|
}
|
|
}
|