From 0a24d1f8df52a1f719e45538de72f034b1f9ff83 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 5 Jun 2020 16:17:22 +0200 Subject: [PATCH] Migrate Comments to the new bootstrap mechanism Signed-off-by: Christoph Wurst --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + apps/comments/lib/AppInfo/Application.php | 74 +++++++++---------- .../Listener/CommentsEntityEventListener.php | 44 +++++++++++ 4 files changed, 81 insertions(+), 39 deletions(-) create mode 100644 apps/comments/lib/Listener/CommentsEntityEventListener.php diff --git a/apps/comments/composer/composer/autoload_classmap.php b/apps/comments/composer/composer/autoload_classmap.php index c1cdce7d14..d5d51c7a12 100644 --- a/apps/comments/composer/composer/autoload_classmap.php +++ b/apps/comments/composer/composer/autoload_classmap.php @@ -16,6 +16,7 @@ return array( 'OCA\\Comments\\Controller\\Notifications' => $baseDir . '/../lib/Controller/Notifications.php', 'OCA\\Comments\\EventHandler' => $baseDir . '/../lib/EventHandler.php', 'OCA\\Comments\\JSSettingsHelper' => $baseDir . '/../lib/JSSettingsHelper.php', + 'OCA\\Comments\\Listener\\CommentsEntityEventListener' => $baseDir . '/../lib/Listener/CommentsEntityEventListener.php', 'OCA\\Comments\\Listener\\LoadAdditionalScripts' => $baseDir . '/../lib/Listener/LoadAdditionalScripts.php', 'OCA\\Comments\\Listener\\LoadSidebarScripts' => $baseDir . '/../lib/Listener/LoadSidebarScripts.php', 'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php', diff --git a/apps/comments/composer/composer/autoload_static.php b/apps/comments/composer/composer/autoload_static.php index b38ae5841a..1292415290 100644 --- a/apps/comments/composer/composer/autoload_static.php +++ b/apps/comments/composer/composer/autoload_static.php @@ -31,6 +31,7 @@ class ComposerStaticInitComments 'OCA\\Comments\\Controller\\Notifications' => __DIR__ . '/..' . '/../lib/Controller/Notifications.php', 'OCA\\Comments\\EventHandler' => __DIR__ . '/..' . '/../lib/EventHandler.php', 'OCA\\Comments\\JSSettingsHelper' => __DIR__ . '/..' . '/../lib/JSSettingsHelper.php', + 'OCA\\Comments\\Listener\\CommentsEntityEventListener' => __DIR__ . '/..' . '/../lib/Listener/CommentsEntityEventListener.php', 'OCA\\Comments\\Listener\\LoadAdditionalScripts' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalScripts.php', 'OCA\\Comments\\Listener\\LoadSidebarScripts' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarScripts.php', 'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php', diff --git a/apps/comments/lib/AppInfo/Application.php b/apps/comments/lib/AppInfo/Application.php index 7f3235eb62..8bcf17b2af 100644 --- a/apps/comments/lib/AppInfo/Application.php +++ b/apps/comments/lib/AppInfo/Application.php @@ -31,6 +31,7 @@ use OCA\Comments\Capabilities; use OCA\Comments\Controller\Notifications; use OCA\Comments\EventHandler; use OCA\Comments\JSSettingsHelper; +use OCA\Comments\Listener\CommentsEntityEventListener; use OCA\Comments\Listener\LoadAdditionalScripts; use OCA\Comments\Listener\LoadSidebarScripts; use OCA\Comments\Notification\Notifier; @@ -38,60 +39,55 @@ use OCA\Comments\Search\Provider; use OCA\Files\Event\LoadAdditionalScriptsEvent; use OCA\Files\Event\LoadSidebar; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Comments\CommentsEntityEvent; -use OCP\EventDispatcher\IEventDispatcher; +use OCP\IServerContainer; use OCP\Util; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'comments'; public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); - $container = $this->getContainer(); + } - $container->registerAlias('NotificationsController', Notifications::class); + public function register(IRegistrationContext $context): void { + $context->registerCapability(Capabilities::class); - $jsSettingsHelper = new JSSettingsHelper($container->getServer()); + $context->registerServiceAlias('NotificationsController', Notifications::class); + + $context->registerEventListener( + LoadAdditionalScriptsEvent::class, + LoadAdditionalScripts::class + ); + $context->registerEventListener( + LoadSidebar::class, + LoadSidebarScripts::class + ); + $context->registerEventListener( + CommentsEntityEvent::EVENT_ENTITY, + CommentsEntityEventListener::class + ); + } + + public function boot(IBootContext $context): void { + $this->registerNotifier($context->getServerContainer()); + $this->registerCommentsEventHandler($context->getServerContainer()); + + $jsSettingsHelper = new JSSettingsHelper($context->getServerContainer()); Util::connectHook('\OCP\Config', 'js', $jsSettingsHelper, 'extend'); - $this->register(); + $context->getServerContainer()->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]); } - private function register() { - $server = $this->getContainer()->getServer(); - - /** @var IEventDispatcher $newDispatcher */ - $dispatcher = $server->query(IEventDispatcher::class); - - $this->registerEventsScripts($dispatcher); - $this->registerDavEntity($dispatcher); - $this->registerNotifier(); - $this->registerCommentsEventHandler(); - - $this->getContainer()->registerCapability(Capabilities::class); - $server->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]); + protected function registerNotifier(IServerContainer $container) { + $container->getNotificationManager()->registerNotifierService(Notifier::class); } - protected function registerEventsScripts(IEventDispatcher $dispatcher) { - $dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScripts::class); - $dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarScripts::class); - } - - protected function registerDavEntity(IEventDispatcher $dispatcher) { - $dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function (CommentsEntityEvent $event) { - $event->addEntityCollection('files', function ($name) { - $nodes = \OC::$server->getUserFolder()->getById((int)$name); - return !empty($nodes); - }); - }); - } - - protected function registerNotifier() { - $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); - } - - protected function registerCommentsEventHandler() { - $this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () { + protected function registerCommentsEventHandler(IServerContainer $container) { + $container->getCommentsManager()->registerEventHandler(function () { return $this->getContainer()->query(EventHandler::class); }); } diff --git a/apps/comments/lib/Listener/CommentsEntityEventListener.php b/apps/comments/lib/Listener/CommentsEntityEventListener.php new file mode 100644 index 0000000000..c08d992405 --- /dev/null +++ b/apps/comments/lib/Listener/CommentsEntityEventListener.php @@ -0,0 +1,44 @@ + + * + * @author 2020 Christoph Wurst + * + * @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\Comments\Listener; + +use OCP\Comments\CommentsEntityEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +class CommentsEntityEventListener implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof CommentsEntityEvent)) { + // Unrelated + return; + } + + $event->addEntityCollection('files', function ($name) { + $nodes = \OC::$server->getUserFolder()->getById((int)$name); + return !empty($nodes); + }); + } +}