mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:34:04 +00:00
* Add &Model to docblock * Fix code style (php-cs-fixer) * Only delete synced resource if the central resource shouldSync * Add central resource detached event and listener * Add SyncedTenant interface * Use the event & listener in the test file * Add getGlobalIdentifierKey(Name) to TenantMorphPivot * Refactor TriggerSyncingEvents * Fix code style (php-cs-fixer) * Test queueing the detaching listener * Move finding the central resource into the event, naming changes * Fix code style (php-cs-fixer) * Simplify listener code * Refactor detaching logic * Create tenant resource after attaching central to tenant, test queueing related listener * Delete dd() * Fix code style (php-cs-fixer) * Move triggerAttachEvent from SyncMaster * Update attach event-related code * Move findResource from SyncedTenant to the pivot trait * Add annotation * Update annotation * Simplify getAttributesForCreation in CreateTenantResourceFromSyncMaster * Update naming * Add tenant trait for attaching/detaching resources * Update test names * Move creation attribute parsing method to trait * Rename variable * Fix code style (php-cs-fixer) * Delete complete to-do * Delete event comment * Rename event property * Find tenant resource in detach listener * Use global ID key of tenant resource in cascade deletes listener * Use global ID key name of the central resource while creating/deleting tenant resources * Add getSyncedCreationAttributes example in the annotation * Fix inconsistencies in SyncedTenant methods * Improve annotation * Don't return the query in `$scopeGetModelQuery` Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Fix code style (php-cs-fixer) * Update scoping getModel query * Only use detach event instead of using both detach and delete events, refactor code * Test that detaching tenant from a central resource doesn't affect other tenants * Delete extra imports * Fix code style (php-cs-fixer) * Add PivotWithRelation, test attaching/detaching resources without polymorphic relations * Refactor TriggerSyncingEvents to work with non-polymorphic relations too * Fix code style (php-cs-fixer) * Rename synced resource changed event, fix tests * Enforce passing Tenant&Model to attach/detach events * Prevent firing saved event automatically in CreateTenantResource * Improve TriggerSyncingEvents trait * Delete unused import * Make TriggerSyncingEvents methods non-static, improve annotations * Pass saved model to event * Move attach/detach queueing tests to ResourceSyncingTest, pass models instead of IDs to attach/detach * Move events to ResourceSyncing\Events * Fix code style (php-cs-fixer) * Use SerializesModels in queueable listeners instead of events * Delete redundant $shouldQueue setting * Rename listener, test cascade deletes from both sides of many-to-many * Move creation attributes-related code to a separate test file, improve comments (wip) * Improve comments, fix variable name capitalization * Delete tracing comma * Extract duplicate code into a trait * Don't accept nullable tenant in SyncMasterDeleted * Fix annotation * Fix code style (php-cs-fixer) * Update annotation * Fix PHPStan error * Fix annotation * Update comments and test naming * Move triggerDeleteEvent to CascadeDeletes interface * Rename test file * Import TenantPivot in Tenant class (tests/Etc) * Add central resource not available in pivot exception * Rename SaveSyncedResource to UpdateOrCreateSyncedResource * Add new events and listeners to TSP stub * Improve comments and naming * Only keep SerializesModels in classes that utilize it * Use tenant->run() * Import events in stub * Move RS listeners to separate namespace, use `Event/`Listener/` in stub for consistency * Fix code style (php-cs-fixer) * Fix namespace changes * Use cursor instead of get * Update src/ResourceSyncing/ParsesCreationAttributes.php Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Update naming, structure (discussed on Discord) * Update uses in in test file * remove double ;; * Add comments * Test if static properties work fine with queueable listeners * Update $shouldQuery test * Update creation attributes * Work on updating the tests * Make synced attributes configurable using static properties * Update resource syncing tests * Get rid of mixed attribute classes * Get rid of TenantUserWIthCreationAttributes * Fix imports * Get rid of the conditionally synced classes, improve tests * Simplify resource creation tests (only test the more complex cases instead of each case - if the complex case works, the simpler cases work too) * Clean up ResourceSyncingTest (mostly duplicate tests that were already in AutomaticResourceCreationTest) * Simplify class naming in polymorhpic tests * Move automatic resource creation tests to ResourceSyncingTest * Test that the sync event doesn't get triggered excessively * Only trigger the sync event if the synced attributes were changed or if the resource was recently created * Update synced attribute instead of unsynced in test * Fix sync event test * Update static property redefining test * Use getGlobalIdentifierKeyName() instead of hardcoding the key name * Delete static properties from the ResourceSyncing trait * Reuse user classes in polymorphic tests * Update tests/ResourceSyncingTest.php Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Use the default tenants() method in central user, override the default in ResourceSyncingTest * Use BelongsToMany as tenants() return type * Fix code style (php-cs-fixer) * Delete extra static property from trait * Delete duplicate events/listeners from TSP stub * Delete weird expectation, use $model->trashed() * Change ResourceUser to TenantUser * Add defaults for getGlobalIdentifierKey(Name) * Use singular tenant in DeleteResourceInTenants name * Rename getSyncedCreationAttributes to getCreationAttributes * Fix comma position in comment * minor fixes in traits and interfaces * Fix code style (php-cs-fixer) * Correct comment * Use $tenant->run() * Update scopeGetModelQuery annotation * Use static property for testing shouldSync * Improve test * Get rid of datasets * Add trashed assertions * Always merge synced attributes with the creation attributes during parsing * Update creation attributes in test's beforeEach * Use only the necessary creation attributes (no need to include the synced attributes because they get merged automatically) * Rename ResourceTenant to MorphTenant * Add TriggerSyncingEvents docblock Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Add force deletes test * Fix code style (php-cs-fixer) * Delete pivot if it can't access the resource class * Make parseCreationAttributes more readable * Comment out setting $scopeGetModelQuery in the stub * Add @var annotations to bootTriggerSyncingEvents * Fix attach()/detach() exception test * Interrupt creation of broken pivots instead of deleting the pivots at a later point * Add more comments * Update CreateTenantResource comment * Assert that forceDelete() doesn't affect other tenant resources * Rename test * Correct with() array formatting * Expand test with soft deletes * Merge SyncedResourceSaved tests * Improve naming, comments and minor details in the assertions * Move test * Fix failing test * Delete duplicate test * Minor test improvement * Delete duplicate test * Improve old test * Minor test improvement * Improve event test * Improve tests (naming, code, comments) * Delete extra test, add comments to the larger test * Refactor central -> tenant attach() test * Apply changes from central -> tenant attach() test on tenant -> central test * Fix assertions in central -> tenant * Correct comment and assertion * Refactor tenant -> central attach() test * Fix inconsistency * Delete unused import * Add comments * Update polymorphic test names * Rename polymorphic tests * Update listener test name * Delete redundant tenant ID assignments * Improve test names * Move polymorphic tests to ResourceSyncingTest * Mention alternative solutions in CentralResourceNotAvailableInPivotException * Add comments * Update test comments * minor changes to tests + review comments * Delete extra tests, update comments * Remove unneeded part of test * Fix comment * Improve comments * Add test for companies() realationship accessibility * Update test name * Complete to-do, add comment * Improve naming and comments (resolve some priority reviews) * Move test * Comment, resolve to-dos * Add low-level pivot assertions * Restore trashed resources if the central resource got restored, try improving tests * Fix code style (php-cs-fixer) * Dekete redundnat unsynced comments * Add to-do, test WIP * Fix restoring logic * Update todo * Add todo to fix phpdoc * Fix code style (php-cs-fixer) * PHPStan error fix wip * Fix PHPStan error * Add regression test * Delete unused trait * Add and test restoring WIP * Fix code style (php-cs-fixer) * Add to-do * Delete comment from test * Focus on restoring in the restore test * Improve maming * Fix stub * Delete redundant part of test * Delete incorrect test leftover * Add triggerRestoredEvent * Fix restore test * Correct tests and restore(() logic * Fix code style (php-cs-fixer) * Check if SoftDeletes are used before firing SyncMasterRestored * Fix comment * Revert restore action changes (phpstan errors) * Delete CascadeDeletes interface * Remove CascadeDeletes from most of the tests * Fix code style (php-cs-fixer) * Rename tests * Fix restoring + tests WIP * Fix restoring * Fix restoring tests * Fix code style (php-cs-fixer) * Test that detaching force deletes the tenant resources * Implement cacscade force deleting * Delete redundant changes * Fix typo * Fix SyncMaster * Improve test * Add force deleting logic back and fix tests * Improve comment * Delete extra assertion * Improve restoring test * Simplify assertion * Delete redundant query scoping from test * Test restore listener queueing * use strict in_array() checks * fix phpstan errors --------- Co-authored-by: lukinovec <lukinovec@gmail.com> Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
257 lines
9.9 KiB
PHP
257 lines
9.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Routing\Route;
|
|
use Stancl\Tenancy\Jobs;
|
|
use Stancl\Tenancy\Events;
|
|
use Stancl\Tenancy\ResourceSyncing;
|
|
use Stancl\Tenancy\Listeners;
|
|
use Stancl\Tenancy\Middleware;
|
|
use Stancl\JobPipeline\JobPipeline;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Stancl\Tenancy\Actions\CloneRoutesAsTenant;
|
|
use Stancl\Tenancy\Overrides\TenancyUrlGenerator;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\Route as RouteFacade;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
|
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
|
|
|
class TenancyServiceProvider extends ServiceProvider
|
|
{
|
|
// By default, no namespace is used to support the callable array syntax.
|
|
public static string $controllerNamespace = '';
|
|
|
|
public function events()
|
|
{
|
|
return [
|
|
// Tenant events
|
|
Events\CreatingTenant::class => [],
|
|
Events\TenantCreated::class => [
|
|
JobPipeline::make([
|
|
Jobs\CreateDatabase::class,
|
|
Jobs\MigrateDatabase::class,
|
|
// Jobs\SeedDatabase::class,
|
|
|
|
// Jobs\CreateStorageSymlinks::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;
|
|
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
|
|
|
|
// Listeners\CreateTenantStorage::class,
|
|
],
|
|
Events\SavingTenant::class => [],
|
|
Events\TenantSaved::class => [],
|
|
Events\UpdatingTenant::class => [],
|
|
Events\TenantUpdated::class => [],
|
|
Events\DeletingTenant::class => [
|
|
JobPipeline::make([
|
|
Jobs\DeleteDomains::class,
|
|
])->send(function (Events\DeletingTenant $event) {
|
|
return $event->tenant;
|
|
})->shouldBeQueued(false),
|
|
|
|
// Listeners\DeleteTenantStorage::class,
|
|
],
|
|
Events\TenantDeleted::class => [
|
|
JobPipeline::make([
|
|
Jobs\DeleteDatabase::class,
|
|
// Jobs\RemoveStorageSymlinks::class,
|
|
])->send(function (Events\TenantDeleted $event) {
|
|
return $event->tenant;
|
|
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
|
|
],
|
|
Events\TenantMaintenanceModeEnabled::class => [],
|
|
Events\TenantMaintenanceModeDisabled::class => [],
|
|
|
|
// Pending tenant events
|
|
Events\CreatingPendingTenant::class => [],
|
|
Events\PendingTenantCreated::class => [],
|
|
Events\PullingPendingTenant::class => [],
|
|
Events\PendingTenantPulled::class => [],
|
|
|
|
// 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
|
|
ResourceSyncing\Events\SyncedResourceSaved::class => [
|
|
ResourceSyncing\Listeners\UpdateOrCreateSyncedResource::class,
|
|
],
|
|
ResourceSyncing\Events\SyncMasterDeleted::class => [
|
|
ResourceSyncing\Listeners\DeleteResourcesInTenants::class,
|
|
],
|
|
ResourceSyncing\Events\SyncMasterRestored::class => [
|
|
ResourceSyncing\Listeners\RestoreResourcesInTenants::class,
|
|
],
|
|
ResourceSyncing\Events\CentralResourceAttachedToTenant::class => [
|
|
ResourceSyncing\Listeners\CreateTenantResource::class,
|
|
],
|
|
ResourceSyncing\Events\CentralResourceDetachedFromTenant::class => [
|
|
ResourceSyncing\Listeners\DeleteResourceInTenant::class,
|
|
],
|
|
// Fired only when a synced resource is changed in a different DB than the origin DB (to avoid infinite loops)
|
|
ResourceSyncing\Events\SyncedResourceSavedInForeignDatabase::class => [],
|
|
|
|
// Storage symlinks
|
|
Events\CreatingStorageSymlink::class => [],
|
|
Events\StorageSymlinkCreated::class => [],
|
|
Events\RemovingStorageSymlink::class => [],
|
|
Events\StorageSymlinkRemoved::class => [],
|
|
];
|
|
}
|
|
|
|
protected function overrideUrlInTenantContext(): void
|
|
{
|
|
/**
|
|
* Example of CLI tenant URL root override:
|
|
*
|
|
* RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant) {
|
|
* $baseUrl = env('APP_URL');
|
|
* $scheme = str($baseUrl)->before('://');
|
|
* $hostname = str($baseUrl)->after($scheme . '://');
|
|
*
|
|
* return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
|
|
* };
|
|
*/
|
|
}
|
|
|
|
public function register()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
$this->bootEvents();
|
|
$this->mapRoutes();
|
|
|
|
$this->makeTenancyMiddlewareHighestPriority();
|
|
$this->overrideUrlInTenantContext();
|
|
|
|
/**
|
|
* Include soft deleted resources in synced resource queries.
|
|
*
|
|
* ResourceSyncing\Listeners\UpdateOrCreateSyncedResource::$scopeGetModelQuery = function (Builder $query) {
|
|
* if ($query->hasMacro('withTrashed')) {
|
|
* $query->withTrashed();
|
|
* }
|
|
* };
|
|
*/
|
|
|
|
/**
|
|
* To make Livewire v3 work with Tenancy, make the update route universal.
|
|
*
|
|
* Livewire::setUpdateRoute(function ($handle) {
|
|
* return Route::post('/livewire/update', $handle)->middleware(['web', 'universal']);
|
|
* });
|
|
*
|
|
* If using domain identification, also make the script route universal.
|
|
*
|
|
* app(FrontendAssets::class)->setScriptRoute(function ($handle) {
|
|
* return Route::get('/livewire/livewire.js', $handle)->middleware(['universal']);
|
|
* });
|
|
*/
|
|
|
|
if (InitializeTenancyByRequestData::inGlobalStack()) {
|
|
TenancyUrlGenerator::$prefixRouteNames = false;
|
|
}
|
|
|
|
if (InitializeTenancyByPath::inGlobalStack()) {
|
|
TenancyUrlGenerator::$prefixRouteNames = true;
|
|
|
|
/** @var CloneRoutesAsTenant $cloneRoutes */
|
|
$cloneRoutes = app(CloneRoutesAsTenant::class);
|
|
|
|
/**
|
|
* You can provide a closure for cloning a specific route, e.g.:
|
|
* $cloneRoutes->cloneUsing('welcome', function () {
|
|
* Route::get('/tenant-welcome', fn () => 'Current tenant: ' . tenant()->getTenantKey())
|
|
* ->middleware(['universal', InitializeTenancyByPath::class])
|
|
* ->name('tenant.welcome');
|
|
* });
|
|
*
|
|
* To make Livewire v2 (2.12.2+) work with kernel path identification,
|
|
* use this closure to override the livewire.message-localized route:
|
|
*
|
|
* $cloneRoutes->cloneUsing('livewire.message-localized', function (Route $route) {
|
|
* $route->setUri(str($route->uri())->replaceFirst('locale', $tenantParameter = PathTenantResolver::tenantParameterName()));
|
|
* $route->parameterNames[0] = $tenantParameter;
|
|
* $route->middleware('tenant');
|
|
* });
|
|
*
|
|
* To see the default behavior of cloning the universal routes, check out the cloneRoute() method in CloneRoutesAsTenant.
|
|
* @see CloneRoutesAsTenant
|
|
*/
|
|
|
|
$cloneRoutes->handle();
|
|
}
|
|
}
|
|
|
|
protected function bootEvents()
|
|
{
|
|
foreach ($this->events() as $event => $listeners) {
|
|
foreach ($listeners as $listener) {
|
|
if ($listener instanceof JobPipeline) {
|
|
$listener = $listener->toListener();
|
|
}
|
|
|
|
Event::listen($event, $listener);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function mapRoutes()
|
|
{
|
|
if (file_exists(base_path('routes/tenant.php'))) {
|
|
RouteFacade::namespace(static::$controllerNamespace)
|
|
->middleware('tenant')
|
|
->group(base_path('routes/tenant.php'));
|
|
}
|
|
}
|
|
|
|
protected function makeTenancyMiddlewareHighestPriority()
|
|
{
|
|
// PreventAccessFromUnwantedDomains has even higher priority than the identification middleware
|
|
$tenancyMiddleware = array_merge([Middleware\PreventAccessFromUnwantedDomains::class], config('tenancy.identification.middleware'));
|
|
|
|
foreach (array_reverse($tenancyMiddleware) as $middleware) {
|
|
$this->app[\Illuminate\Contracts\Http\Kernel::class]->prependToMiddlewarePriority($middleware);
|
|
}
|
|
}
|
|
}
|