Skip to content

Commit 3c0e21b

Browse files
viicslenCopilotlukinovecstancl
authored
[4.x] Filesystem bootstrapper: scoped disk support (#1402)
Fixes #1401 --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: lukinovec <lukinovec@gmail.com> Co-authored-by: Samuel Stancl <samuel@archte.ch>
1 parent 7955aae commit 3c0e21b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"doctrine/dbal": "^3.6.0",
3434
"spatie/valuestore": "^1.2.5",
3535
"pestphp/pest": "^3.0",
36-
"larastan/larastan": "^3.0"
36+
"larastan/larastan": "^3.0",
37+
"league/flysystem-path-prefixing": "^3.0"
3738
},
3839
"autoload": {
3940
"psr-4": {

src/Bootstrappers/FilesystemTenancyBootstrapper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,18 @@ protected function assetHelper(string|false $suffix): void
114114

115115
protected function forgetDisks(): void
116116
{
117-
Storage::forgetDisk($this->app['config']['tenancy.filesystem.disks']);
117+
$tenantDisks = $this->app['config']['tenancy.filesystem.disks'];
118+
$scopedDisks = [];
119+
120+
foreach ($this->app['config']['filesystems.disks'] as $name => $disk) {
121+
if (isset($disk['driver'], $disk['disk'])
122+
&& $disk['driver'] === 'scoped'
123+
&& in_array($disk['disk'], $tenantDisks, true)) {
124+
$scopedDisks[] = $name;
125+
}
126+
}
127+
128+
Storage::forgetDisk(array_merge($tenantDisks, $scopedDisks));
118129
}
119130

120131
protected function diskRoot(string $disk, Tenant|false $tenant): void

tests/Bootstrappers/FilesystemTenancyBootstrapperTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,39 @@
221221
expect(is_dir($centralStoragePath . "/tenant{$tenant->id}/framework/cache"))->toBeFalse();
222222
}
223223
})->with([true, false]);
224+
225+
test('scoped disks are scoped per tenant', function () {
226+
config([
227+
'tenancy.bootstrappers' => [
228+
FilesystemTenancyBootstrapper::class,
229+
],
230+
'filesystems.disks.scoped_disk' => [
231+
'driver' => 'scoped',
232+
'disk' => 'public',
233+
'prefix' => 'scoped_disk_prefix',
234+
],
235+
]);
236+
237+
$tenant = Tenant::create();
238+
239+
Storage::disk('scoped_disk')->put('foo.txt', 'central');
240+
expect(Storage::disk('scoped_disk')->get('foo.txt'))->toBe('central');
241+
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central');
242+
243+
tenancy()->initialize($tenant);
244+
245+
expect(Storage::disk('scoped_disk')->get('foo.txt'))->toBe(null);
246+
Storage::disk('scoped_disk')->put('foo.txt', 'tenant');
247+
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
248+
expect(Storage::disk('scoped_disk')->get('foo.txt'))->toBe('tenant');
249+
250+
tenancy()->end();
251+
252+
expect(Storage::disk('scoped_disk')->get('foo.txt'))->toBe('central');
253+
Storage::disk('scoped_disk')->put('foo.txt', 'central2');
254+
expect(Storage::disk('scoped_disk')->get('foo.txt'))->toBe('central2');
255+
256+
expect(file_get_contents(storage_path() . "/app/public/scoped_disk_prefix/foo.txt"))->toBe('central2');
257+
expect(file_get_contents(storage_path() . "/tenant{$tenant->id}/app/public/scoped_disk_prefix/foo.txt"))->toBe('tenant');
258+
});
259+

0 commit comments

Comments
 (0)