1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 22:24:03 +00:00

Add DeleteTenantStorage listener

This commit is contained in:
lukinovec 2022-09-12 08:58:27 +02:00
parent 624f032775
commit 29ba72fe53
5 changed files with 44 additions and 7 deletions

View file

@ -53,6 +53,7 @@ class TenancyServiceProvider extends ServiceProvider
])->send(function (Events\TenantDeleted $event) { ])->send(function (Events\TenantDeleted $event) {
return $event->tenant; return $event->tenant;
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production. })->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
// Listeners\DeleteTenantStorage::class,
], ],
// Domain events // Domain events

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class DeletingTenantStorage extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class TenantStorageDeleted extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Listeners;
use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Events\DeletingTenantStorage;
use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Events\TenantStorageDeleted;
class DeleteTenantStorage
{
public function handle(TenantDeleted $event): void
{
event(new DeletingTenantStorage($event->tenant));
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
event(new TenantStorageDeleted($event->tenant));
}
}

View file

@ -23,6 +23,7 @@ use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
beforeEach(function () { beforeEach(function () {
$this->mockConsoleOutput = false; $this->mockConsoleOutput = false;
@ -186,19 +187,14 @@ test('filesystem data is separated', function () {
expect($new_storage_path)->toEqual($expected_storage_path); expect($new_storage_path)->toEqual($expected_storage_path);
}); });
test('tenant storage gets deleted after the tenant if filesystem.delete_storage_after_tenant_deletion is true', function () { test('tenant storage gets deleted after the tenant', function () {
config([ config([
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class, FilesystemTenancyBootstrapper::class,
], ],
'tenancy.filesystem.delete_storage_after_tenant_deletion' => true
]); ]);
Event::listen(TenantDeleted::class, function(TenantDeleted $event) { Event::listen(TenantDeleted::class, DeleteTenantStorage::class);
if (config('tenancy.filesystem.delete_storage_after_tenant_deletion')) {
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
}
});
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
$tenantStoragePath = storage_path(); $tenantStoragePath = storage_path();