Skip to content
Closed
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
30 changes: 20 additions & 10 deletions lib/private/Files/Storage/StorageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ class StorageFactory implements IStorageFactory {
/**
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
*/
private $storageWrappers = [];
private array $storageWrappers = [];

private bool $wrappersSorted = false;

/**
* @var IStorage[]
*/
private array $wrappersList = [];

#[\Override]
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool {
Expand All @@ -33,6 +40,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
}

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

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

/**
Expand All @@ -58,15 +67,16 @@ 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) {
if (!$this->wrappersSorted) {
uasort($this->storageWrappers, function ($a, $b) {
return $b['priority'] - $a['priority'];
});

$this->wrappersSorted = true;
$this->wrappersList = array_column($this->storageWrappers, 'wrapper');
}

foreach ($this->wrappersList as $wrapper) {
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
if (!($storage instanceof IStorage)) {
throw new \Exception('Invalid result from storage wrapper');
Expand Down
Loading