99
1010use OC \Hooks \Emitter ;
1111use OC \Hooks \EmitterTrait ;
12+ use OCA \Files_Sharing \MountProvider ;
1213use OCP \Diagnostics \IEventLogger ;
1314use OCP \Files \Config \IHomeMountProvider ;
1415use OCP \Files \Config \IMountProvider ;
16+ use OCP \Files \Config \IMountProviderArgs ;
1517use OCP \Files \Config \IMountProviderCollection ;
18+ use OCP \Files \Config \IPartialMountProvider ;
1619use OCP \Files \Config \IRootMountProvider ;
1720use OCP \Files \Config \IUserMountCache ;
1821use OCP \Files \Mount \IMountManager ;
1922use OCP \Files \Mount \IMountPoint ;
2023use OCP \Files \Storage \IStorageFactory ;
2124use OCP \IUser ;
25+ use function get_class ;
26+ use function in_array ;
2227
2328class MountProviderCollection implements IMountProviderCollection, Emitter {
2429 use EmitterTrait;
@@ -29,7 +34,7 @@ class MountProviderCollection implements IMountProviderCollection, Emitter {
2934 private array $ homeProviders = [];
3035
3136 /**
32- * @var list< IMountProvider>
37+ * @var array<class-string<IMountProvider>, IMountProvider>
3338 */
3439 private array $ providers = [];
3540
@@ -67,28 +72,61 @@ private function getUserMountsForProviders(IUser $user, array $providers): array
6772 $ mounts = array_map (function (IMountProvider $ provider ) use ($ user , $ loader ) {
6873 return $ this ->getMountsFromProvider ($ provider , $ user , $ loader );
6974 }, $ providers );
70- $ mounts = array_reduce ($ mounts , function (array $ mounts , array $ providerMounts ) {
71- return array_merge ($ mounts , $ providerMounts );
72- }, []);
75+ $ mounts = array_merge (...$ mounts );
7376 return $ this ->filterMounts ($ user , $ mounts );
7477 }
7578
7679 /**
7780 * @return list<IMountPoint>
7881 */
7982 public function getMountsForUser (IUser $ user ): array {
80- return $ this ->getUserMountsForProviders ($ user , $ this ->providers );
83+ return $ this ->getUserMountsForProviders ($ user , array_values ( $ this ->providers ) );
8184 }
8285
8386 /**
87+ * @param IMountProviderArgs[] $mountProviderArgs
88+ * @return array<string, IMountPoint> IMountPoint array indexed by mount
89+ * point.
90+ */
91+ public function getUserMountsFromProviderByPath (
92+ string $ providerClass ,
93+ string $ path ,
94+ array $ mountProviderArgs ,
95+ ): array {
96+ $ provider = $ this ->providers [$ providerClass ] ?? null ;
97+ if ($ provider === null ) {
98+ return [];
99+ }
100+
101+ if (!is_a ($ providerClass , IPartialMountProvider::class, true )) {
102+ throw new \LogicException (
103+ 'Mount provider does not support partial mounts '
104+ );
105+ }
106+
107+ /** @var IPartialMountProvider $provider */
108+ return $ provider ->getMountsForPath (
109+ $ path ,
110+ $ mountProviderArgs ,
111+ $ this ->loader ,
112+ );
113+ }
114+
115+ /**
116+ * Returns the mounts for the user from the specified provider classes.
117+ * Providers not registered in the MountProviderCollection will be skipped.
118+ *
119+ * @inheritdoc
120+ *
84121 * @return list<IMountPoint>
85122 */
86123 public function getUserMountsForProviderClasses (IUser $ user , array $ mountProviderClasses ): array {
87124 $ providers = array_filter (
88125 $ this ->providers ,
89- fn (IMountProvider $ mountProvider ) => (in_array (get_class ($ mountProvider ), $ mountProviderClasses ))
126+ fn (string $ providerClass ) => in_array ($ providerClass , $ mountProviderClasses ),
127+ ARRAY_FILTER_USE_KEY
90128 );
91- return $ this ->getUserMountsForProviders ($ user , $ providers );
129+ return $ this ->getUserMountsForProviders ($ user , array_values ( $ providers) );
92130 }
93131
94132 /**
@@ -99,16 +137,21 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla
99137 // to check for name collisions
100138 $ firstMounts = [];
101139 if ($ providerFilter ) {
102- $ providers = array_filter ($ this ->providers , $ providerFilter );
140+ $ providers = array_filter ($ this ->providers , $ providerFilter, ARRAY_FILTER_USE_KEY );
103141 } else {
104142 $ providers = $ this ->providers ;
105143 }
106- $ firstProviders = array_filter ($ providers , function (IMountProvider $ provider ) {
107- return (get_class ($ provider ) !== 'OCA\Files_Sharing\MountProvider ' );
108- });
109- $ lastProviders = array_filter ($ providers , function (IMountProvider $ provider ) {
110- return (get_class ($ provider ) === 'OCA\Files_Sharing\MountProvider ' );
111- });
144+ $ firstProviders
145+ = array_filter (
146+ $ providers ,
147+ fn (string $ providerClass ) => ($ providerClass !== MountProvider::class),
148+ ARRAY_FILTER_USE_KEY
149+ );
150+ $ lastProviders = array_filter (
151+ $ providers ,
152+ fn (string $ providerClass ) => $ providerClass === MountProvider::class,
153+ ARRAY_FILTER_USE_KEY
154+ );
112155 foreach ($ firstProviders as $ provider ) {
113156 $ mounts = $ this ->getMountsFromProvider ($ provider , $ user , $ this ->loader );
114157 $ firstMounts = array_merge ($ firstMounts , $ mounts );
@@ -150,7 +193,7 @@ public function getHomeMountForUser(IUser $user): IMountPoint {
150193 * Add a provider for mount points
151194 */
152195 public function registerProvider (IMountProvider $ provider ): void {
153- $ this ->providers [] = $ provider ;
196+ $ this ->providers [get_class ( $ provider ) ] = $ provider ;
154197
155198 $ this ->emit ('\OC\Files\Config ' , 'registerMountProvider ' , [$ provider ]);
156199 }
@@ -228,7 +271,7 @@ public function clearProviders(): void {
228271 * @return list<IMountProvider>
229272 */
230273 public function getProviders (): array {
231- return $ this ->providers ;
274+ return array_values ( $ this ->providers ) ;
232275 }
233276
234277 /**
0 commit comments