diff --git a/src/Illuminate/Filesystem/FilesystemManager.php b/src/Illuminate/Filesystem/FilesystemManager.php index dfdab7c6e7f8..d4d4be8e8136 100644 --- a/src/Illuminate/Filesystem/FilesystemManager.php +++ b/src/Illuminate/Filesystem/FilesystemManager.php @@ -298,7 +298,16 @@ public function createScopedDriver(array $config) return $this->build(tap( is_string($config['disk']) ? $this->getConfig($config['disk']) : $config['disk'], function (&$parent) use ($config) { - $parent['prefix'] = $config['prefix']; + if (empty($parent['prefix'])) { + $parent['prefix'] = $config['prefix']; + } else { + $separator = $parent['directory_separator'] ?? DIRECTORY_SEPARATOR; + + $parentPrefix = rtrim($parent['prefix'], $separator); + $scopedPrefix = ltrim($config['prefix'], $separator); + + $parent['prefix'] = "{$parentPrefix}{$separator}{$scopedPrefix}"; + } if (isset($config['visibility'])) { $parent['visibility'] = $config['visibility']; diff --git a/tests/Filesystem/FilesystemManagerTest.php b/tests/Filesystem/FilesystemManagerTest.php index 8850c5557a06..4bc8e0545a73 100644 --- a/tests/Filesystem/FilesystemManagerTest.php +++ b/tests/Filesystem/FilesystemManagerTest.php @@ -99,6 +99,38 @@ public function testCanBuildScopedDisks() } } + public function testCanBuildScopedDiskFromScopedDisk() + { + try { + $filesystem = new FilesystemManager(tap(new Application, function ($app) { + $app['config'] = [ + 'filesystems.disks.local' => [ + 'driver' => 'local', + 'root' => 'root-to-be-scoped', + ], + 'filesystems.disks.scoped-from-root' => [ + 'driver' => 'scoped', + 'disk' => 'local', + 'prefix' => 'scoped-from-root-prefix', + ], + ]; + })); + + $root = $filesystem->disk('local'); + $nestedScoped = $filesystem->build([ + 'driver' => 'scoped', + 'disk' => 'scoped-from-root', + 'prefix' => 'nested-scoped-prefix', + ]); + + $nestedScoped->put('dirname/filename.txt', 'file content'); + $this->assertEquals('file content', $root->get('scoped-from-root-prefix/nested-scoped-prefix/dirname/filename.txt')); + $root->deleteDirectory('scoped-from-root-prefix'); + } finally { + rmdir(__DIR__.'/../../root-to-be-scoped'); + } + } + #[RequiresOperatingSystem('Linux|Darwin')] public function testCanBuildScopedDisksWithVisibility() {