diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 1cb358de0..050a2ffcd 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -46,6 +46,7 @@ public function events() Jobs\CreateDatabase::class, Jobs\MigrateDatabase::class, // Jobs\SeedDatabase::class, + // Jobs\CreateTenantStorage::class, // Jobs\CreateStorageSymlinks::class, // Your own jobs to prepare the tenant. @@ -53,8 +54,6 @@ public function events() ])->send(function (Events\TenantCreated $event) { return $event->tenant; })->shouldBeQueued(false), - - // Listeners\CreateTenantStorage::class, ], Events\SavingTenant::class => [], Events\TenantSaved::class => [], @@ -63,12 +62,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([ diff --git a/src/Listeners/CreateTenantStorage.php b/src/Jobs/CreateTenantStorage.php similarity index 51% rename from src/Listeners/CreateTenantStorage.php rename to src/Jobs/CreateTenantStorage.php index 3bebb7310..8e4e15a91 100644 --- a/src/Listeners/CreateTenantStorage.php +++ b/src/Jobs/CreateTenantStorage.php @@ -2,9 +2,14 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Listeners; +namespace Stancl\Tenancy\Jobs; -use Stancl\Tenancy\Events\Contracts\TenantEvent; +use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; +use Stancl\Tenancy\Contracts\Tenant; /** * Can be used to manually create framework directories in the tenant storage when storage_path() is scoped. @@ -13,11 +18,17 @@ * * Generally not needed anymore as the directory is also created by the FilesystemTenancyBootstrapper. */ -class CreateTenantStorage +class CreateTenantStorage implements ShouldQueue { - public function handle(TenantEvent $event): void + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + + public function __construct( + public Tenant $tenant, + ) {} + + public function handle(): void { - $storage_path = tenancy()->run($event->tenant, fn () => storage_path()); + $storage_path = tenancy()->run($this->tenant, fn () => storage_path()); $cache_path = "$storage_path/framework/cache"; if (! is_dir($cache_path)) { diff --git a/src/Jobs/DeleteTenantStorage.php b/src/Jobs/DeleteTenantStorage.php new file mode 100644 index 000000000..c0329c502 --- /dev/null +++ b/src/Jobs/DeleteTenantStorage.php @@ -0,0 +1,31 @@ +run($this->tenant, fn () => storage_path()); + + if (is_dir($path)) { + File::deleteDirectory($path); + } + } +} diff --git a/src/Listeners/DeleteTenantStorage.php b/src/Listeners/DeleteTenantStorage.php deleted file mode 100644 index ec360073e..000000000 --- a/src/Listeners/DeleteTenantStorage.php +++ /dev/null @@ -1,20 +0,0 @@ -run($event->tenant, fn () => storage_path()); - - if (is_dir($path)) { - File::deleteDirectory($path); - } - } -} diff --git a/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php b/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php index 628b974e3..4594d7aa9 100644 --- a/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php +++ b/tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php @@ -13,7 +13,8 @@ use Stancl\Tenancy\Jobs\CreateStorageSymlinks; use Stancl\Tenancy\Jobs\RemoveStorageSymlinks; use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\DeleteTenantStorage; +use Stancl\Tenancy\Jobs\CreateTenantStorage; +use Stancl\Tenancy\Jobs\DeleteTenantStorage; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use function Stancl\Tenancy\Tests\pest; @@ -184,8 +185,32 @@ $this->assertDirectoryDoesNotExist(public_path("public-$tenantKey")); }); +test('tenant storage gets created when TenantCreated listens to CreateTenantStorage', function() { + config([ + 'tenancy.bootstrappers' => [ + FilesystemTenancyBootstrapper::class, + ], + ]); + + Event::listen(TenantCreated::class, + JobPipeline::make([CreateTenantStorage::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->shouldBeQueued(false)->toListener() + ); + + $centralStoragePath = storage_path(); + $tenant = Tenant::create(); + $tenantStoragePath = $centralStoragePath . '/tenant' . $tenant->getTenantKey(); + + $this->assertDirectoryExists($tenantStoragePath . '/framework/cache'); +}); + test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function() { - Event::listen(DeletingTenant::class, DeleteTenantStorage::class); + Event::listen(DeletingTenant::class, + JobPipeline::make([DeleteTenantStorage::class])->send(function (DeletingTenant $event) { + return $event->tenant; + })->shouldBeQueued(false)->toListener() + ); tenancy()->initialize(Tenant::create()); $tenantStoragePath = storage_path(); @@ -256,4 +281,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'); }); -