Skip to content
Draft
Changes from all 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
28 changes: 14 additions & 14 deletions lib/private/Files/Storage/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use Psr\Log\LoggerInterface;

class StorageFactory implements IStorageFactory {
/**
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
*/
private $storageWrappers = [];
/** @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;

public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool {
if (isset($this->storageWrappers[$wrapperName])) {
Expand All @@ -30,6 +31,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
}

$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
$this->dirty = true;
return true;
}

Expand All @@ -54,16 +56,14 @@ public function getInstance(IMountPoint $mountPoint, string $class, array $argum
}

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');
}
Expand Down
Loading