From 9a6869943e9a929cd2e0331c1d25d07f03d865e9 Mon Sep 17 00:00:00 2001 From: Julius Härtl Date: Thu, 12 May 2022 10:18:40 +0200 Subject: Introduce event for loading additional script on projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/base.php | 4 ++- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Collaboration/Resources/Listener.php | 33 ++++++++++++++---- .../Resources/LoadAdditionalScriptsEvent.php | 40 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index 8cf5360b084..cd9f4fc2478 100644 --- a/lib/base.php +++ b/lib/base.php @@ -62,9 +62,11 @@ * */ +use OC\EventDispatcher\SymfonyAdapter; use OCP\EventDispatcher\IEventDispatcher; use OCP\Group\Events\UserRemovedEvent; use OCP\ILogger; +use OCP\Server; use OCP\Share; use OC\Encryption\HookManager; use OC\Files\Filesystem; @@ -881,7 +883,7 @@ class OC { } private static function registerResourceCollectionHooks() { - \OC\Collaboration\Resources\Listener::register(\OC::$server->getEventDispatcher()); + \OC\Collaboration\Resources\Listener::register(Server::get(SymfonyAdapter::class), Server::get(IEventDispatcher::class)); } /** diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 233c374edbf..96666ee6021 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -148,6 +148,7 @@ return array( 'OCP\\Collaboration\\Resources\\IProvider' => $baseDir . '/lib/public/Collaboration/Resources/IProvider.php', 'OCP\\Collaboration\\Resources\\IProviderManager' => $baseDir . '/lib/public/Collaboration/Resources/IProviderManager.php', 'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php', + 'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php', 'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php', 'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php', 'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 0b59d14657d..ddea8460724 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -177,6 +177,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Collaboration\\Resources\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProvider.php', 'OCP\\Collaboration\\Resources\\IProviderManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IProviderManager.php', 'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php', + 'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php', 'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php', 'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php', 'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php', diff --git a/lib/private/Collaboration/Resources/Listener.php b/lib/private/Collaboration/Resources/Listener.php index ba012b4ca44..b9f1e72cbfa 100644 --- a/lib/private/Collaboration/Resources/Listener.php +++ b/lib/private/Collaboration/Resources/Listener.php @@ -26,14 +26,16 @@ declare(strict_types=1); */ namespace OC\Collaboration\Resources; +use OC\EventDispatcher\SymfonyAdapter; use OCP\Collaboration\Resources\IManager; +use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IGroup; use OCP\IUser; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; class Listener { - public static function register(EventDispatcherInterface $dispatcher): void { + public static function register(SymfonyAdapter $symfonyDispatcher, IEventDispatcher $eventDispatcher): void { $listener = function (GenericEvent $event) { /** @var IUser $user */ $user = $event->getArgument('user'); @@ -42,10 +44,10 @@ class Listener { $resourceManager->invalidateAccessCacheForUser($user); }; - $dispatcher->addListener(IGroup::class . '::postAddUser', $listener); - $dispatcher->addListener(IGroup::class . '::postRemoveUser', $listener); + $symfonyDispatcher->addListener(IGroup::class . '::postAddUser', $listener); + $symfonyDispatcher->addListener(IGroup::class . '::postRemoveUser', $listener); - $dispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) { + $symfonyDispatcher->addListener(IUser::class . '::postDelete', function (GenericEvent $event) { /** @var IUser $user */ $user = $event->getSubject(); /** @var IManager $resourceManager */ @@ -54,7 +56,7 @@ class Listener { $resourceManager->invalidateAccessCacheForUser($user); }); - $dispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) { + $symfonyDispatcher->addListener(IGroup::class . '::preDelete', function (GenericEvent $event) { /** @var IGroup $group */ $group = $event->getSubject(); /** @var IManager $resourceManager */ @@ -64,5 +66,24 @@ class Listener { $resourceManager->invalidateAccessCacheForUser($user); } }); + + // Stay backward compatible with the legacy event for now + $fallbackEventRunning = false; + $symfonyDispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () use ($eventDispatcher, &$fallbackEventRunning) { + if ($fallbackEventRunning) { + return; + } + $fallbackEventRunning = true; + $eventDispatcher->dispatchTyped(new LoadAdditionalScriptsEvent()); + $fallbackEventRunning = false; + }); + $eventDispatcher->addListener(LoadAdditionalScriptsEvent::class, static function () use ($symfonyDispatcher, &$fallbackEventRunning) { + if ($fallbackEventRunning) { + return; + } + $fallbackEventRunning = true; + $symfonyDispatcher->dispatch('\OCP\Collaboration\Resources::loadAdditionalScripts'); + $fallbackEventRunning = false; + }); } } diff --git a/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php b/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php new file mode 100644 index 00000000000..2f1dc5a0a00 --- /dev/null +++ b/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php @@ -0,0 +1,40 @@ + + * + * @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\Collaboration\Resources; + +use OCP\EventDispatcher\Event; + +/** + * This event is used by apps to register their own frontend scripts for integrating + * projects in their app. Apps also need to dispatch the event in order to load + * scripts during page load + * + * @see https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/projects.html + * @since 25.0.0 + */ +class LoadAdditionalScriptsEvent extends Event { +} -- cgit v1.2.3