1717
1818class StorageFactory implements IStorageFactory {
1919 /**
20- * @var array[] [$name=>[' priority'=>$priority, ' wrapper'=>$ callable] $storageWrappers
20+ * @var array<string, array{ priority: int, wrapper: ( callable(string, IStorage, IMountPoint): IStorage)}> $storageWrappers
2121 */
22- private $ storageWrappers = [];
22+ private array $ storageWrappers = [];
23+ /** @var bool $dirty Whether the list of storage wrappers is sorted */
24+ private bool $ dirty = true ;
2325
2426 #[\Override]
2527 public function addStorageWrapper (string $ wrapperName , callable $ callback , int $ priority = 50 , array $ existingMounts = []): bool {
@@ -33,6 +35,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
3335 }
3436
3537 $ this ->storageWrappers [$ wrapperName ] = ['wrapper ' => $ callback , 'priority ' => $ priority ];
38+ $ this ->dirty = true ;
3639 return true ;
3740 }
3841
@@ -44,6 +47,7 @@ public function addStorageWrapper(string $wrapperName, callable $callback, int $
4447 */
4548 public function removeStorageWrapper (string $ wrapperName ): void {
4649 unset($ this ->storageWrappers [$ wrapperName ]);
50+ $ this ->dirty = true ;
4751 }
4852
4953 /**
@@ -58,14 +62,14 @@ public function getInstance(IMountPoint $mountPoint, string $class, array $argum
5862 }
5963
6064 public function wrap (IMountPoint $ mountPoint , IStorage $ storage ): IStorage {
65+ if ($ this ->dirty ) {
66+ uasort ($ this ->storageWrappers , static fn (array $ a , array $ b ) => $ b ['priority ' ] - $ a ['priority ' ]);
67+ $ this ->dirty = false ;
68+ }
6169 $ wrappers = array_values ($ this ->storageWrappers );
62- usort ($ wrappers , function ($ a , $ b ) {
63- return $ b ['priority ' ] - $ a ['priority ' ];
64- });
65- /** @var callable[] $wrappers */
66- $ wrappers = array_map (function ($ wrapper ) {
67- return $ wrapper ['wrapper ' ];
68- }, $ wrappers );
70+
71+ /** @var list<callable(string, IStorage, IMountPoint): IStorage> $wrappers */
72+ $ wrappers = array_map (static fn (array $ wrapper ): callable => $ wrapper ['wrapper ' ], $ wrappers );
6973 foreach ($ wrappers as $ wrapper ) {
7074 $ storage = $ wrapper ($ mountPoint ->getMountPoint (), $ storage , $ mountPoint );
7175 if (!($ storage instanceof IStorage)) {
0 commit comments