Skip to content

Commit e5ffa1b

Browse files
authored
[9.x] Add read-only filesystem adapter decoration as a config option (#44079)
* [NEW] Add read-only filesystem adapter decoration as a config option. * CS fixes * Remove function imports. * Added suggests item for flysystem's read-only package.
1 parent bb7e7dd commit e5ffa1b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"guzzlehttp/guzzle": "^7.2",
8888
"league/flysystem-aws-s3-v3": "^3.0",
8989
"league/flysystem-ftp": "^3.0",
90+
"league/flysystem-read-only": "^3.3",
9091
"league/flysystem-sftp-v3": "^3.0",
9192
"mockery/mockery": "^1.4.4",
9293
"orchestra/testbench-core": "^7.1",
@@ -150,6 +151,7 @@
150151
"laravel/tinker": "Required to use the tinker console command (^2.0).",
151152
"league/flysystem-aws-s3-v3": "Required to use the Flysystem S3 driver (^3.0).",
152153
"league/flysystem-ftp": "Required to use the Flysystem FTP driver (^3.0).",
154+
"league/flysystem-read-only": "Required to use read-only disks (^3.3)",
153155
"league/flysystem-sftp-v3": "Required to use the Flysystem SFTP driver (^3.0).",
154156
"mockery/mockery": "Required to use mocking (^1.4.4).",
155157
"nyholm/psr7": "Required to use PSR-7 bridging features (^1.2).",

src/Illuminate/Filesystem/FilesystemManager.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use League\Flysystem\Local\LocalFilesystemAdapter as LocalAdapter;
1717
use League\Flysystem\PhpseclibV3\SftpAdapter;
1818
use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
19+
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
1920
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
2021
use League\Flysystem\Visibility;
2122

@@ -275,6 +276,10 @@ protected function formatS3Config(array $config)
275276
*/
276277
protected function createFlysystem(FlysystemAdapter $adapter, array $config)
277278
{
279+
if ($config['read-only'] ?? false === true) {
280+
$adapter = new ReadOnlyFilesystemAdapter($adapter);
281+
}
282+
278283
return new Flysystem($adapter, Arr::only($config, [
279284
'directory_visibility',
280285
'disable_asserts',

tests/Filesystem/FilesystemManagerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,37 @@ public function testCanBuildOnDemandDisk()
3737

3838
rmdir(__DIR__.'/../../my-custom-path');
3939
}
40+
41+
public function testCanBuildReadOnlyDisks()
42+
{
43+
$filesystem = new FilesystemManager(new Application);
44+
45+
$disk = $filesystem->build([
46+
'driver' => 'local',
47+
'read-only' => true,
48+
'root' => 'my-custom-path',
49+
'url' => 'my-custom-url',
50+
'visibility' => 'public',
51+
]);
52+
53+
file_put_contents(__DIR__.'/../../my-custom-path/path.txt', 'contents');
54+
55+
// read operations work
56+
$this->assertEquals('contents', $disk->get('path.txt'));
57+
$this->assertEquals(['path.txt'], $disk->files());
58+
59+
// write operations fail
60+
$this->assertFalse($disk->put('path.txt', 'contents'));
61+
$this->assertFalse($disk->delete('path.txt'));
62+
$this->assertFalse($disk->deleteDirectory('directory'));
63+
$this->assertFalse($disk->prepend('path.txt', 'data'));
64+
$this->assertFalse($disk->append('path.txt', 'data'));
65+
$handle = fopen('php://memory', 'rw');
66+
fwrite($handle, 'content');
67+
$this->assertFalse($disk->writeStream('path.txt', $handle));
68+
fclose($handle);
69+
70+
unlink(__DIR__.'/../../my-custom-path/path.txt');
71+
rmdir(__DIR__.'/../../my-custom-path');
72+
}
4073
}

0 commit comments

Comments
 (0)