mirror of
https://github.com/archtechx/tenancy.git
synced 2026-09-20 13:34:04 +00:00
Simplify baseDiskName()
Refrain from dealing with the impossible "self-referencing" scoped disk case. Instead of that, test the inline parent behavior. Also update the exception message in forgetDisks() so that it's a bit less vague.
This commit is contained in:
parent
dde9be6f58
commit
197ca18566
2 changed files with 19 additions and 58 deletions
|
|
@ -132,12 +132,10 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$baseDisk = static::baseDiskName($name);
|
if (in_array(static::baseDiskName($name), $tenantDisks, true)) {
|
||||||
|
|
||||||
if (in_array($baseDisk, $tenantDisks, true)) {
|
|
||||||
$scopedDisks[] = $name;
|
$scopedDisks[] = $name;
|
||||||
} elseif (in_array($name, $tenantDisks, true)) {
|
} elseif (in_array($name, $tenantDisks, true)) {
|
||||||
throw new Exception("A disk using the 'scoped' driver cannot be tenant-aware. List its base disk in tenancy.filesystem.disks instead.");
|
throw new Exception("Disk [$name] uses the 'scoped' driver, so it has no root to make tenant-aware. List its base disk in tenancy.filesystem.disks instead.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -359,27 +357,17 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
* Disks using the 'scoped' driver have no root or url of their own -- they inherit those from their parent disk,
|
* Disks using the 'scoped' driver have no root or url of their own -- they inherit those from their parent disk,
|
||||||
* which can be scoped as well, so only the final/base parent has to be tenant-aware.
|
* which can be scoped as well, so only the final/base parent has to be tenant-aware.
|
||||||
*
|
*
|
||||||
* Returns null if the chain doesn't end with a named disk, i.e. when a parent disk is
|
* Returns null if the base disk has no name, i.e. when the disk is configured inline as an array.
|
||||||
* configured inline or when the disks reference each other.
|
|
||||||
*/
|
*/
|
||||||
public static function baseDiskName(string $disk): string|null
|
public static function baseDiskName(string $disk): string|null
|
||||||
{
|
{
|
||||||
// Keep track of visited disks to avoid infinite loops in case of disks referencing each other
|
|
||||||
$visited = [];
|
|
||||||
|
|
||||||
while (config("filesystems.disks.$disk.driver") === 'scoped') {
|
while (config("filesystems.disks.$disk.driver") === 'scoped') {
|
||||||
if (in_array($disk, $visited, true)) {
|
if (! is_string($parent = config("filesystems.disks.$disk.disk"))) {
|
||||||
// The disk and its parents reference each other, invalid
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$visited[] = $disk;
|
|
||||||
$disk = config("filesystems.disks.$disk.disk");
|
|
||||||
|
|
||||||
if (! is_string($disk)) {
|
|
||||||
// Laravel allows configuring the parent disk inline as an array, and such a disk has no name
|
// Laravel allows configuring the parent disk inline as an array, and such a disk has no name
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$disk = $parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $disk;
|
return $disk;
|
||||||
|
|
|
||||||
|
|
@ -341,35 +341,33 @@ test('adding a scoped disk to tenancy.filesystem.disks throws an exception if it
|
||||||
'disk' => 'foo',
|
'disk' => 'foo',
|
||||||
'prefix' => 'bar',
|
'prefix' => 'bar',
|
||||||
],
|
],
|
||||||
// Disks referencing each other (neither has a base disk)
|
// Scoped disk with an inline parent
|
||||||
'filesystems.disks.abc' => [
|
'filesystems.disks.inline_parent' => [
|
||||||
'driver' => 'scoped',
|
'driver' => 'scoped',
|
||||||
'disk' => 'def',
|
'disk' => [
|
||||||
'prefix' => 'abc',
|
'driver' => 'local',
|
||||||
],
|
'root' => storage_path('app/inline'),
|
||||||
'filesystems.disks.def' => [
|
],
|
||||||
'driver' => 'scoped',
|
'prefix' => 'inline_parent',
|
||||||
'disk' => 'abc',
|
|
||||||
'prefix' => 'def',
|
|
||||||
],
|
],
|
||||||
'tenancy.filesystem.disks' => [$disk],
|
'tenancy.filesystem.disks' => [$disk],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(fn () => tenancy()->initialize(Tenant::create()))
|
expect(fn () => tenancy()->initialize(Tenant::create()))
|
||||||
->toThrow(Exception::class, "List its base disk in tenancy.filesystem.disks instead");
|
->toThrow(Exception::class, "Disk [$disk] uses the 'scoped' driver, so it has no root to make tenant-aware.");
|
||||||
|
|
||||||
// Parent of 'abc' is 'def', whose parent is 'abc' -- there's no base disk for these, so these are
|
// 'inline_parent' has no base disk name to list, so there's no way to make it tenant-aware
|
||||||
// still invalid and the exception will still be thrown.
|
// and the exception is thrown regardless of what's listed.
|
||||||
if ($disk !== 'abc') {
|
if ($disk !== 'inline_parent') {
|
||||||
config(['tenancy.filesystem.disks' => ['public', $disk]]);
|
config(['tenancy.filesystem.disks' => ['public', $disk]]);
|
||||||
|
|
||||||
expect(fn () => tenancy()->initialize(Tenant::create()))
|
expect(fn () => tenancy()->initialize(Tenant::create()))
|
||||||
->not()->toThrow(Exception::class, "List its base disk in tenancy.filesystem.disks instead");
|
->not()->toThrow(Exception::class, "Disk [$disk] uses the 'scoped' driver, so it has no root to make tenant-aware.");
|
||||||
}
|
}
|
||||||
})->with([
|
})->with([
|
||||||
'scoped disk' => 'foo',
|
'scoped disk' => 'foo',
|
||||||
'nested scoped disk' => 'bar',
|
'nested scoped disk' => 'bar',
|
||||||
'scoped disk with no base disk' => 'abc',
|
'scoped disk with an inline base disk' => 'inline_parent',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk when its base disk is listed too', function () {
|
test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk when its base disk is listed too', function () {
|
||||||
|
|
@ -401,31 +399,6 @@ test('adding a scoped disk to tenancy.filesystem.disks has no effect on the disk
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('scoped disks referencing each other do not make bootstrapper hang', function () {
|
|
||||||
config([
|
|
||||||
'tenancy.bootstrappers' => [
|
|
||||||
FilesystemTenancyBootstrapper::class,
|
|
||||||
],
|
|
||||||
'filesystems.disks.foo' => [
|
|
||||||
'driver' => 'scoped',
|
|
||||||
'disk' => 'bar',
|
|
||||||
'prefix' => 'foo',
|
|
||||||
],
|
|
||||||
'filesystems.disks.bar' => [
|
|
||||||
'driver' => 'scoped',
|
|
||||||
'disk' => 'foo',
|
|
||||||
'prefix' => 'bar',
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect(FilesystemTenancyBootstrapper::baseDiskName('foo'))->toBeNull();
|
|
||||||
expect(FilesystemTenancyBootstrapper::baseDiskName('bar'))->toBeNull();
|
|
||||||
|
|
||||||
tenancy()->initialize(Tenant::create());
|
|
||||||
|
|
||||||
expect(tenant())->not()->toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('file cache stores get their paths scoped on bootstrap and restored back on revert', function () {
|
test('file cache stores get their paths scoped on bootstrap and restored back on revert', function () {
|
||||||
$fooPath = storage_path('framework/cache/foo_file');
|
$fooPath = storage_path('framework/cache/foo_file');
|
||||||
$barPath = storage_path('framework/cache/bar_file');
|
$barPath = storage_path('framework/cache/bar_file');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue