Skip to content
5 changes: 1 addition & 4 deletions assets/TenancyServiceProvider.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public function events()
])->send(function (Events\TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(false),

// Listeners\CreateTenantStorage::class,
],
Events\SavingTenant::class => [],
Events\TenantSaved::class => [],
Expand All @@ -63,12 +61,11 @@ public function events()
Events\DeletingTenant::class => [
JobPipeline::make([
Jobs\DeleteDomains::class,
// Jobs\DeleteTenantStorage::class,
// Jobs\RemoveStorageSymlinks::class,
])->send(function (Events\DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false),

// Listeners\DeleteTenantStorage::class,
],
Events\TenantDeleted::class => [
JobPipeline::make([
Expand Down
43 changes: 43 additions & 0 deletions src/Jobs/DeleteTenantStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Contracts\Tenant;

class DeleteTenantStorage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public Tenant $tenant,
) {}

public function handle(): void
{
if (config('tenancy.filesystem.suffix_storage_path') === false) {
// Skip storage deletion if path suffixing is disabled
return;
}

$centralStoragePath = tenancy()->central(fn () => storage_path());
$tenantStoragePath = tenancy()->run($this->tenant, fn () => storage_path());

if ($tenantStoragePath === $centralStoragePath) {
// Check again to ensure the tenant storage path is distinct from the central storage path
// to avoid any accidental central storage path deletion
return;
}

if (is_dir($tenantStoragePath)) {
File::deleteDirectory($tenantStoragePath);
}
}
}
6 changes: 1 addition & 5 deletions src/Listeners/CreateTenantStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
use Stancl\Tenancy\Events\Contracts\TenantEvent;

/**
* Can be used to manually create framework directories in the tenant storage when storage_path() is scoped.
*
* Useful when using real-time facades which use the framework/cache directory.
*
* Generally not needed anymore as the directory is also created by the FilesystemTenancyBootstrapper.
* @deprecated FilesystemTenancyBootstrapper creates the path automatically when suffix_storage_path is enabled.
*/
class CreateTenantStorage
{
Expand Down
21 changes: 18 additions & 3 deletions src/Listeners/DeleteTenantStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@
use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Events\Contracts\TenantEvent;

/**
* @deprecated Use Stancl\Tenancy\Jobs\DeleteTenantStorage in a job pipeline instead.
*/
class DeleteTenantStorage
{
public function handle(TenantEvent $event): void
{
$path = tenancy()->run($event->tenant, fn () => storage_path());
if (config('tenancy.filesystem.suffix_storage_path') === false) {
// Skip storage deletion if path suffixing is disabled
return;
}

$centralStoragePath = tenancy()->central(fn () => storage_path());
$tenantStoragePath = tenancy()->run($event->tenant, fn () => storage_path());

if ($tenantStoragePath === $centralStoragePath) {
// Check again to ensure the tenant storage path is distinct from the central storage path
// to avoid any accidental central storage path deletion
return;
}

if (is_dir($path)) {
File::deleteDirectory($path);
if (is_dir($tenantStoragePath)) {
File::deleteDirectory($tenantStoragePath);
}
}
}
55 changes: 48 additions & 7 deletions tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
use Stancl\Tenancy\Jobs\DeleteTenantStorage;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use function Stancl\Tenancy\Tests\pest;
Expand Down Expand Up @@ -184,21 +184,63 @@
$this->assertDirectoryDoesNotExist(public_path("public-$tenantKey"));
});

test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function() {
Event::listen(DeletingTenant::class, DeleteTenantStorage::class);
test('tenant storage gets deleted during tenant deletion when the DeletingTenant pipeline contains DeleteTenantStorage', function() {
Event::listen(DeletingTenant::class,
JobPipeline::make([DeleteTenantStorage::class])->send(function (DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false)->toListener()
);

$centralStoragePath = storage_path();
tenancy()->initialize(Tenant::create());

// FilesystemTenancyBootstrapper not enabled,
// tenant and central storage path is the same,
// the storage deletion will be skipped.
$tenantStoragePath = storage_path();
expect($tenantStoragePath)->toBe($centralStoragePath);
Comment thread
stancl marked this conversation as resolved.
expect(File::isDirectory($centralStoragePath))->toBeTrue();
tenant()->delete();

Storage::fake('test');
expect(File::isDirectory($centralStoragePath))->toBeTrue();

expect(File::isDirectory($tenantStoragePath))->toBeTrue();
config([
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
'tenancy.filesystem.suffix_storage_path' => false,
]);

tenancy()->initialize(Tenant::create());

$tenantStoragePath = storage_path();

// FilesystemTenancyBootstrapper enabled,
// but tenant and central storage path is still the same
// because suffix_storage_path is false.
// The storage deletion will be skipped.
expect($tenantStoragePath)->toBe($centralStoragePath);
expect(File::isDirectory($centralStoragePath))->toBeTrue();
tenant()->delete();

expect(File::isDirectory($centralStoragePath))->toBeTrue();

Storage::put('test.txt', 'testing file');
config([
'tenancy.bootstrappers' => [FilesystemTenancyBootstrapper::class],
'tenancy.filesystem.suffix_storage_path' => true,
]);

tenancy()->initialize(Tenant::create());
$tenantStoragePath = storage_path();

// FilesystemTenancyBootstrapper enabled,
// suffix_storage_path enabled, so the two paths are distinct.
// Tenant storage will be deleted.
expect($tenantStoragePath)->not()->toBe($centralStoragePath);
expect(File::isDirectory($tenantStoragePath))->toBeTrue();

tenant()->delete();

expect(File::isDirectory($tenantStoragePath))->toBeFalse();
expect(File::isDirectory($centralStoragePath))->toBeTrue();
});

test('the framework/cache directory is created when storage_path is scoped', function (bool $suffixStoragePath) {
Expand Down Expand Up @@ -256,4 +298,3 @@
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
});

Loading