1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:34:04 +00:00

Refactor more old code and get tests to pass

This commit is contained in:
Samuel Štancl 2020-05-13 04:51:37 +02:00
parent c5377a16f7
commit c32f229dd5
72 changed files with 425 additions and 531 deletions

View file

@ -2,20 +2,36 @@
namespace App\Providers;
use Closure;
use Stancl\Tenancy\Contracts\Tenant;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
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\Listeners\JobPipeline;
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\Listeners\JobPipeline;
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;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tenancy;
class TenancyServiceProvider extends ServiceProvider
{
@ -25,35 +41,82 @@ class TenancyServiceProvider extends ServiceProvider
TenantCreated::class => [
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class, // triggers DatabaseMigrated event
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;
})->queue(true),
})->queue(false), // `false` by default, but you probably want to make this `true` for production.
],
DatabaseCreated::class => [],
DatabaseMigrated::class => [],
DatabaseSeeded::class => [],
TenantSaved::class => [],
TenantUpdated::class => [],
TenantDeleted::class => [
JobPipeline::make([
DeleteDatabase::class,
])->send(function (TenantDeleted $event) {
return $event->tenant;
})->queue(true),
// DeleteStorage::class,
})->queue(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()
{
//
// Make sure Tenancy is stateful.
$this->app->singleton(Tenancy::class);
// Make sure features are bootstrapped as soon as Tenancy is instantiated.
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
foreach ($this->app['config']['tenancy.features'] as $feature) {
$this->app[$feature]->bootstrap($tenancy);
}
return $tenancy;
});
// Make it possible to inject the current tenant by typehinting the Tenant contract.
$this->app->bind(Tenant::class, function ($app) {
return $app[Tenancy::class]->tenant;
});
// Make sure bootstrappers are stateful (singletons).
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
$this->app->singleton($bootstrapper);
}
// Bind the class in the tenancy.id_generator config to the UniqueIdentifierGenerator abstract.
$this->app->bind(UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
}
public function boot()
{
$this->bootEvents();
$this->mapRoutes();
//
}
@ -70,4 +133,15 @@ class TenancyServiceProvider extends ServiceProvider
}
}
}
}
protected function mapRoutes()
{
$this->app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
Route::middleware(['web'])
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
->group(base_path('routes/tenant.php'));
}
});
}
}

View file

@ -24,23 +24,6 @@ return [
'localhost',
],
'storage' => [
'data_column' => 'data',
'custom_columns' => [
// 'plan',
],
/**
* Here you can enable the Cached Tenant Lookup.
*
* You can specify what cache store should be used to cache the tenant resolution.
* Set to string with a specific cache store name, or to null to disable cache.
*/
'cache_store' => null, // env('CACHE_DRIVER')
'cache_ttl' => 3600, // seconds
],
/**
* Controller namespace used by routes in routes/tenant.php.
*/
@ -76,7 +59,6 @@ return [
*/
'prefix' => 'tenant',
'suffix' => '',
// todo get rid of this stuff, just set the closure instead
],
/**
@ -194,18 +176,11 @@ return [
* See the documentation page for each class to
* understand which ones you want to enable.
*/
'features' => [ // todo test features
// Stancl\Tenancy\Features\Timestamps::class, // https://tenancy.samuelstancl.me/docs/v2/features/timestamps/
'features' => [
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/
// Stancl\Tenancy\Features\TelescopeTags::class, // https://tenancy.samuelstancl.me/docs/v2/telescope/
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-redirect/
],
/**
* The URL to which users will be redirected when they try to acceess a central route on a tenant domain.
*/
'home_url' => '/app', // todo move this to static
'migration_parameters' => [
'--force' => true, // Set this to true to be able to run migrations in production
'--path' => [database_path('migrations/tenant')],

View file

@ -7,10 +7,17 @@
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider
| with the tenancy and web middleware groups. Good luck!
| with the namespace configured in your tenancy config.
|
| Feel free to customize them however you want. Good luck!
|
*/
Route::get('/app', function () {
return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
});
Route::group([
'middleware' => InitializeTenancyByDomain::class,
'prefix' => '/app',
], function () {
Route::get('/', function () {
return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
});
});