1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 18:44:04 +00:00

Extract Link command logic into actions

This commit is contained in:
lukinovec 2022-08-23 14:45:37 +02:00
parent fa783641f4
commit 2594b1960f
6 changed files with 182 additions and 110 deletions

View file

@ -4,8 +4,12 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\LazyCollection;
use Stancl\Tenancy\Concerns\HasATenantsOption;
use Stancl\Tenancy\CreateStorageSymlinksAction;
use Stancl\Tenancy\RemoveStorageSymlinksAction;
class Link extends Command
{
@ -27,7 +31,7 @@ class Link extends Command
*
* @var string
*/
protected $description = 'Create symbolic links for tenants.';
protected $description = 'Create or remove tenant symbolic links.';
/**
* Execute the console command.
@ -36,102 +40,48 @@ class Link extends Command
*/
public function handle()
{
$tenants = collect($this->option('tenants')) ?? $this->getTenants()->map->getTenantKey();
$links = $tenants->flatMap(fn ($tenantKey) => $this->getPossibleTenantSymlinks($tenantKey))
->mapWithKeys(fn ($item) => $item)
->all();
$tenants = $this->getTenants();
if ($this->option('remove')) {
$this->removeLinks($links);
} else {
$this->createLinks($links);
try {
if ($this->option('remove')) {
$this->removeLinks($tenants);
} else {
$this->createLinks($tenants);
}
} catch (Exception $exception) {
$this->error($exception->getMessage());
return 1;
}
}
protected function removeLinks(array $links)
/**
* @param LazyCollection $tenants
* @return void
*/
protected function removeLinks($tenants)
{
foreach ($links as $publicPath => $storagePath) {
$this->removeLink($publicPath);
}
RemoveStorageSymlinksAction::handle(
$tenants,
afterLinkRemoval: fn($publicPath) => $this->info("The [$publicPath] link has been removed.")
);
$this->info('The links have been removed.');
}
protected function createLinks(array $links)
/**
* @param LazyCollection $tenants
* @return void
*/
protected function createLinks($tenants)
{
foreach ($links as $link => $storagePath) {
$this->createLink($link, $storagePath);
}
CreateStorageSymlinksAction::handle(
$tenants,
$this->option('relative') ?? 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.');
}
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].");
}
/**
* Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']).
*
* @return array
*/
protected function getPossibleTenantSymlinks(int|string $tenantKey)
{
$diskUrls = config('tenancy.filesystem.url_override');
$disks = config('tenancy.filesystem.root_override');
$suffixBase = config('tenancy.filesystem.suffix_base');
$symlinks = [];
foreach ($diskUrls as $disk => $publicPath) {
$storagePath = str_replace('%storage_path%', $suffixBase . $tenantKey, $disks[$disk]);
$storagePath = storage_path($storagePath);
$publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath);
$publicPath = public_path($publicPath);
$symlinks[] = [$publicPath => $storagePath];
}
return $symlinks;
}
/**
* Determine if the provided path is an existing symlink.
*/
protected function symlinkExists(string $link): bool
{
return file_exists($link) && is_link($link);
}
}