mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 13:14:05 +00:00
120 lines
3.8 KiB
PHP
120 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Stancl\Tenancy\Listeners\JobPipeline;
|
|
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
|
use Stancl\Tenancy\Events\DatabaseCreated;
|
|
use Stancl\Tenancy\Events\DatabaseDeleted;
|
|
use Stancl\Tenancy\Events\DatabaseMigrated;
|
|
use Stancl\Tenancy\Events\DatabaseRolledBack;
|
|
use Stancl\Tenancy\Events\DatabaseSeeded;
|
|
use Stancl\Tenancy\Events\DomainCreated;
|
|
use Stancl\Tenancy\Events\DomainDeleted;
|
|
use Stancl\Tenancy\Events\DomainSaved;
|
|
use Stancl\Tenancy\Events\DomainUpdated;
|
|
use Stancl\Tenancy\Events\RevertedToCentralContext;
|
|
use Stancl\Tenancy\Events\TenancyBootstrapped;
|
|
use Stancl\Tenancy\Events\TenancyEnded;
|
|
use Stancl\Tenancy\Events\TenancyInitialized;
|
|
use Stancl\Tenancy\Events\TenantCreated;
|
|
use Stancl\Tenancy\Events\TenantDeleted;
|
|
use Stancl\Tenancy\Events\TenantSaved;
|
|
use Stancl\Tenancy\Events\TenantUpdated;
|
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
|
use Stancl\Tenancy\Jobs\DeleteDatabase;
|
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
|
use Stancl\Tenancy\Jobs\SeedDatabase;
|
|
|
|
class TenancyServiceProvider extends ServiceProvider
|
|
{
|
|
public function events()
|
|
{
|
|
return [
|
|
TenantCreated::class => [
|
|
JobPipeline::make([
|
|
CreateDatabase::class,
|
|
MigrateDatabase::class,
|
|
SeedDatabase::class,
|
|
|
|
// Your own jobs to prepare the tenant.
|
|
// Provision API keys, create S3 buckets, anything you want!
|
|
|
|
])->send(function (TenantCreated $event) {
|
|
return $event->tenant;
|
|
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
|
|
],
|
|
TenantSaved::class => [],
|
|
TenantUpdated::class => [],
|
|
TenantDeleted::class => [
|
|
JobPipeline::make([
|
|
DeleteDatabase::class,
|
|
])->send(function (TenantDeleted $event) {
|
|
return $event->tenant;
|
|
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
|
|
],
|
|
|
|
DomainCreated::class => [],
|
|
DomainSaved::class => [],
|
|
DomainUpdated::class => [],
|
|
DomainDeleted::class => [],
|
|
|
|
|
|
DatabaseCreated::class => [],
|
|
DatabaseMigrated::class => [],
|
|
DatabaseSeeded::class => [],
|
|
DatabaseRolledBack::class => [],
|
|
DatabaseDeleted::class => [],
|
|
|
|
TenancyInitialized::class => [
|
|
BootstrapTenancy::class,
|
|
],
|
|
TenancyEnded::class => [
|
|
RevertToCentralContext::class,
|
|
],
|
|
|
|
TenancyBootstrapped::class => [],
|
|
RevertedToCentralContext::class => [],
|
|
];
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
$this->bootEvents();
|
|
$this->mapRoutes();
|
|
|
|
//
|
|
}
|
|
|
|
protected function bootEvents()
|
|
{
|
|
foreach ($this->events() as $event => $listeners) {
|
|
foreach (array_unique($listeners) as $listener) {
|
|
if ($listener instanceof JobPipeline) {
|
|
$listener = $listener->toListener();
|
|
}
|
|
|
|
Event::listen($event, $listener);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function mapRoutes()
|
|
{
|
|
$this->app->booted(function () {
|
|
if (file_exists(base_path('routes/tenant.php'))) {
|
|
Route::namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
|
|
->group(base_path('routes/tenant.php'));
|
|
}
|
|
});
|
|
}
|
|
}
|