Skip to content

Commit

Permalink
feat(OCM): Dispatch an event so apps can register resources
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Oct 13, 2023
1 parent fc445a2 commit 51ca141
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Expand Up @@ -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\\ResourceTypeRegisterEvent' => $baseDir . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Expand Up @@ -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\\ResourceTypeRegisterEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.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',
Expand Down
15 changes: 15 additions & 0 deletions lib/private/OCM/Model/OCMProvider.php
Expand Up @@ -26,6 +26,8 @@

namespace OC\OCM\Model;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\OCM\Events\ResourceTypeRegisterEvent;
use OCP\OCM\Exceptions\OCMArgumentException;
use OCP\OCM\Exceptions\OCMProviderException;
use OCP\OCM\IOCMProvider;
Expand All @@ -41,6 +43,13 @@ class OCMProvider implements IOCMProvider {
/** @var IOCMResource[] */
private array $resourceTypes = [];

private bool $emittedEvent = false;

public function __construct(
protected IEventDispatcher $dispatcher,
) {
}

/**
* @param bool $enabled
*
Expand Down Expand Up @@ -129,6 +138,12 @@ public function setResourceTypes(array $resourceTypes): static {
* @return IOCMResource[]
*/
public function getResourceTypes(): array {
if (!$this->emittedEvent) {
$this->emittedEvent = true;
$event = new ResourceTypeRegisterEvent($this);
$this->dispatcher->dispatchTyped($event);
}

return $this->resourceTypes;
}

Expand Down
58 changes: 58 additions & 0 deletions lib/public/OCM/Events/ResourceTypeRegisterEvent.php
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);
/*
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\OCM\Events;

use OCP\EventDispatcher\Event;
use OCP\OCM\IOCMProvider;

/**
* Use this event to register additional OCM resources before the API returns
* them in the OCM provider list and capability
*
* @since 28.0.0
*/
class ResourceTypeRegisterEvent extends Event {
public function __construct(

Check failure on line 38 in lib/public/OCM/Events/ResourceTypeRegisterEvent.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/OCM/Events/ResourceTypeRegisterEvent.php:38:2: InvalidDocblock: PHPDoc is required for methods in OCP. (see https://psalm.dev/008)
protected IOCMProvider $provider,
) {
parent::__construct();
}

/**
* @param string $name
* @param list<string> $shareTypes List of supported share recipients, e.g. 'user', 'group', …
* @param array<string, string> $protocols List of supported protocols and their location,
* e.g. ['webdav' => '/remote.php/webdav/']
* @since 28.0.0
*/
public function registerResourceType(string $name, array $shareTypes, array $protocols): void {
$resourceType = $this->provider->createNewResourceType();
$resourceType->setName($name)
->setShareTypes($shareTypes)
->setProtocols($protocols);
$this->provider->addResourceType($resourceType);
}
}

0 comments on commit 51ca141

Please sign in to comment.