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] Add read-only filesystem adapter decoration as a config option #44079

Merged
merged 4 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"guzzlehttp/guzzle": "^7.2",
"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-ftp": "^3.0",
"league/flysystem-read-only": "^3.3",
frankdejonge marked this conversation as resolved.
Show resolved Hide resolved
"league/flysystem-sftp-v3": "^3.0",
"mockery/mockery": "^1.4.4",
"orchestra/testbench-core": "^7.1",
Expand Down
5 changes: 5 additions & 0 deletions src/Illuminate/Filesystem/FilesystemManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
use League\Flysystem\PhpseclibV3\SftpAdapter;
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\Visibility;

Expand Down Expand Up @@ -275,6 +276,10 @@ protected function formatS3Config(array $config)
*/
protected function createFlysystem(FlysystemAdapter $adapter, array $config)
{
if ($config['read-only'] ?? false === true) {
$adapter = new ReadOnlyFilesystemAdapter($adapter);
}

return new Flysystem($adapter, Arr::only($config, [
'directory_visibility',
'disable_asserts',
Expand Down
38 changes: 38 additions & 0 deletions tests/Filesystem/FilesystemManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace Illuminate\Tests\Filesystem;

use function fclose;
use function file_put_contents;
use function fopen;
use function fwrite;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Foundation\Application;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use function unlink;
driesvints marked this conversation as resolved.
Show resolved Hide resolved

class FilesystemManagerTest extends TestCase
{
Expand Down Expand Up @@ -37,4 +42,37 @@ public function testCanBuildOnDemandDisk()

rmdir(__DIR__.'/../../my-custom-path');
}

public function testCanBuildReadOnlyDisks()
{
$filesystem = new FilesystemManager(new Application);

$disk = $filesystem->build([
'driver' => 'local',
'read-only' => true,
'root' => 'my-custom-path',
'url' => 'my-custom-url',
'visibility' => 'public',
]);

file_put_contents(__DIR__.'/../../my-custom-path/path.txt', 'contents');

// read operations work
$this->assertEquals('contents', $disk->get('path.txt'));
$this->assertEquals(['path.txt'], $disk->files());

// write operations fail
$this->assertFalse($disk->put('path.txt', 'contents'));
$this->assertFalse($disk->delete('path.txt'));
$this->assertFalse($disk->deleteDirectory('directory'));
$this->assertFalse($disk->prepend('path.txt', 'data'));
$this->assertFalse($disk->append('path.txt', 'data'));
$handle = fopen('php://memory', 'rw');
fwrite($handle, 'content');
$this->assertFalse($disk->writeStream('path.txt', $handle));
fclose($handle);

unlink(__DIR__.'/../../my-custom-path/path.txt');
rmdir(__DIR__.'/../../my-custom-path');
}
}