From 506a1f30c0ac228836cf46ea6eeda5481d619dac Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 12 Oct 2023 14:37:50 +0200 Subject: [PATCH] feat(OCM): Dispatch an event so apps can register resources Signed-off-by: Joas Schilling --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/OCM/Model/OCMProvider.php | 15 ++++++++++ .../Events/BeforeResourceTypesGetEvent.php | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 lib/public/OCM/Events/BeforeResourceTypesGetEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 108a2f74b5031..f5f86cf42160a 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -537,6 +537,7 @@ 'OCP\\Notification\\IManager' => $baseDir . '/lib/public/Notification/IManager.php', 'OCP\\Notification\\INotification' => $baseDir . '/lib/public/Notification/INotification.php', 'OCP\\Notification\\INotifier' => $baseDir . '/lib/public/Notification/INotifier.php', + 'OCP\\OCM\\Events\\BeforeResourceTypesGetEvent' => $baseDir . '/lib/public/OCM/Events/BeforeResourceTypesGetEvent.php', 'OCP\\OCM\\Exceptions\\OCMArgumentException' => $baseDir . '/lib/public/OCM/Exceptions/OCMArgumentException.php', 'OCP\\OCM\\Exceptions\\OCMProviderException' => $baseDir . '/lib/public/OCM/Exceptions/OCMProviderException.php', 'OCP\\OCM\\IOCMDiscoveryService' => $baseDir . '/lib/public/OCM/IOCMDiscoveryService.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index ae1f13fc89bde..2b60b2fba9b74 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -570,6 +570,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Notification\\IManager' => __DIR__ . '/../../..' . '/lib/public/Notification/IManager.php', 'OCP\\Notification\\INotification' => __DIR__ . '/../../..' . '/lib/public/Notification/INotification.php', 'OCP\\Notification\\INotifier' => __DIR__ . '/../../..' . '/lib/public/Notification/INotifier.php', + 'OCP\\OCM\\Events\\BeforeResourceTypesGetEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/BeforeResourceTypesGetEvent.php', 'OCP\\OCM\\Exceptions\\OCMArgumentException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMArgumentException.php', 'OCP\\OCM\\Exceptions\\OCMProviderException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMProviderException.php', 'OCP\\OCM\\IOCMDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCM/IOCMDiscoveryService.php', diff --git a/lib/private/OCM/Model/OCMProvider.php b/lib/private/OCM/Model/OCMProvider.php index 9156fc7631fa3..ad92be24c245e 100644 --- a/lib/private/OCM/Model/OCMProvider.php +++ b/lib/private/OCM/Model/OCMProvider.php @@ -27,6 +27,8 @@ namespace OC\OCM\Model; use JsonSerializable; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\OCM\Events\BeforeResourceTypesGetEvent; use OCP\OCM\Exceptions\OCMArgumentException; use OCP\OCM\Exceptions\OCMProviderException; use OCP\OCM\IOCMProvider; @@ -42,6 +44,13 @@ class OCMProvider implements IOCMProvider, JsonSerializable { /** @var IOCMResource[] */ private array $resourceTypes = []; + private bool $emittedEvent = false; + + public function __construct( + protected IEventDispatcher $dispatcher, + ) { + } + /** * @param bool $enabled * @@ -130,6 +139,12 @@ public function setResourceTypes(array $resourceTypes): self { * @return IOCMResource[] */ public function getResourceTypes(): array { + if (!$this->emittedEvent) { + $this->emittedEvent = true; + $event = new BeforeResourceTypesGetEvent(); + $this->dispatcher->dispatchTyped($event); + } + return $this->resourceTypes; } diff --git a/lib/public/OCM/Events/BeforeResourceTypesGetEvent.php b/lib/public/OCM/Events/BeforeResourceTypesGetEvent.php new file mode 100644 index 0000000000000..9aa8df39a3e7b --- /dev/null +++ b/lib/public/OCM/Events/BeforeResourceTypesGetEvent.php @@ -0,0 +1,29 @@ + + * + * @author Joas Schilling + * + * @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\OCM\Events; + +use OCP\EventDispatcher\Event; + +class BeforeResourceTypesGetEvent extends Event { +}