mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:04:02 +00:00
Remove symlink-related jobs, instantiate and use actions
This commit is contained in:
parent
182d2495b7
commit
547440c887
9 changed files with 46 additions and 117 deletions
|
|
@ -4,14 +4,15 @@ 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
|
||||
{
|
||||
|
|
@ -28,7 +29,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Jobs\CreateDatabase::class,
|
||||
Jobs\MigrateDatabase::class,
|
||||
// Jobs\SeedDatabase::class,
|
||||
Jobs\CreateStorageSymlinks::class,
|
||||
Actions\CreateStorageSymlinks::class,
|
||||
|
||||
// Your own jobs to prepare the tenant.
|
||||
// Provision API keys, create S3 buckets, anything you want!
|
||||
|
|
@ -53,7 +54,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Events\TenantDeleted::class => [
|
||||
JobPipeline::make([
|
||||
Jobs\DeleteDatabase::class,
|
||||
Jobs\RemoveStorageSymlinks::class,
|
||||
Actions\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.
|
||||
|
|
|
|||
|
|
@ -12,29 +12,33 @@ use Stancl\Tenancy\Database\Models\Tenant;
|
|||
use Stancl\Tenancy\Events\CreatingStorageSymlink;
|
||||
use Stancl\Tenancy\Events\StorageSymlinkCreated;
|
||||
|
||||
class CreateStorageSymlinksAction
|
||||
class CreateStorageSymlinks
|
||||
{
|
||||
use DealsWithTenantSymlinks;
|
||||
|
||||
public static function handle(Tenant|Collection|LazyCollection $tenants, bool $relativeLink = false, bool $force = false): void
|
||||
public function __construct(protected Tenant|Collection|LazyCollection $tenants, protected bool $relativeLink = false, protected bool $force = false)
|
||||
{
|
||||
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$tenants = $this->tenants instanceof Tenant ? collect([$this->tenants]) : $this->tenants;
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
foreach ($tenants as $tenant) {
|
||||
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
|
||||
static::createLink($publicPath, $storagePath, $tenant, $relativeLink, $force);
|
||||
foreach ($this->possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
|
||||
$this->createLink((string) $publicPath, (string) $storagePath, $tenant);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function createLink(string $publicPath, string $storagePath, Tenant $tenant, bool $relativeLink, bool $force): void
|
||||
protected function createLink(string $publicPath, string $storagePath, Tenant $tenant): void
|
||||
{
|
||||
event(new CreatingStorageSymlink($tenant));
|
||||
|
||||
if (static::symlinkExists($publicPath)) {
|
||||
if ($this->symlinkExists($publicPath)) {
|
||||
// If $force isn't passed, don't overwrite the existing symlink
|
||||
throw_if(! $force, new Exception("The [$publicPath] link already exists."));
|
||||
throw_if(! $this->force, new Exception("The [$publicPath] link already exists."));
|
||||
|
||||
app()->make('files')->delete($publicPath);
|
||||
}
|
||||
|
|
@ -44,7 +48,7 @@ class CreateStorageSymlinksAction
|
|||
mkdir($storagePath, 0777, true);
|
||||
}
|
||||
|
||||
if ($relativeLink) {
|
||||
if ($this->relativeLink) {
|
||||
app()->make('files')->relativeLink($storagePath, $publicPath);
|
||||
} else {
|
||||
app()->make('files')->link($storagePath, $publicPath);
|
||||
|
|
@ -11,25 +11,29 @@ use Stancl\Tenancy\Database\Models\Tenant;
|
|||
use Stancl\Tenancy\Events\RemovingStorageSymlink;
|
||||
use Stancl\Tenancy\Events\StorageSymlinkRemoved;
|
||||
|
||||
class RemoveStorageSymlinksAction
|
||||
class RemoveStorageSymlinks
|
||||
{
|
||||
use DealsWithTenantSymlinks;
|
||||
|
||||
public static function handle(Tenant|Collection|LazyCollection $tenants): void
|
||||
public function __construct(protected Tenant|Collection|LazyCollection $tenants)
|
||||
{
|
||||
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;
|
||||
}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$tenants = $this->tenants instanceof Tenant ? collect([$this->tenants]) : $this->tenants;
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
foreach ($tenants as $tenant) {
|
||||
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
|
||||
static::removeLink($publicPath, $tenant);
|
||||
foreach ($this->possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
|
||||
$this->removeLink((string) $publicPath, $tenant);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static function removeLink(string $publicPath, Tenant $tenant): void
|
||||
protected function removeLink(string $publicPath, Tenant $tenant): void
|
||||
{
|
||||
if (static::symlinkExists($publicPath)) {
|
||||
if ($this->symlinkExists($publicPath)) {
|
||||
event(new RemovingStorageSymlink($tenant));
|
||||
|
||||
app()->make('files')->delete($publicPath);
|
||||
|
|
@ -7,8 +7,8 @@ namespace Stancl\Tenancy\Commands;
|
|||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinks;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
|
||||
class Link extends Command
|
||||
|
|
@ -55,18 +55,18 @@ class Link extends Command
|
|||
|
||||
protected function removeLinks(LazyCollection $tenants): void
|
||||
{
|
||||
RemoveStorageSymlinksAction::handle($tenants);
|
||||
(new RemoveStorageSymlinks($tenants))->handle();
|
||||
|
||||
$this->info('The links have been removed.');
|
||||
}
|
||||
|
||||
protected function createLinks(LazyCollection $tenants): void
|
||||
{
|
||||
CreateStorageSymlinksAction::handle(
|
||||
(new CreateStorageSymlinks(
|
||||
$tenants,
|
||||
$this->option('relative') ?? false,
|
||||
$this->option('force') ?? false,
|
||||
);
|
||||
))->handle();
|
||||
|
||||
$this->info('The links have been created.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 static function possibleTenantSymlinks(Tenant $tenant): Collection
|
||||
protected 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 static function symlinkExists(string $link): bool
|
||||
protected function symlinkExists(string $link): bool
|
||||
{
|
||||
return file_exists($link) && is_link($link);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
<?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 Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class CreateStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public Tenant $tenant;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Tenant $tenant)
|
||||
{
|
||||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
CreateStorageSymlinksAction::handle($this->tenant);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
<?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 Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class RemoveStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public Tenant $tenant;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Tenant $tenant)
|
||||
{
|
||||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
RemoveStorageSymlinksAction::handle($this->tenant);
|
||||
}
|
||||
}
|
||||
|
|
@ -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\CreateStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinks;
|
||||
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"));
|
||||
|
||||
CreateStorageSymlinksAction::handle($tenant);
|
||||
(new CreateStorageSymlinks($tenant))->handle();
|
||||
|
||||
$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);
|
||||
|
||||
CreateStorageSymlinksAction::handle($tenant);
|
||||
(new CreateStorageSymlinks($tenant))->handle();
|
||||
|
||||
$this->assertDirectoryExists($publicPath = public_path("public-$tenantKey"));
|
||||
|
||||
RemoveStorageSymlinksAction::handle($tenant);
|
||||
(new RemoveStorageSymlinks($tenant))->handle();
|
||||
|
||||
$this->assertDirectoryDoesNotExist($publicPath);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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\Jobs\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinks;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinks;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\DeleteTenantStorage;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue