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

Refactor Link command

This commit is contained in:
lukinovec 2022-08-09 10:56:48 +02:00
parent 17f11f5ec7
commit a45f02445c

View file

@ -6,7 +6,6 @@ namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Stancl\Tenancy\Concerns\HasATenantsOption; use Stancl\Tenancy\Concerns\HasATenantsOption;
use Stancl\Tenancy\Contracts\Tenant;
class Link extends Command class Link extends Command
{ {
@ -37,62 +36,82 @@ class Link extends Command
*/ */
public function handle() public function handle()
{ {
$relative = $this->option('relative'); $tenants = collect($this->option('tenants')) ?? $this->getTenants()->map->getTenantKey();
$links = $tenants->flatMap(fn ($tenantKey) => $this->getPossibleTenantSymlinks($tenantKey))
->mapWithKeys(fn ($item) => $item)
->all();
if ($this->option('remove')) { if ($this->option('remove')) {
foreach ($this->links() as $link => $target) { $this->removeLinks($links);
if (is_link($link)) { } else {
$this->laravel->make('files')->delete($link); $this->createLinks($links);
$this->info("The [$link] link has been removed.");
} }
} }
protected function removeLinks(array $links)
{
foreach ($links as $publicPath => $storagePath) {
$this->removeLink($publicPath);
}
$this->info('The links have been removed.'); $this->info('The links have been removed.');
return;
} }
foreach ($this->links() as $link => $target) { protected function createLinks(array $links)
if (file_exists($link) && ! $this->isRemovableSymlink($link, $this->option('force'))) { {
$this->error("The [$link] link already exists."); foreach ($links as $link => $storagePath) {
continue; $this->createLink($link, $storagePath);
}
if (is_link($link)) {
$this->laravel->make('files')->delete($link);
}
if ($relative) {
$this->laravel->make('files')->relativeLink($target, $link);
} else {
$this->laravel->make('files')->link($target, $link);
}
$this->info("The [$link] link has been connected to [$target].");
} }
$this->info('The links have been created.'); $this->info('The links have been created.');
} }
protected function removeLink(string $publicPath)
{
if ($this->symlinkExists($publicPath)) {
$this->laravel->make('files')->delete($publicPath);
$this->info("The [$publicPath] link has been removed.");
}
}
protected function createLink(string $publicPath, string $storagePath)
{
if ($this->symlinkExists($publicPath)) {
// If the 'force' option isn't passed, don't overwrite the existing symlink
if (! $this->option('force')) {
$this->error("The [$publicPath] link already exists.");
return;
}
$this->laravel->make('files')->delete($publicPath);
}
// Make sure the storage path exists before we create a symlink
if (! is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
if ($this->option('relative')) {
$this->laravel->make('files')->relativeLink($storagePath, $publicPath);
} else {
$this->laravel->make('files')->link($storagePath, $publicPath);
}
$this->info("The [$publicPath] link has been connected to [$storagePath].");
}
/** /**
* Create symbolic links using the tenancy.filesystem config. * Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']).
* *
* @return array * @return array
*/ */
protected function links() protected function getPossibleTenantSymlinks(int|string $tenantKey)
{ {
$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');
$tenants = $this->option('remove') && filled($this->option('tenants'))
? collect($this->option('tenants'))
: $this->getTenants()->map(function (Tenant $tenant) {
return $tenant->getTenantKey();
});
return $tenants->map(function ($tenantKey) use ($suffixBase, $diskUrls, $disks) {
$symlinks = []; $symlinks = [];
foreach ($diskUrls as $disk => $publicPath) { foreach ($diskUrls as $disk => $publicPath) {
@ -102,25 +121,17 @@ class Link extends Command
$publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath); $publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath);
$publicPath = public_path($publicPath); $publicPath = public_path($publicPath);
// Make sure the storage path exists before we create a symlink
if (! is_dir($storagePath)) {
mkdir($storagePath, 0777, true);
}
$symlinks[] = [$publicPath => $storagePath]; $symlinks[] = [$publicPath => $storagePath];
} }
return $symlinks; return $symlinks;
})->flatten(1)
->mapWithKeys(fn ($item) => $item)
->all();
} }
/** /**
* Determine if the provided path is a removable symlink. * Determine if the provided path is an existing symlink.
*/ */
protected function isRemovableSymlink(string $link, bool $force): bool protected function symlinkExists(string $link): bool
{ {
return is_link($link) && $force; return file_exists($link) && is_link($link);
} }
} }