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 12, 2023
1 parent 2069c4f commit 506a1f3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
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\\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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
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\\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',
Expand Down
15 changes: 15 additions & 0 deletions lib/private/OCM/Model/OCMProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions lib/public/OCM/Events/BeforeResourceTypesGetEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* @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;

class BeforeResourceTypesGetEvent extends Event {
}

0 comments on commit 506a1f3

Please sign in to comment.