Skip to content
Open
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
22 changes: 13 additions & 9 deletions lib/private/Files/Storage/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

class StorageFactory implements IStorageFactory {
/**
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
* @var array<string, array{priority: int, wrapper: (callable(string, IStorage, IMountPoint): IStorage)}> $storageWrappers
*/
private $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 {
Expand All @@ -33,6 +35,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
}

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

Expand All @@ -44,6 +47,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
*/
public function removeStorageWrapper(string $wrapperName): void {
unset($this->storageWrappers[$wrapperName]);
$this->dirty = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unset should not change the order 🤔

}

/**
Expand All @@ -58,14 +62,14 @@ public function getInstance(IMountPoint $mountPoint, string $class, array $argum
}

public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage {
if ($this->dirty) {
uasort($this->storageWrappers, static fn (array $a, array $b) => $b['priority'] - $a['priority']);
$this->dirty = false;
}
$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);

/** @var list<callable(string, IStorage, IMountPoint): IStorage> $wrappers */
$wrappers = array_map(static fn (array $wrapper): callable => $wrapper['wrapper'], $wrappers);
foreach ($wrappers as $wrapper) {
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
if (!($storage instanceof IStorage)) {
Expand Down
Loading