Skip to content

Commit 1658ed3

Browse files
committed
chore: Add event to register mount providers more lazy
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 250bb12 commit 1658ed3

6 files changed

Lines changed: 140 additions & 32 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
namespace OCA\Collectives\Listeners\Event;
24+
25+
use OCA\Collectives\Mount\MountProvider;
26+
use OCP\EventDispatcher\Event;
27+
use OCP\EventDispatcher\IEventListener;
28+
use OCP\Files\Events\RegisterMountProviderEvent;
29+
30+
class RegisterMountProviderListener implements IEventListener {
31+
32+
public function __construct(
33+
private MountProvider $mountProvider,
34+
) {
35+
}
36+
37+
public function handle(Event $event): void {
38+
if (!$event instanceof RegisterMountProviderEvent) {
39+
return;
40+
}
41+
42+
$event->getProviderCollection()->registerProvider($this->mountProvider);
43+
}
44+
}

apps/files_sharing/lib/AppInfo/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use OCP\Files\Config\IMountProviderCollection;
4141
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
4242
use OCP\Files\Events\BeforeZipCreatedEvent;
43+
use OCP\Files\Events\RegisterMountProviderEvent;
4344
use OCP\Group\Events\GroupChangedEvent;
4445
use OCP\Group\Events\GroupDeletedEvent;
4546
use OCP\Group\Events\UserAddedEvent;
@@ -95,6 +96,8 @@ function () use ($c) {
9596
// Handle download events for view only checks
9697
$context->registerEventListener(BeforeZipCreatedEvent::class, BeforeZipCreatedListener::class);
9798
$context->registerEventListener(BeforeDirectFileDownloadEvent::class, BeforeDirectFileDownloadListener::class);
99+
100+
$context->registerEventListener(RegisterMountProviderEvent::class);
98101
}
99102

