1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 01:44:04 +00:00

Remove afterLink closures, add types, move actions, add usage explanation to the symlink trait

This commit is contained in:
lukinovec 2022-08-25 07:04:44 +02:00
parent f2d562cd8b
commit c4f65afa0a
6 changed files with 19 additions and 43 deletions

View file

@ -2,9 +2,8 @@
declare(strict_types=1); declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy\Actions;
use Closure;
use Exception; use Exception;
use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks; use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks;
use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Database\Models\Tenant;
@ -15,19 +14,19 @@ class CreateStorageSymlinksAction
{ {
use DealsWithTenantSymlinks; use DealsWithTenantSymlinks;
public static function handle($tenants, bool $relativeLink = false, bool $force = false, Closure|null $afterLinkCreation = null) public static function handle($tenants, bool $relativeLink = false, bool $force = false): void
{ {
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; $tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;
/** @var Tenant $tenant */ /** @var Tenant $tenant */
foreach ($tenants as $tenant) { foreach ($tenants as $tenant) {
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
static::createLink($publicPath, $storagePath, $tenant, $relativeLink, $force, $afterLinkCreation); static::createLink($publicPath, $storagePath, $tenant, $relativeLink, $force);
} }
} }
} }
protected static function createLink(string $publicPath, string $storagePath, Tenant $tenant, bool $relativeLink, bool $force, Closure|null $afterLinkCreation) protected static function createLink(string $publicPath, string $storagePath, Tenant $tenant, bool $relativeLink, bool $force): void
{ {
event(new CreatingStorageSymlink($tenant)); event(new CreatingStorageSymlink($tenant));
@ -50,9 +49,5 @@ class CreateStorageSymlinksAction
} }
event((new StorageSymlinkCreated($tenant))); event((new StorageSymlinkCreated($tenant)));
if ($afterLinkCreation) {
$afterLinkCreation($publicPath, $storagePath);
}
} }
} }

View file

@ -2,9 +2,8 @@
declare(strict_types=1); declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy\Actions;
use Closure;
use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks; use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks;
use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Events\RemovingStorageSymlink; use Stancl\Tenancy\Events\RemovingStorageSymlink;
@ -14,19 +13,19 @@ class RemoveStorageSymlinksAction
{ {
use DealsWithTenantSymlinks; use DealsWithTenantSymlinks;
public static function handle($tenants, Closure|null $afterLinkRemoval = null) public static function handle($tenants): void
{ {
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; $tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants;
/** @var Tenant $tenant */ /** @var Tenant $tenant */
foreach ($tenants as $tenant) { foreach ($tenants as $tenant) {
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) {
static::removeLink($publicPath, $tenant, $afterLinkRemoval); static::removeLink($publicPath, $tenant);
} }
} }
} }
protected static function removeLink(string $publicPath, Tenant $tenant, Closure|null $afterLinkRemoval) protected static function removeLink(string $publicPath, Tenant $tenant): void
{ {
if (static::symlinkExists($publicPath)) { if (static::symlinkExists($publicPath)) {
event(new RemovingStorageSymlink($tenant)); event(new RemovingStorageSymlink($tenant));
@ -34,10 +33,6 @@ class RemoveStorageSymlinksAction
app()->make('files')->delete($publicPath); app()->make('files')->delete($publicPath);
event(new StorageSymlinkRemoved($tenant)); event(new StorageSymlinkRemoved($tenant));
if ($afterLinkRemoval) {
$afterLinkRemoval($publicPath);
}
} }
} }
} }

View file

@ -8,8 +8,8 @@ use Exception;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\LazyCollection; use Illuminate\Support\LazyCollection;
use Stancl\Tenancy\Concerns\HasATenantsOption; use Stancl\Tenancy\Concerns\HasATenantsOption;
use Stancl\Tenancy\CreateStorageSymlinksAction; use Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
use Stancl\Tenancy\RemoveStorageSymlinksAction; use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
class Link extends Command class Link extends Command
{ {
@ -55,31 +55,19 @@ class Link extends Command
} }
} }
/** protected function removeLinks(LazyCollection $tenants): void
* @param LazyCollection $tenants
* @return void
*/
protected function removeLinks($tenants)
{ {
RemoveStorageSymlinksAction::handle( RemoveStorageSymlinksAction::handle($tenants);
$tenants,
afterLinkRemoval: fn ($publicPath) => $this->info("The [$publicPath] link has been removed.")
);
$this->info('The links have been removed.'); $this->info('The links have been removed.');
} }
/** protected function createLinks(LazyCollection $tenants): void
* @param LazyCollection $tenants
* @return void
*/
protected function createLinks($tenants)
{ {
CreateStorageSymlinksAction::handle( CreateStorageSymlinksAction::handle(
$tenants, $tenants,
$this->option('relative') ?? false, $this->option('relative') ?? false,
$this->option('force') ?? false, $this->option('force') ?? false,
afterLinkCreation: fn ($publicPath, $storagePath) => $this->info("The [$publicPath] link has been connected to [$storagePath].")
); );
$this->info('The links have been created.'); $this->info('The links have been created.');

View file

@ -11,6 +11,7 @@ trait DealsWithTenantSymlinks
{ {
/** /**
* Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']). * 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.
* *
* @return array * @return array
*/ */
@ -19,20 +20,17 @@ trait DealsWithTenantSymlinks
$diskUrls = config('tenancy.filesystem.url_override'); $diskUrls = config('tenancy.filesystem.url_override');
$disks = config('tenancy.filesystem.root_override'); $disks = config('tenancy.filesystem.root_override');
$suffixBase = config('tenancy.filesystem.suffix_base'); $suffixBase = config('tenancy.filesystem.suffix_base');
$symlinks = []; $symlinks = collect();
$tenantKey = $tenant->getTenantKey(); $tenantKey = $tenant->getTenantKey();
foreach ($diskUrls as $disk => $publicPath) { foreach ($diskUrls as $disk => $publicPath) {
$storagePath = str_replace('%storage_path%', $suffixBase . $tenantKey, $disks[$disk]); $storagePath = str_replace('%storage_path%', $suffixBase . $tenantKey, $disks[$disk]);
$storagePath = storage_path($storagePath);
$publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath); $publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath);
$publicPath = public_path($publicPath);
$symlinks[] = [$publicPath => $storagePath]; $symlinks->push([public_path($publicPath) => storage_path($storagePath)]);
} }
return collect($symlinks)->mapWithKeys(fn ($item) => $item); return $symlinks->mapWithKeys(fn ($item) => $item);
} }
/** /**

View file

@ -10,7 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\CreateStorageSymlinksAction; use Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
class CreateStorageSymlinks implements ShouldQueue class CreateStorageSymlinks implements ShouldQueue
{ {

View file

@ -10,7 +10,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\RemoveStorageSymlinksAction; use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
class RemoveStorageSymlinks implements ShouldQueue class RemoveStorageSymlinks implements ShouldQueue
{ {