From 6f3bc93920758e3a7d52cc19b388edb875795b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 18 Jun 2024 15:15:28 +0200 Subject: [PATCH] chore: Add event to register mount providers more lazy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../Event/RegisterMountProviderListener.php | 44 ++++++++++++++++ .../files_sharing/lib/AppInfo/Application.php | 10 ++-- .../RegisterMountProviderListener.php | 36 +++++++++++++ .../Files/Config/MountProviderCollection.php | 52 ++++++++----------- lib/private/Server.php | 3 +- .../Events/RegisterMountProviderEvent.php | 34 ++++++++++++ 6 files changed, 141 insertions(+), 38 deletions(-) create mode 100644 OCA/Collectives/Listeners/Event/RegisterMountProviderListener.php create mode 100644 apps/files_sharing/lib/Listener/RegisterMountProviderListener.php create mode 100644 lib/public/Files/Events/RegisterMountProviderEvent.php diff --git a/OCA/Collectives/Listeners/Event/RegisterMountProviderListener.php b/OCA/Collectives/Listeners/Event/RegisterMountProviderListener.php new file mode 100644 index 0000000000000..0c26b44f051e9 --- /dev/null +++ b/OCA/Collectives/Listeners/Event/RegisterMountProviderListener.php @@ -0,0 +1,44 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\Collectives\Listeners\Event; + +use OCA\Collectives\Mount\MountProvider; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\RegisterMountProviderEvent; + +class RegisterMountProviderListener implements IEventListener { + + public function __construct( + private MountProvider $mountProvider, + ) { + } + + public function handle(Event $event): void { + if (!$event instanceof RegisterMountProviderEvent) { + return; + } + + $event->getProviderCollection()->registerProvider($this->mountProvider); + } +} diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 82a5981febf85..22093a7212eba 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -19,6 +19,7 @@ use OCA\Files_Sharing\Listener\BeforeZipCreatedListener; use OCA\Files_Sharing\Listener\LoadAdditionalListener; use OCA\Files_Sharing\Listener\LoadSidebarListener; +use OCA\Files_Sharing\Listener\RegisterMountProviderListener; use OCA\Files_Sharing\Listener\ShareInteractionListener; use OCA\Files_Sharing\Listener\UserAddedToGroupListener; use OCA\Files_Sharing\Listener\UserShareAcceptanceListener; @@ -40,6 +41,7 @@ use OCP\Files\Config\IMountProviderCollection; use OCP\Files\Events\BeforeDirectFileDownloadEvent; use OCP\Files\Events\BeforeZipCreatedEvent; +use OCP\Files\Events\RegisterMountProviderEvent; use OCP\Group\Events\GroupChangedEvent; use OCP\Group\Events\GroupDeletedEvent; use OCP\Group\Events\UserAddedEvent; @@ -95,6 +97,8 @@ function () use ($c) { // Handle download events for view only checks $context->registerEventListener(BeforeZipCreatedEvent::class, BeforeZipCreatedListener::class); $context->registerEventListener(BeforeDirectFileDownloadEvent::class, BeforeDirectFileDownloadListener::class); + + $context->registerEventListener(RegisterMountProviderEvent::class, RegisterMountProviderListener::class); } public function boot(IBootContext $context): void { @@ -107,12 +111,6 @@ public function boot(IBootContext $context): void { Share::registerBackend('folder', Folder::class, 'file'); } - - public function registerMountProviders(IMountProviderCollection $mountProviderCollection, MountProvider $mountProvider, ExternalMountProvider $externalMountProvider): void { - $mountProviderCollection->registerProvider($mountProvider); - $mountProviderCollection->registerProvider($externalMountProvider); - } - public function registerEventsScripts(IEventDispatcher $dispatcher): void { $dispatcher->addListener(ResourcesLoadAdditionalScriptsEvent::class, function () { \OCP\Util::addScript('files_sharing', 'collaboration'); diff --git a/apps/files_sharing/lib/Listener/RegisterMountProviderListener.php b/apps/files_sharing/lib/Listener/RegisterMountProviderListener.php new file mode 100644 index 0000000000000..c4d932f4fb745 --- /dev/null +++ b/apps/files_sharing/lib/Listener/RegisterMountProviderListener.php @@ -0,0 +1,36 @@ + */ +class RegisterMountProviderListener implements IEventListener { + + public function __construct( + private MountProvider $mountProvider, + private ExternalMountProvider $externalMountProvider, + ) { + } + + public function handle(Event $event): void { + if (!($event instanceof RegisterMountProviderEvent)) { + return; + } + + $mountProviderCollection = $event->getProviderCollection(); + $mountProviderCollection->registerProvider($this->mountProvider); + $mountProviderCollection->registerProvider($this->externalMountProvider); + } +} diff --git a/lib/private/Files/Config/MountProviderCollection.php b/lib/private/Files/Config/MountProviderCollection.php index 0e103690b6bb0..717dd724af388 100644 --- a/lib/private/Files/Config/MountProviderCollection.php +++ b/lib/private/Files/Config/MountProviderCollection.php @@ -10,11 +10,13 @@ use OC\Hooks\Emitter; use OC\Hooks\EmitterTrait; use OCP\Diagnostics\IEventLogger; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Config\IHomeMountProvider; use OCP\Files\Config\IMountProvider; use OCP\Files\Config\IMountProviderCollection; use OCP\Files\Config\IRootMountProvider; use OCP\Files\Config\IUserMountCache; +use OCP\Files\Events\RegisterMountProviderEvent; use OCP\Files\Mount\IMountManager; use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorageFactory; @@ -23,46 +25,30 @@ class MountProviderCollection implements IMountProviderCollection, Emitter { use EmitterTrait; - /** - * @var \OCP\Files\Config\IHomeMountProvider[] - */ - private $homeProviders = []; + /** @var \OCP\Files\Config\IHomeMountProvider[] */ + private array $homeProviders = []; - /** - * @var \OCP\Files\Config\IMountProvider[] - */ - private $providers = []; + /** @var \OCP\Files\Config\IMountProvider[] */ + private array $providers = []; /** @var \OCP\Files\Config\IRootMountProvider[] */ - private $rootProviders = []; - - /** - * @var \OCP\Files\Storage\IStorageFactory - */ - private $loader; - - /** - * @var \OCP\Files\Config\IUserMountCache - */ - private $mountCache; + private array $rootProviders = []; /** @var callable[] */ - private $mountFilters = []; + private array $mountFilters = []; - private IEventLogger $eventLogger; + private bool $registerEventEmitted = false; /** * @param \OCP\Files\Storage\IStorageFactory $loader * @param IUserMountCache $mountCache */ public function __construct( - IStorageFactory $loader, - IUserMountCache $mountCache, - IEventLogger $eventLogger + private IStorageFactory $loader, + private IUserMountCache $mountCache, + private IEventLogger $eventLogger, + private IEventDispatcher $eventDispatcher, ) { - $this->loader = $loader; - $this->mountCache = $mountCache; - $this->eventLogger = $eventLogger; } private function getMountsFromProvider(IMountProvider $provider, IUser $user, IStorageFactory $loader): array { @@ -91,12 +77,12 @@ private function getUserMountsForProviders(IUser $user, array $providers): array } public function getMountsForUser(IUser $user): array { - return $this->getUserMountsForProviders($user, $this->providers); + return $this->getUserMountsForProviders($user, $this->getProviders()); } public function getUserMountsForProviderClasses(IUser $user, array $mountProviderClasses): array { $providers = array_filter( - $this->providers, + $this->getProviders(), fn (IMountProvider $mountProvider) => (in_array(get_class($mountProvider), $mountProviderClasses)) ); return $this->getUserMountsForProviders($user, $providers); @@ -107,9 +93,9 @@ public function addMountForUser(IUser $user, IMountManager $mountManager, ?calla // to check for name collisions $firstMounts = []; if ($providerFilter) { - $providers = array_filter($this->providers, $providerFilter); + $providers = array_filter($this->getProviders(), $providerFilter); } else { - $providers = $this->providers; + $providers = $this->getProviders(); } $firstProviders = array_filter($providers, function (IMountProvider $provider) { return (get_class($provider) !== 'OCA\Files_Sharing\MountProvider'); @@ -236,6 +222,10 @@ public function clearProviders() { } public function getProviders(): array { + if (!$this->registerEventEmitted) { + $this->registerEventEmitted = true; + $this->eventDispatcher->dispatchTyped(new RegisterMountProviderEvent($this)); + } return $this->providers; } } diff --git a/lib/private/Server.php b/lib/private/Server.php index bcdf482f02d0e..a95241142995f 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -889,7 +889,8 @@ public function __construct($webRoot, \OC\Config $config) { $loader = $c->get(IStorageFactory::class); $mountCache = $c->get(IUserMountCache::class); $eventLogger = $c->get(IEventLogger::class); - $manager = new MountProviderCollection($loader, $mountCache, $eventLogger); + $eventDispatcher = $c->get(IEventDispatcher::class); + $manager = new MountProviderCollection($loader, $mountCache, $eventLogger, $eventDispatcher); // builtin providers diff --git a/lib/public/Files/Events/RegisterMountProviderEvent.php b/lib/public/Files/Events/RegisterMountProviderEvent.php new file mode 100644 index 0000000000000..63b2c54581fff --- /dev/null +++ b/lib/public/Files/Events/RegisterMountProviderEvent.php @@ -0,0 +1,34 @@ + + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCP\Files\Events; + +use OCP\EventDispatcher\Event; +use OCP\Files\Config\IMountProviderCollection; + +class RegisterMountProviderEvent extends Event { + private IMountProviderCollection $providerCollection; + + public function getProviderCollection(): IMountProviderCollection { + return $this->providerCollection; + } +}