Skip to content

Commit

Permalink
Move calendar resource/room backend registration to IBootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed Feb 7, 2022
1 parent bb55b79 commit 9a656e5
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 102 deletions.
52 changes: 52 additions & 0 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
namespace OC\AppFramework\Bootstrap;

use Closure;
use OCP\Calendar\Resource\IBackend as IResourceBackend;
use OCP\Calendar\Room\IBackend as IRoomBackend;
use OCP\Talk\ITalkBackend;
use RuntimeException;
use function array_shift;
Expand Down Expand Up @@ -70,6 +72,12 @@ class RegistrationContext {
/** @var null|ServiceRegistration<ITalkBackend> */
private $talkBackendRegistration = null;

/** @var ServiceRegistration<IResourceBackend>[] */
private $calendarResourceBackendRegistrations = [];

/** @var ServiceRegistration<IRoomBackend>[] */
private $calendarRoomBackendRegistrations = [];

/** @var ServiceFactoryRegistration[] */
private $services = [];

Expand Down Expand Up @@ -271,6 +279,20 @@ public function registerTalkBackend(string $backend): void {
$backend
);
}

public function registerCalendarResourceBackend(string $class): void {
$this->context->registerCalendarResourceBackend(
$this->appId,
$class
);
}

public function registerCalendarRoomBackend(string $class): void {
$this->context->registerCalendarRoomBackend(
$this->appId,
$class
);
}
};
}

Expand Down Expand Up @@ -376,6 +398,20 @@ public function registerTalkBackend(string $appId, string $backend) {
$this->talkBackendRegistration = new ServiceRegistration($appId, $backend);
}

public function registerCalendarResourceBackend(string $appId, string $class) {
$this->calendarResourceBackendRegistrations[] = new ServiceRegistration(
$appId,
$class,
);
}

public function registerCalendarRoomBackend(string $appId, string $class) {
$this->calendarRoomBackendRegistrations[] = new ServiceRegistration(
$appId,
$class,
);
}

/**
* @param App[] $apps
*/
Expand Down Expand Up @@ -635,4 +671,20 @@ public function getProfileLinkActions(): array {
public function getTalkBackendRegistration(): ?ServiceRegistration {
return $this->talkBackendRegistration;
}

/**
* @return ServiceRegistration[]
* @psalm-return ServiceRegistration<IResourceBackend>[]
*/
public function getCalendarResourceBackendRegistrations(): array {
return $this->calendarResourceBackendRegistrations;
}

/**
* @return ServiceRegistration[]
* @psalm-return ServiceRegistration<IRoomBackend>[]
*/
public function getCalendarRoomBackendRegistrations(): array {
return $this->calendarRoomBackendRegistrations;
}
}
45 changes: 35 additions & 10 deletions lib/private/Calendar/Resource/Manager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
Expand All @@ -24,26 +27,30 @@
*/
namespace OC\Calendar\Resource;

use OC\AppFramework\Bootstrap\Coordinator;
use OCP\Calendar\Resource\IBackend;
use OCP\Calendar\Resource\IManager;
use OCP\IServerContainer;

class Manager implements \OCP\Calendar\Resource\IManager {
class Manager implements IManager {
private Coordinator $bootstrapCoordinator;

/** @var IServerContainer */
private $server;
private IServerContainer $server;

/** @var string[] holds all registered resource backends */
private bool $bootstrapBackendsLoaded = false;

/**
* @var string[] holds all registered resource backends
* @psalm-var class-string<IBackend>[]
*/
private $backends = [];

/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];

/**
* Manager constructor.
*
* @param IServerContainer $server
*/
public function __construct(IServerContainer $server) {
public function __construct(Coordinator $bootstrapCoordinator,
IServerContainer $server) {
$this->bootstrapCoordinator = $bootstrapCoordinator;
$this->server = $server;
}

Expand All @@ -69,12 +76,30 @@ public function unregisterBackend(string $backendClass) {
unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
}

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();
}
}