100103
public function boot(IBootContext $context): void {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Files_Sharing\Listener;
11+
12+
use OCA\Files_Sharing\External\MountProvider as ExternalMountProvider;
13+
use OCA\Files_Sharing\MountProvider;
14+
use OCP\EventDispatcher\Event;
15+
use OCP\EventDispatcher\IEventListener;
16+
use OCP\Files\Events\RegisterMountProviderEvent;
17+
18+
/** @template-implements IEventListener<RegisterMountProviderEvent> */
19+
class RegisterMountProviderListener implements IEventListener {
20+
21+
public function __construct(
22+
private MountProvider $mountProvider,
23+
private ExternalMountProvider $externalMountProvider,
24+
) {
25+
}
26+
27+
public function handle(Event $event): void {
28+
if (!($event instanceof RegisterMountProviderEvent)) {
29+
return;
30+
}
31+
32+
$mountProviderCollection = $event->getProviderCollection();
33+
$mountProviderCollection->registerProvider($this->mountProvider);
34+
$mountProviderCollection->registerProvider($this->externalMountProvider);
35+
}
36+
}

lib/private/Files/Config/MountProviderCollection.php

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
use OC\Hooks\Emitter;
1111
use OC\Hooks\EmitterTrait;
1212
use OCP\Diagnostics\IEventLogger;
13+
use OCP\EventDispatcher\IEventDispatcher;
1314
use OCP\Files\Config\IHomeMountProvider;
1415
use OCP\Files\Config\IMountProvider;
1516
use OCP\Files\Config\IMountProviderCollection;
1617
use OCP\Files\Config\IRootMountProvider;
1718
use OCP\Files\Config\IUserMountCache;
19+
use OCP\Files\Events\RegisterMountProviderEvent;
1820
use OCP\Files\Mount\IMountManager;
1921
use OCP\Files\Mount\IMountPoint;
2022
use OCP\Files\Storage\IStorageFactory;
@@ -23,46 +25,30 @@
2325
class MountProviderCollection implements IMountProviderCollection, Emitter {
2426
use EmitterTrait;
2527

26-
/**
27-
* @var \OCP\Files\Config\IHomeMountProvider[]
28-
*/
29-
private $homeProviders = [];
28+
/** @var \OCP\Files\Config\IHomeMountProvider[] */
29+
private array $homeProviders = [];
3030

31-
/**
32-
* @var \OCP\Files\Config\IMountProvider[]
33-
*/
34-
private $providers = [];
31+
/** @var \OCP\Files\Config\IMountProvider[] */
32+
private array $providers = [];
3533

3634
/** @var \OCP\Files\Config\IRootMountProvider[] */
37-
private $rootProviders = [];
38-
39-
/**
40-
* @var \OCP\Files\Storage\IStorageFactory
41-
*/
42-
private $loader;
43-
44-
/**
45-
* @var \OCP\Files\Config\IUserMountCache
46-
*/
47-
private $mountCache;
35+
private array $rootProviders = [];
4836

4937
/** @var callable[] */
50-
private $mountFilters = [];
38+
private array $mountFilters = [];
5139

52-
private IEventLogger $eventLogger;
40+
private bool $registerEventEmitted = false;
5341

5442
/**
5543
* @param \OCP\Files\Storage\IStorageFactory $loader
5644
* @param IUserMountCache $mountCache
5745
*/
5846
public function __construct(
59-
IStorageFactory $loader,
60-
IUserMountCache $mountCache,
61-
IEventLogger $eventLogger
47+
private IStorageFactory $loader,
48+
private IUserMountCache $mountCache,
49+
private IEventLogger $eventLogger,
50+
private IEventDispatcher $eventDispatcher,
6251
) {
63-
$this->loader = $loader;
64-
$this->mountCache = $mountCache;
65-
$this->eventLogger = $eventLogger;
6652
}
6753

6854
private function getMountsFromProvider(IMountProvider $provider, IUser $user, IStorageFactory $loader): array {
@@ -91,12 +77,12 @@ private function getUserMountsForProviders(IUser $user, array $providers): array
9177
}
9278

9379
public function getMountsForUser(IUser $user): array {
94-
return $this->getUserMountsForProviders($user, $this->providers);
80+
return $this->getUserMountsForProviders($user, $this->getProviders());
9581
}
9682

9783
public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array {
9884
$providers = array_filter(
99-
$this->providers,
85+
$this->getProviders(),
10086
fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses))
10187
);
10288
return $this->getUserMountsForProviders($user, $providers);
@@ -107,9 +93,9 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla
10793
// to check for name collisions
10894
$firstMounts = [];
10995
if ($providerFilter) {
110-
$providers = array_filter($this->providers, $providerFilter);
96+
$providers = array_filter($this->getProviders(), $providerFilter);
11197
} else {
112-
$providers = $this->providers;
98+
$providers = $this->getProviders();
11399
}
114100
$firstProviders = array_filter($providers, function (IMountProvider $provider) {
115101
return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider');
@@ -236,6 +222,10 @@ public function clearProviders() {
236222
}
237223

238224
public function getProviders(): array {
225+
if (!$this->registerEventEmitted) {
226+
$this->registerEventEmitted = true;
227+
$this->eventDispatcher->dispatchTyped(new RegisterMountProviderEvent($this));
228+
}
239229
return $this->providers;
240230
}
241231
}

lib/private/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,8 @@ public function __construct($webRoot, \OC\Config $config) {
889889
$loader = $c->get(IStorageFactory::class);
890890
$mountCache = $c->get(IUserMountCache::class);
891891
$eventLogger = $c->get(IEventLogger::class);
892-
$manager = new MountProviderCollection($loader, $mountCache, $eventLogger);
892+
$eventDispatcher = $c->get(IEventDispatcher::class);
893+
$manager = new MountProviderCollection($loader, $mountCache, $eventLogger, $eventDispatcher);
893894

894895
// builtin providers
895896

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
namespace OCP\Files\Events;
24+
25+
use OCP\EventDispatcher\Event;
26+
use OCP\Files\Config\IMountProviderCollection;
27+
28+
class RegisterMountProviderEvent extends Event {
29+
private IMountProviderCollection $providerCollection;
30+
31+
public function getProviderCollection(): IMountProviderCollection {
32+
return $this->providerCollection;
33+
}
34+
}

0 commit comments

Comments
 (0)