diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 43476811..a3626225 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -4,15 +4,14 @@ declare(strict_types=1); namespace App\Providers; -use Stancl\Tenancy\Jobs; -use Stancl\Tenancy\Actions; -use Stancl\Tenancy\Events; -use Stancl\Tenancy\Listeners; -use Stancl\Tenancy\Middleware; -use Stancl\JobPipeline\JobPipeline; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Events; +use Stancl\Tenancy\Jobs; +use Stancl\Tenancy\Listeners; +use Stancl\Tenancy\Middleware; class TenancyServiceProvider extends ServiceProvider { @@ -29,7 +28,7 @@ class TenancyServiceProvider extends ServiceProvider Jobs\CreateDatabase::class, Jobs\MigrateDatabase::class, // Jobs\SeedDatabase::class, - Actions\CreateStorageSymlinks::class, + Jobs\CreateStorageSymlinks::class, // Your own jobs to prepare the tenant. // Provision API keys, create S3 buckets, anything you want! @@ -54,7 +53,7 @@ class TenancyServiceProvider extends ServiceProvider Events\TenantDeleted::class => [ JobPipeline::make([ Jobs\DeleteDatabase::class, - Actions\RemoveStorageSymlinks::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. diff --git a/src/Actions/CreateStorageSymlinks.php b/src/Actions/CreateStorageSymlinksAction.php similarity index 58% rename from src/Actions/CreateStorageSymlinks.php rename to src/Actions/CreateStorageSymlinksAction.php index 4e2c60f2..779a42af 100644 --- a/src/Actions/CreateStorageSymlinks.php +++ b/src/Actions/CreateStorageSymlinksAction.php @@ -12,33 +12,29 @@ use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Events\CreatingStorageSymlink; use Stancl\Tenancy\Events\StorageSymlinkCreated; -class CreateStorageSymlinks +class CreateStorageSymlinksAction { use DealsWithTenantSymlinks; - public function __construct(protected Tenant|Collection|LazyCollection $tenants, protected bool $relativeLink = false, protected bool $force = false) + public static function handle(Tenant|Collection|LazyCollection $tenants, bool $relativeLink = false, bool $force = false): void { - } - - public function handle(): void - { - $tenants = $this->tenants instanceof Tenant ? collect([$this->tenants]) : $this->tenants; + $tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; /** @var Tenant $tenant */ foreach ($tenants as $tenant) { - foreach ($this->possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { - $this->createLink((string) $publicPath, (string) $storagePath, $tenant); + foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { + static::createLink($publicPath, $storagePath, $tenant, $relativeLink, $force); } } } - protected function createLink(string $publicPath, string $storagePath, Tenant $tenant): void + protected static function createLink(string $publicPath, string $storagePath, Tenant $tenant, bool $relativeLink, bool $force): void { event(new CreatingStorageSymlink($tenant)); - if ($this->symlinkExists($publicPath)) { + if (static::symlinkExists($publicPath)) { // If $force isn't passed, don't overwrite the existing symlink - throw_if(! $this->force, new Exception("The [$publicPath] link already exists.")); + throw_if(! $force, new Exception("The [$publicPath] link already exists.")); app()->make('files')->delete($publicPath); } @@ -48,7 +44,7 @@ class CreateStorageSymlinks mkdir($storagePath, 0777, true); } - if ($this->relativeLink) { + if ($relativeLink) { app()->make('files')->relativeLink($storagePath, $publicPath); } else { app()->make('files')->link($storagePath, $publicPath); diff --git a/src/Actions/RemoveStorageSymlinks.php b/src/Actions/RemoveStorageSymlinksAction.php similarity index 55% rename from src/Actions/RemoveStorageSymlinks.php rename to src/Actions/RemoveStorageSymlinksAction.php index b546966c..bfbcfa0a 100644 --- a/src/Actions/RemoveStorageSymlinks.php +++ b/src/Actions/RemoveStorageSymlinksAction.php @@ -11,29 +11,25 @@ use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Events\RemovingStorageSymlink; use Stancl\Tenancy\Events\StorageSymlinkRemoved; -class RemoveStorageSymlinks +class RemoveStorageSymlinksAction { use DealsWithTenantSymlinks; - public function __construct(protected Tenant|Collection|LazyCollection $tenants) + public static function handle(Tenant|Collection|LazyCollection $tenants): void { - } - - public function handle(): void - { - $tenants = $this->tenants instanceof Tenant ? collect([$this->tenants]) : $this->tenants; + $tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; /** @var Tenant $tenant */ foreach ($tenants as $tenant) { - foreach ($this->possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { - $this->removeLink((string) $publicPath, $tenant); + foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { + static::removeLink($publicPath, $tenant); } } } - protected function removeLink(string $publicPath, Tenant $tenant): void + protected static function removeLink(string $publicPath, Tenant $tenant): void { - if ($this->symlinkExists($publicPath)) { + if (static::symlinkExists($publicPath)) { event(new RemovingStorageSymlink($tenant)); app()->make('files')->delete($publicPath); diff --git a/src/Commands/Link.php b/src/Commands/Link.php index d1fc7a6c..061f2d3d 100644 --- a/src/Commands/Link.php +++ b/src/Commands/Link.php @@ -7,8 +7,8 @@ namespace Stancl\Tenancy\Commands; use Exception; use Illuminate\Console\Command; use Illuminate\Support\LazyCollection; -use Stancl\Tenancy\Actions\CreateStorageSymlinks; -use Stancl\Tenancy\Actions\RemoveStorageSymlinks; +use Stancl\Tenancy\Actions\CreateStorageSymlinksAction; +use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction; use Stancl\Tenancy\Concerns\HasATenantsOption; class Link extends Command @@ -55,18 +55,18 @@ class Link extends Command protected function removeLinks(LazyCollection $tenants): void { - (new RemoveStorageSymlinks($tenants))->handle(); + RemoveStorageSymlinksAction::handle($tenants); $this->info('The links have been removed.'); } protected function createLinks(LazyCollection $tenants): void { - (new CreateStorageSymlinks( + CreateStorageSymlinksAction::handle( $tenants, $this->option('relative') ?? false, $this->option('force') ?? false, - ))->handle(); + ); $this->info('The links have been created.'); } diff --git a/src/Concerns/DealsWithTenantSymlinks.php b/src/Concerns/DealsWithTenantSymlinks.php index d9fe23dc..c2813bc8 100644 --- a/src/Concerns/DealsWithTenantSymlinks.php +++ b/src/Concerns/DealsWithTenantSymlinks.php @@ -13,7 +13,7 @@ trait DealsWithTenantSymlinks * Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']). * This is used for creating all possible tenant symlinks and removing all existing tenant symlinks. */ - protected function possibleTenantSymlinks(Tenant $tenant): Collection + protected static function possibleTenantSymlinks(Tenant $tenant): Collection { $diskUrls = config('tenancy.filesystem.url_override'); $disks = config('tenancy.filesystem.root_override'); @@ -34,7 +34,7 @@ trait DealsWithTenantSymlinks } /** Determine if the provided path is an existing symlink. */ - protected function symlinkExists(string $link): bool + protected static function symlinkExists(string $link): bool { return file_exists($link) && is_link($link); } diff --git a/src/Jobs/CreateStorageSymlinks.php b/src/Jobs/CreateStorageSymlinks.php new file mode 100644 index 00000000..4f18bb03 --- /dev/null +++ b/src/Jobs/CreateStorageSymlinks.php @@ -0,0 +1,40 @@ +tenant = $tenant; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + CreateStorageSymlinksAction::handle($this->tenant); + } +} diff --git a/src/Jobs/RemoveStorageSymlinks.php b/src/Jobs/RemoveStorageSymlinks.php new file mode 100644 index 00000000..3022da79 --- /dev/null +++ b/src/Jobs/RemoveStorageSymlinks.php @@ -0,0 +1,40 @@ +tenant = $tenant; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + RemoveStorageSymlinksAction::handle($this->tenant); + } +} diff --git a/tests/ActionTest.php b/tests/ActionTest.php index 1a63c3d1..42d190f2 100644 --- a/tests/ActionTest.php +++ b/tests/ActionTest.php @@ -8,8 +8,8 @@ use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Actions\CreateStorageSymlinks; -use Stancl\Tenancy\Actions\RemoveStorageSymlinks; +use Stancl\Tenancy\Actions\CreateStorageSymlinksAction; +use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; beforeEach(function () { @@ -35,7 +35,7 @@ test('create storage symlinks action works', function() { $this->assertDirectoryDoesNotExist($publicPath = public_path("public-$tenantKey")); - (new CreateStorageSymlinks($tenant))->handle(); + CreateStorageSymlinksAction::handle($tenant); $this->assertDirectoryExists($publicPath); $this->assertEquals(storage_path("app/public/"), readlink($publicPath)); @@ -57,11 +57,11 @@ test('remove storage symlinks action works', function() { tenancy()->initialize($tenant); - (new CreateStorageSymlinks($tenant))->handle(); + CreateStorageSymlinksAction::handle($tenant); $this->assertDirectoryExists($publicPath = public_path("public-$tenantKey")); - (new RemoveStorageSymlinks($tenant))->handle(); + RemoveStorageSymlinksAction::handle($tenant); $this->assertDirectoryDoesNotExist($publicPath); }); diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 78c7a8b7..a610fbd2 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -18,8 +18,8 @@ use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Events\DeletingTenant; use Illuminate\Filesystem\FilesystemAdapter; use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Actions\CreateStorageSymlinks; -use Stancl\Tenancy\Actions\RemoveStorageSymlinks; +use Stancl\Tenancy\Jobs\CreateStorageSymlinks; +use Stancl\Tenancy\Jobs\RemoveStorageSymlinks; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\DeleteTenantStorage; use Stancl\Tenancy\Listeners\RevertToCentralContext;