/**
* @return IBackend[]
* @throws \OCP\AppFramework\QueryException
* @since 14.0.0
*/
public function getBackends():array {
$this->fetchBootstrapBackends();

foreach ($this->backends as $backend) {
if (isset($this->initializedBackends[$backend])) {
continue;
Expand Down
55 changes: 43 additions & 12 deletions lib/private/Calendar/Room/Manager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
Expand All @@ -24,26 +27,30 @@
*/
namespace OC\Calendar\Room;

use OC\AppFramework\Bootstrap\Coordinator;
use OCP\Calendar\Room\IBackend;
use OCP\Calendar\Room\IManager;
use OCP\IServerContainer;

class Manager implements \OCP\Calendar\Room\IManager {
class Manager implements IManager {
private Coordinator $bootstrapCoordinator;

/** @var IServerContainer */
private $server;
private IServerContainer $server;

/** @var string[] holds all registered resource backends */
private $backends = [];

/** @var IBackend[] holds all backends that have been initialized already */
private $initializedBackends = [];
private bool $bootstrapBackendsLoaded = false;

/**
* Manager constructor.
*
* @param IServerContainer $server
* @var string[] holds all registered resource backends
* @psalm-var class-string<IBackend>[]
*/
public function __construct(IServerContainer $server) {
private array $backends = [];

/** @var IBackend[] holds all backends that have been initialized already */
private array $initializedBackends = [];

public function __construct(Coordinator $bootstrapCoordinator,
IServerContainer $server) {
$this->bootstrapCoordinator = $bootstrapCoordinator;
$this->server = $server;
}

Expand All @@ -69,17 +76,41 @@ public function unregisterBackend(string $backendClass) {
unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
}

private function fetchBootstrapBackends(): void {
if ($this->bootstrapBackendsLoaded) {
return;
}

$context = $this->bootstrapCoordinator->getRegistrationContext();
if ($context === null) {
// Too soon
return;
}

foreach ($context->getCalendarRoomBackendRegistrations() as $registration) {
$this->backends[] = $registration->getService();
}
}

/**
* @return IBackend[]
* @throws \OCP\AppFramework\QueryException
* @since 14.0.0
*/
public function getBackends():array {
$this->fetchBootstrapBackends();

foreach ($this->backends as $backend) {
if (isset($this->initializedBackends[$backend])) {
continue;
}

/**
* @todo fetch from the app container
*
* The backend might have services injected that can't be build from the
* server container.
*/
$this->initializedBackends[$backend] = $this->server->query($backend);
}

Expand Down
20 changes: 20 additions & 0 deletions lib/public/AppFramework/Bootstrap/IRegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,24 @@ public function registerProfileLinkAction(string $actionClass): void;
* @since 24.0.0
*/
public function registerTalkBackend(string $backend): void;

/**
* Register a resource backend for the DAV server
*
* @param string $actionClass
* @psalm-param class-string<\OCP\Calendar\Resource\IBackend> $actionClass
* @return void
* @since 24.0.0
*/
public function registerCalendarResourceBackend(string $class): void;

/**
* Register a room backend for the DAV server
*
* @param string $actionClass
* @psalm-param class-string<\OCP\Calendar\Room\IBackend> $actionClass
* @return void
* @since 24.0.0
*/
public function registerCalendarRoomBackend(string $class): void;
}
11 changes: 9 additions & 2 deletions lib/public/Calendar/Resource/IManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
Expand All @@ -23,9 +26,8 @@
namespace OCP\Calendar\Resource;

/**
* Interface IManager
*
* @since 14.0.0
* @deprecated 24.0.0
*/
interface IManager {

Expand All @@ -35,6 +37,7 @@ interface IManager {
* @param string $backendClass
* @return void
* @since 14.0.0
* @deprecated 24.0.0 use \OCP\AppFramework\Bootstrap\IRegistrationContext::registerCalendarResourceBackend
*/
public function registerBackend(string $backendClass);

Expand All @@ -44,26 +47,30 @@ public function registerBackend(string $backendClass);
* @param string $backendClass
* @return void
* @since 14.0.0
* @deprecated 24.0.0
*/
public function unregisterBackend(string $backendClass);

/**
* @return IBackend[]
* @since 14.0.0
* @deprecated 24.0.0
*/
public function getBackends():array;

/**
* @param string $backendId
* @return IBackend|null
* @since 14.0.0
* @deprecated 24.0.0
*/
public function getBackend($backendId);

/**
* removes all registered backend instances
* @return void
* @since 14.0.0
* @deprecated 24.0.0
*/
public function clear();
}
11 changes: 9 additions & 2 deletions lib/public/Calendar/Room/IManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
Expand All @@ -23,9 +26,8 @@
namespace OCP\Calendar\Room;

/**
* Interface IManager
*
* @since 14.0.0
* @deprecated 24.0.0
*/
interface IManager {

Expand All @@ -35,6 +37,7 @@ interface IManager {
* @param string $backendClass
* @return void
* @since 14.0.0
* @deprecated 24.0.0 use \OC\AppFramework\Bootstrap\::registerCalendarRoomBackend
*/
public function registerBackend(string $backendClass);

Expand All @@ -44,26 +47,30 @@ public function registerBackend(string $backendClass);
* @param string $backendClass
* @return void
* @since 14.0.0
* @deprecated 24.0.0
*/
public function unregisterBackend(string $backendClass);

/**
* @return IBackend[]
* @since 14.0.0
* @deprecated 24.0.0
*/
public function getBackends():array;

/**
* @param string $backendId
* @return IBackend|null
* @since 14.0.0
* @deprecated 24.0.0
*/
public function getBackend($backendId);

/**
* removes all registered backend instances
* @return void
* @since 14.0.0
* @deprecated 24.0.0
*/
public function clear();
}
Loading

0 comments on commit 9a656e5

Please sign in to comment.