mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 19:54:04 +00:00
Add DeleteTenantStorage listener
This commit is contained in:
parent
624f032775
commit
29ba72fe53
5 changed files with 44 additions and 7 deletions
|
|
@ -53,6 +53,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
])->send(function (Events\TenantDeleted $event) {
|
||||
return $event->tenant;
|
||||
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
|
||||
// Listeners\DeleteTenantStorage::class,
|
||||
],
|
||||
|
||||
// Domain events
|
||||
|
|
|
|||
9
src/Events/DeletingTenantStorage.php
Normal file
9
src/Events/DeletingTenantStorage.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class DeletingTenantStorage extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/TenantStorageDeleted.php
Normal file
9
src/Events/TenantStorageDeleted.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class TenantStorageDeleted extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
22
src/Listeners/DeleteTenantStorage.php
Normal file
22
src/Listeners/DeleteTenantStorage.php
Normal 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));
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
|
|||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Events\TenantDeleted;
|
||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->mockConsoleOutput = false;
|
||||
|
|
@ -186,19 +187,14 @@ test('filesystem data is separated', function () {
|
|||
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([
|
||||
'tenancy.bootstrappers' => [
|
||||
FilesystemTenancyBootstrapper::class,
|
||||
],
|
||||
'tenancy.filesystem.delete_storage_after_tenant_deletion' => true
|
||||
]);
|
||||
|
||||
Event::listen(TenantDeleted::class, function(TenantDeleted $event) {
|
||||
if (config('tenancy.filesystem.delete_storage_after_tenant_deletion')) {
|
||||
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
|
||||
}
|
||||
});
|
||||
Event::listen(TenantDeleted::class, DeleteTenantStorage::class);
|
||||
|
||||
tenancy()->initialize(Tenant::create());
|
||||
$tenantStoragePath = storage_path();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue