Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Added scoped filesystem driver #44105

Merged
merged 5 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"guzzlehttp/guzzle": "^7.2",
"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-ftp": "^3.0",
"league/flysystem-path-prefixing": "^3.3",
"league/flysystem-read-only": "^3.3",
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.4.4",
Expand Down Expand Up @@ -152,6 +153,7 @@
"laravel/tinker": "Required to use the tinker console command (^2.0).",
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).",
"league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).",
"league/flysystem-path-prefixing": "Required to use the scoped driver (^3.3).",
"league/flysystem-read-only": "Required to use read-only disks (^3.3)",
"league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).",
"mockery/mockery": "Required to use mocking (^1.4.4).",
Expand Down
9 changes: 6 additions & 3 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ public function __construct(FilesystemOperator $driver, FlysystemAdapter $adapte
$this->driver = $driver;
$this->adapter = $adapter;
$this->config = $config;
$separator = $config['directory_separator'] ?? DIRECTORY_SEPARATOR;

$this->prefixer = new PathPrefixer(
$config['root'] ?? '', $config['directory_separator'] ?? DIRECTORY_SEPARATOR
);
$this->prefixer = new PathPrefixer($config['root'] ?? '', $separator);

if (isset($config['prefix'])) {
$this->prefixer = new PathPrefixer($this->prefixer->prefixPath($config['prefix']), $separator);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Illuminate/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
Expand Down Expand Up @@ -267,6 +268,26 @@ protected function formatS3Config(array $config)
return $config;
}

/**
* Create a scoped driver.
*
* @param array $config
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
public function createScopedDriver(array $config)
{
if (empty($config['disk'])) {
throw new InvalidArgumentException('Scoped disk is missing "disk" configuration option.');
} elseif (empty($config['prefix'])) {
throw new InvalidArgumentException('Scoped disk is missing "prefix" configuration option.');
}

return $this->build(tap(
$this->getConfig($config['disk']),
fn (&$parent) => $parent['prefix'] = $config['prefix']
));
}

/**
* Create a Flysystem instance with the given adapter.
*
Expand All @@ -280,6 +301,10 @@ protected function createFlysystem(FlysystemAdapter $adapter, array $config)
$adapter = new ReadOnlyFilesystemAdapter($adapter);
}

if (! empty($config['prefix'])) {
$adapter = new PathPrefixedAdapter($adapter, $config['prefix']);
}

return new Flysystem($adapter, Arr::only($config, [
'directory_visibility',
'disable_asserts',
Expand Down
28 changes: 28 additions & 0 deletions tests/Filesystem/FilesystemManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,32 @@ public function testCanBuildReadOnlyDisks()
unlink(__DIR__.'/../../my-custom-path/path.txt');
rmdir(__DIR__.'/../../my-custom-path');
}

public function testCanBuildScopedDisks()
{
try {
$filesystem = new FilesystemManager(tap(new Application, function ($app) {
$app['config'] = [
'filesystems.disks.local' => [
'driver' => 'local',
'root' => 'to-be-scoped',
],
];
}));

$local = $filesystem->disk('local');
$scoped = $filesystem->build([
'driver' => 'scoped',
'disk' => 'local',
'prefix' => 'path-prefix',
]);

$scoped->put('dirname/filename.txt', 'file content');
$this->assertEquals('file content', $local->get('path-prefix/dirname/filename.txt'));
$local->deleteDirectory('path-prefix');
} finally {
rmdir(__DIR__.'/../../to-be-scoped');
}

}
}