-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Laravel filesystem storage custom driver to tenantise stora…
…ge disks
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
|
||
'listen_for_routing' => true, | ||
|
||
'services' => [ | ||
'storage' => true, | ||
], | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sprout\Listeners; | ||
|
||
use Illuminate\Filesystem\FilesystemManager; | ||
use Sprout\Events\CurrentTenantChanged; | ||
|
||
final class CleanupLaravelServices | ||
{ | ||
/** | ||
* @var \Illuminate\Filesystem\FilesystemManager | ||
*/ | ||
private FilesystemManager $filesystemManager; | ||
|
||
public function __construct(FilesystemManager $filesystemManager) | ||
{ | ||
$this->filesystemManager = $filesystemManager; | ||
} | ||
|
||
/** | ||
* @template TenantClass of \Sprout\Contracts\Tenant | ||
* | ||
* @param \Sprout\Events\CurrentTenantChanged<TenantClass> $event | ||
* | ||
* @return void | ||
*/ | ||
public function handle(CurrentTenantChanged $event): void | ||
{ | ||
$this->purgeFilesystemDisks(); | ||
} | ||
|
||
private function purgeFilesystemDisks(): void | ||
{ | ||
// If we're not overriding the storage service we can exit early | ||
if (config('sprout.services.storage', false) === false) { | ||
return; | ||
} | ||
|
||
/** @var array<string, array<string, mixed>> $diskConfig */ | ||
$diskConfig = config('filesystems.disks', []); | ||
|
||
// If any of the disks have the 'sprout' driver we need to purge them, | ||
// if they exist, so we don't end up leaking tenant information | ||
foreach ($diskConfig as $disk => $config) { | ||
if (($config['driver'] ?? null) === 'sprout') { | ||
$this->filesystemManager->forgetDisk($disk); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters