1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 09:34:04 +00:00
tenancy/src/Concerns/DealsWithTenantSymlinks.php
lukinovec 8cd15db1fc
[4.x] Make RemoveStorageSymlinksAction able to delete broken symlinks (#1323)
* Add regression test for removing invalid symlinks

* Move commented RemoveStorageSymlinks job to the DeletingTenant pipeline (better default - the symlinks will be removed *before* deleting tenant storage)

* Remove symlink validity check from symlinkExists() (only check for the symlink's existence)

* Delete complete todo0

* Make the symlink assertions more explicit

* update test name

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2025-03-18 21:27:27 +01:00

61 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Exception;
use Stancl\Tenancy\Contracts\Tenant;
trait DealsWithTenantSymlinks
{
/**
* Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']).
*
* Tenants can have a symlink for each disk registered in the tenancy.filesystem.url_override config.
* This is used for creating all possible tenant symlinks and removing all existing tenant symlinks.
* The same storage path can be symlinked to multiple public paths, which is why the public path
* is the Collection key.
*
* @return array<string, string>
*/
protected function possibleTenantSymlinks(Tenant $tenant): array
{
$disks = config('filesystems.disks');
$urlOverrides = config('tenancy.filesystem.url_override');
$rootOverrides = config('tenancy.filesystem.root_override');
$tenantKey = $tenant->getTenantKey();
$tenantStoragePath = tenancy()->run($tenant, fn () => storage_path());
/** @var array<string, string> $symlinks */
$symlinks = [];
foreach ($urlOverrides as $disk => $publicPath) {
if (! isset($disks[$disk])) {
continue;
}
if (! isset($rootOverrides[$disk])) {
continue;
}
if ($disks[$disk]['driver'] !== 'local') {
throw new Exception("Disk $disk is not a local disk. Only local disks can be symlinked.");
}
$publicPath = str_replace('%tenant%', (string) $tenantKey, $publicPath);
$storagePath = str_replace('%storage_path%', $tenantStoragePath, $rootOverrides[$disk]);
$symlinks[public_path($publicPath)] = $storagePath;
}
return $symlinks;
}
/** Determine if the provided path is an existing symlink. */
protected function symlinkExists(string $link): bool
{
return is_link($link);
}
}