From 27d98c1e6d08c1e02ae5a343ef47b8d3581db743 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 10 Nov 2020 20:49:24 +0100 Subject: [PATCH 1/2] Move files_sharing from app.php to IBootstrap Signed-off-by: Morris Jobke --- apps/files_sharing/appinfo/app.php | 35 --------- .../files_sharing/lib/AppInfo/Application.php | 73 +++++++++++-------- 2 files changed, 43 insertions(+), 65 deletions(-) delete mode 100644 apps/files_sharing/appinfo/app.php diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php deleted file mode 100644 index 84fb68dbe2e61..0000000000000 --- a/apps/files_sharing/appinfo/app.php +++ /dev/null @@ -1,35 +0,0 @@ - - * @author John Molakvoæ (skjnldsv) - * @author Morris Jobke - * @author Robin Appelman - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -use OCA\Files_Sharing\ShareBackend\File; -use OCA\Files_Sharing\ShareBackend\Folder; -use OCA\Files_Sharing\AppInfo\Application; - -\OCA\Files_Sharing\Helper::registerHooks(); - -\OC\Share\Share::registerBackend('file', File::class); -\OC\Share\Share::registerBackend('folder', Folder::class, 'file'); - -\OC::$server->query(Application::class); diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 078a0a5f59dee..58be4779604e5 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -48,30 +48,62 @@ use OCA\Files_Sharing\Notification\Notifier; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files\Event\LoadSidebar; +use OCA\Files_Sharing\ShareBackend\File; +use OCA\Files_Sharing\ShareBackend\Folder; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudIdManager; -use OCP\Files\Config\IMountProviderCollection; use OCP\Group\Events\UserAddedEvent; use OCP\IDBConnection; use OCP\IGroup; -use OCP\IServerContainer; use OCP\Share\Events\ShareCreatedEvent; use OCP\Util; use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'files_sharing'; public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); + } + + public function register(IRegistrationContext $context): void { + /** + * Middleware + */ + $context->registerMiddleWare(SharingCheckMiddleware::class); + $context->registerMiddleWare(OCSShareAPIMiddleware::class); + $context->registerMiddleWare(ShareInfoMiddleware::class); + + /** + * Capabilities + */ + $context->registerCapability(Capabilities::class); + + /** + * Events + */ + $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); + $context->registerEventListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class); + $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class); + $context->registerEventListener(ShareCreatedEvent::class, ShareInteractionListener::class); + $context->registerEventListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class); + $context->registerEventListener(UserAddedEvent::class, UserAddedToGroupListener::class); + } - $container = $this->getContainer(); + public function boot(IBootContext $context): void { + \OCA\Files_Sharing\Helper::registerHooks(); - /** @var IServerContainer $server */ - $server = $container->getServer(); + \OC\Share\Share::registerBackend('file', File::class); + \OC\Share\Share::registerBackend('folder', Folder::class, 'file'); + + $container = $context->getAppContainer(); + $server = $context->getServerContainer(); /** @var IEventDispatcher $dispatcher */ $dispatcher = $container->query(IEventDispatcher::class); @@ -100,14 +132,6 @@ public function __construct(array $urlParams = []) { $server->query(IEventDispatcher::class) ); }); - - /** - * Middleware - */ - $container->registerMiddleWare(SharingCheckMiddleware::class); - $container->registerMiddleWare(OCSShareAPIMiddleware::class); - $container->registerMiddleWare(ShareInfoMiddleware::class); - $container->registerService('ExternalMountProvider', function (ContainerInterface $c) { return new \OCA\Files_Sharing\External\MountProvider( $c->get(IDBConnection::class), @@ -118,14 +142,14 @@ function () use ($c) { ); }); + $notifications->registerNotifierService(Notifier::class); + /** - * Register capabilities + * Mount provider */ - $container->registerCapability(Capabilities::class); - - $notifications->registerNotifierService(Notifier::class); + $mountProviderCollection->registerProvider($container->query(MountProvider::class)); + $mountProviderCollection->registerProvider($container->query('ExternalMountProvider')); - $this->registerMountProviders($mountProviderCollection); $this->registerEventsScripts($dispatcher, $oldDispatcher); $this->setupSharingMenus(); @@ -135,22 +159,11 @@ function () use ($c) { Util::addScript(self::APP_ID, 'dist/main'); } - protected function registerMountProviders(IMountProviderCollection $mountProviderCollection) { - $mountProviderCollection->registerProvider($this->getContainer()->query(MountProvider::class)); - $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); - } - protected function registerEventsScripts(IEventDispatcher $dispatcher, EventDispatcherInterface $oldDispatcher) { // sidebar and files scripts - $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class); - $dispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, LegacyBeforeTemplateRenderedListener::class); - $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class); - $dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class); $dispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () { \OCP\Util::addScript('files_sharing', 'dist/collaboration'); }); - $dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class); - $dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class); // notifications api to accept incoming user shares $oldDispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) { From 1c4a9e5e6a5cb77dd15c5ebbd8860506424732ae Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 23 Nov 2020 22:34:51 +0100 Subject: [PATCH 2/2] Try to unregirster share backend after each test Signed-off-by: Morris Jobke --- apps/files_sharing/tests/SharedStorageTest.php | 3 +++ lib/private/Share/Share.php | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/apps/files_sharing/tests/SharedStorageTest.php b/apps/files_sharing/tests/SharedStorageTest.php index 6b51c471586c5..6ceb411084240 100644 --- a/apps/files_sharing/tests/SharedStorageTest.php +++ b/apps/files_sharing/tests/SharedStorageTest.php @@ -71,6 +71,9 @@ protected function tearDown(): void { \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin'); + \OC\Share\Share::unregisterBackend('file'); + \OC\Share\Share::unregisterBackend('folder'); + parent::tearDown(); } diff --git a/lib/private/Share/Share.php b/lib/private/Share/Share.php index 2d0d4f1cf87e1..ce17584cdd734 100644 --- a/lib/private/Share/Share.php +++ b/lib/private/Share/Share.php @@ -89,6 +89,15 @@ public static function registerBackend($itemType, $class, $collectionOf = null, return false; } + /** + * Unregister a sharing backend + * @param string $itemType Item type + * @internal FOR TESTING ONLY + */ + public static function unregisterBackend($itemType) { + unset(self::$backendTypes[$itemType]); + } + /** * Get the items of item type shared with the current user * @param string $itemType