Skip to content

Commit d14a032

Browse files
committed
feat: implement support for authoritative mount providers
Signed-off-by: Salvatore Martire <[email protected]>
1 parent fcdb28e commit d14a032

File tree

5 files changed

+214
-43
lines changed

5 files changed

+214
-43
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3641,12 +3641,6 @@
36413641
<InvalidOperand>
36423642
<code><![CDATA[$user]]></code>
36433643
</InvalidOperand>
3644-
<RedundantCondition>
3645-
<code><![CDATA[get_class($provider) !== 'OCA\Files_Sharing\MountProvider']]></code>
3646-
</RedundantCondition>
3647-
<TypeDoesNotContainType>
3648-
<code><![CDATA[get_class($provider) === 'OCA\Files_Sharing\MountProvider']]></code>
3649-
</TypeDoesNotContainType>
36503644
</file>
36513645
<file src="lib/private/Files/Config/UserMountCache.php">
36523646
<InvalidReturnType>

build/stubs/php-polyfill.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
// PHP 8.4
88
function array_find(array $array, callable $callback) {}
99

10+
// PHP 8.5
11+
function array_any(array $array, callable $callback): bool {}
12+

lib/private/Files/Config/MountProviderCollection.php

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99

1010
use OC\Hooks\Emitter;
1111
use OC\Hooks\EmitterTrait;
12+
use OCA\Files_Sharing\MountProvider;
1213
use OCP\Diagnostics\IEventLogger;
1314
use OCP\Files\Config\IHomeMountProvider;
1415
use OCP\Files\Config\IMountProvider;
16+
use OCP\Files\Config\IMountProviderArgs;
1517
use OCP\Files\Config\IMountProviderCollection;
18+
use OCP\Files\Config\IPartialMountProvider;
1619
use OCP\Files\Config\IRootMountProvider;
1720
use OCP\Files\Config\IUserMountCache;
1821
use OCP\Files\Mount\IMountManager;
1922
use OCP\Files\Mount\IMountPoint;
2023
use OCP\Files\Storage\IStorageFactory;
2124
use OCP\IUser;
25+
use function get_class;
26+
use function in_array;
2227

2328
class 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,29 +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_merge(...array_values($mounts));
75+
$mounts = array_merge(...$mounts);
7176
return $this->filterMounts($user, $mounts);
7277
}
7378

7479
/**
7580
* @return list<IMountPoint>
7681
*/
7782
public function getMountsForUser(IUser $user): array {
78-
return $this->getUserMountsForProviders($user, $this->providers);
83+
return $this->getUserMountsForProviders($user, array_values($this->providers));
84+
}
85+
86+
/**
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+
);
79113
}
80114

81115
/**
82116
* Returns the mounts for the user from the specified provider classes.
83117
* Providers not registered in the MountProviderCollection will be skipped.
84118
*
119+
* @inheritdoc
120+
*
85121
* @return list<IMountPoint>
86122
*/
87123
public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array {
88124
$providers = array_filter(
89125
$this->providers,
90-
fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses))
126+
fn (string $providerClass) => in_array($providerClass, $mountProviderClasses),
127+
ARRAY_FILTER_USE_KEY
91128
);
92-
return $this->getUserMountsForProviders($user, $providers);
129+
return $this->getUserMountsForProviders($user, array_values($providers));
93130
}
94131

95132
/**
@@ -100,16 +137,21 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla
100137
// to check for name collisions
101138
$firstMounts = [];
102139
if ($providerFilter) {
103-
$providers = array_filter($this->providers, $providerFilter);
140+
$providers = array_filter($this->providers, $providerFilter, ARRAY_FILTER_USE_KEY);
104141
} else {
105142
$providers = $this->providers;
106143
}
107-
$firstProviders = array_filter($providers, function (IMountProvider $provider) {
108-
return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
109-
});
110-
$lastProviders = array_filter($providers, function (IMountProvider $provider) {
111-
return (get_class($provider) === 'OCA\Files_Sharing\MountProvider');
112-
});
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+
);
113155
foreach ($firstProviders as $provider) {
114156
$mounts = $this->getMountsFromProvider($provider, $user, $this->loader);
115157
$firstMounts = array_merge($firstMounts, $mounts);
@@ -151,7 +193,7 @@ public function getHomeMountForUser(IUser $user): IMountPoint {
151193
* Add a provider for mount points
152194
*/
153195
public function registerProvider(IMountProvider $provider): void {
154-
$this->providers[] = $provider;
196+
$this->providers[get_class($provider)] = $provider;
155197

156198
$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
157199
}
@@ -229,7 +271,7 @@ public function clearProviders(): void {
229271
* @return list<IMountProvider>
230272
*/
231273
public function getProviders(): array {
232-
return $this->providers;
274+
return array_values($this->providers);
233275
}
234276

235277
/**

0 commit comments

Comments
 (0)