2018-06-18 14:26:32 +02:00
|
|
|
<?php
|
2022-02-04 11:13:50 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-06-18 14:26:32 +02:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-06-18 14:26:32 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Calendar\Resource;
|
|
|
|
|
2022-02-04 11:13:50 +01:00
|
|
|
use OC\AppFramework\Bootstrap\Coordinator;
|
2024-05-16 21:27:07 +02:00
|
|
|
use OC\Calendar\ResourcesRoomsUpdater;
|
2018-06-18 14:26:32 +02:00
|
|
|
use OCP\Calendar\Resource\IBackend;
|
2022-02-04 11:13:50 +01:00
|
|
|
use OCP\Calendar\Resource\IManager;
|
2018-10-08 01:50:17 +02:00
|
|
|
use OCP\IServerContainer;
|
2018-06-18 14:26:32 +02:00
|
|
|
|
2022-02-04 11:13:50 +01:00
|
|
|
class Manager implements IManager {
|
|
|
|
private bool $bootstrapBackendsLoaded = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[] holds all registered resource backends
|
|
|
|
* @psalm-var class-string<IBackend>[]
|
|
|
|
*/
|
2023-06-25 13:58:30 +03:30
|
|
|
private array $backends = [];
|
2018-06-18 14:26:32 +02:00
|
|
|
|
2018-08-24 15:21:04 +02:00
|
|
|
/** @var IBackend[] holds all backends that have been initialized already */
|
2023-06-25 13:58:30 +03:30
|
|
|
private array $initializedBackends = [];
|
2018-08-24 15:21:04 +02:00
|
|
|
|
2023-06-25 13:58:30 +03:30
|
|
|
public function __construct(
|
|
|
|
private Coordinator $bootstrapCoordinator,
|
|
|
|
private IServerContainer $server,
|
2024-05-16 21:27:07 +02:00
|
|
|
private ResourcesRoomsUpdater $updater,
|
2023-06-25 13:58:30 +03:30
|
|
|
) {
|
2018-10-08 01:50:17 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 14:26:32 +02:00
|
|
|
/**
|
|
|
|
* Registers a resource backend
|
|
|
|
*
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2023-06-25 13:58:30 +03:30
|
|
|
public function registerBackend(string $backendClass): void {
|
2018-08-24 15:21:04 +02:00
|
|
|
$this->backends[$backendClass] = $backendClass;
|
2018-06-18 14:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregisters a resource backend
|
|
|
|
*
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2023-06-25 13:58:30 +03:30
|
|
|
public function unregisterBackend(string $backendClass): void {
|
2018-08-24 15:21:04 +02:00
|
|
|
unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
|
2018-06-18 14:26:32 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 11:13:50 +01:00
|
|
|
private function fetchBootstrapBackends(): void {
|
|
|
|
if ($this->bootstrapBackendsLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = $this->bootstrapCoordinator->getRegistrationContext();
|
|
|
|
if ($context === null) {
|
|
|
|
// Too soon
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($context->getCalendarResourceBackendRegistrations() as $registration) {
|
|
|
|
$this->backends[] = $registration->getService();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 14:26:32 +02:00
|
|
|
/**
|
|
|
|
* @return IBackend[]
|
2018-08-24 15:21:04 +02:00
|
|
|
* @throws \OCP\AppFramework\QueryException
|
2018-06-18 14:26:32 +02:00
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function getBackends():array {
|
2022-02-04 11:13:50 +01:00
|
|
|
$this->fetchBootstrapBackends();
|
|
|
|
|
2018-08-24 15:21:04 +02:00
|
|
|
foreach ($this->backends as $backend) {
|
|
|
|
if (isset($this->initializedBackends[$backend])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-08 01:50:17 +02:00
|
|
|
$this->initializedBackends[$backend] = $this->server->query($backend);
|
2018-08-24 15:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return array_values($this->initializedBackends);
|
2018-06-18 14:26:32 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 18:15:50 +02:00
|
|
|
/**
|
|
|
|
* @param string $backendId
|
2018-08-24 15:21:04 +02:00
|
|
|
* @throws \OCP\AppFramework\QueryException
|
2018-06-18 18:15:50 +02:00
|
|
|
*/
|
2023-06-25 13:58:30 +03:30
|
|
|
public function getBackend($backendId): ?IBackend {
|
2018-08-24 15:21:04 +02:00
|
|
|
$backends = $this->getBackends();
|
|
|
|
foreach ($backends as $backend) {
|
|
|
|
if ($backend->getBackendIdentifier() === $backendId) {
|
|
|
|
return $backend;
|
|
|
|
}
|
2018-06-18 18:15:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 15:21:04 +02:00
|
|
|
return null;
|
2018-06-18 18:15:50 +02:00
|
|
|
}
|
|
|
|
|
2018-06-18 14:26:32 +02:00
|
|
|
/**
|
|
|
|
* removes all registered backend instances
|
2023-06-25 13:58:30 +03:30
|
|
|
*
|
2018-06-18 14:26:32 +02:00
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2023-06-25 13:58:30 +03:30
|
|
|
public function clear(): void {
|
2018-06-18 14:26:32 +02:00
|
|
|
$this->backends = [];
|
2018-08-24 15:21:04 +02:00
|
|
|
$this->initializedBackends = [];
|
2018-06-18 14:26:32 +02:00
|
|
|
}
|
2024-05-16 21:27:07 +02:00
|
|
|
|
|
|
|
public function update(): void {
|
|
|
|
$this->updater->updateResources();
|
|
|
|
}
|
2018-06-18 14:26:32 +02:00
|
|
|
}
|