Skip to content

Commit

Permalink
Allow scoped disks to be scoped from other scoped disks (#54124)
Browse files Browse the repository at this point in the history
* Add failing test

* If the parent config has a prefix append the prefix to it

* Update FilesystemManager.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
willrowe and taylorotwell authored Jan 9, 2025
1 parent 941c439 commit 70bbb85
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Illuminate/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
32 changes: 32 additions & 0 deletions tests/Filesystem/FilesystemManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 70bbb85

Please sign in to comment.