|
|
|
|
@ -18,6 +18,8 @@ use Psr\Log\LoggerInterface; |
|
|
|
|
class StorageFactory implements IStorageFactory { |
|
|
|
|
/** @var array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */ |
|
|
|
|
private array $storageWrappers = []; |
|
|
|
|
/** @var bool $dirty Whether the list of storage wrappers is sorted */ |
|
|
|
|
private bool $dirty = true; |
|
|
|
|
|
|
|
|
|
#[\Override] |
|
|
|
|
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool { |
|
|
|
|
@ -31,6 +33,7 @@ class StorageFactory implements IStorageFactory { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority]; |
|
|
|
|
$this->dirty = true; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -56,16 +59,14 @@ class StorageFactory implements IStorageFactory { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage { |
|
|
|
|
$wrappers = array_values($this->storageWrappers); |
|
|
|
|
usort($wrappers, function ($a, $b) { |
|
|
|
|
return $b['priority'] - $a['priority']; |
|
|
|
|
}); |
|
|
|
|
/** @var callable[] $wrappers */ |
|
|
|
|
$wrappers = array_map(function ($wrapper) { |
|
|
|
|
return $wrapper['wrapper']; |
|
|
|
|
}, $wrappers); |
|
|
|
|
foreach ($wrappers as $wrapper) { |
|
|
|
|
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint); |
|
|
|
|
if ($this->dirty) { |
|
|
|
|
uasort($this->storageWrappers, static fn (array $a, array $b) => $b['priority'] - $a['priority']); |
|
|
|
|
$this->dirty = false; |
|
|
|
|
} |
|
|
|
|
foreach ($this->storageWrappers as $wrapper) { |
|
|
|
|
/** @var callable(string, IStorage, IMountPoint): IStorage $wrapperCallable */ |
|
|
|
|
$wrapperCallable = $wrapper['wrapper']; |
|
|
|
|
$storage = $wrapperCallable($mountPoint->getMountPoint(), $storage, $mountPoint); |
|
|
|
|
if (!($storage instanceof IStorage)) { |
|
|
|
|
throw new \Exception('Invalid result from storage wrapper'); |
|
|
|
|
} |
|
|
|
|
|