Manager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Calendar\Room;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\Calendar\ResourcesRoomsUpdater;
  10. use OCP\Calendar\Room\IBackend;
  11. use OCP\Calendar\Room\IManager;
  12. use OCP\IServerContainer;
  13. class Manager implements IManager {
  14. private bool $bootstrapBackendsLoaded = false;
  15. /**
  16. * @var string[] holds all registered resource backends
  17. * @psalm-var class-string<IBackend>[]
  18. */
  19. private array $backends = [];
  20. /** @var IBackend[] holds all backends that have been initialized already */
  21. private array $initializedBackends = [];
  22. public function __construct(
  23. private Coordinator $bootstrapCoordinator,
  24. private IServerContainer $server,
  25. private ResourcesRoomsUpdater $updater,
  26. ) {
  27. }
  28. /**
  29. * Registers a resource backend
  30. *
  31. * @since 14.0.0
  32. */
  33. public function registerBackend(string $backendClass): void {
  34. $this->backends[$backendClass] = $backendClass;
  35. }
  36. /**
  37. * Unregisters a resource backend
  38. *
  39. * @param string $backendClass
  40. * @since 14.0.0
  41. */
  42. public function unregisterBackend(string $backendClass): void {
  43. unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
  44. }
  45. private function fetchBootstrapBackends(): void {
  46. if ($this->bootstrapBackendsLoaded) {
  47. return;
  48. }
  49. $context = $this->bootstrapCoordinator->getRegistrationContext();
  50. if ($context === null) {
  51. // Too soon
  52. return;
  53. }
  54. foreach ($context->getCalendarRoomBackendRegistrations() as $registration) {
  55. $this->backends[] = $registration->getService();
  56. }
  57. }
  58. /**
  59. * @return IBackend[]
  60. * @throws \OCP\AppFramework\QueryException
  61. * @since 14.0.0
  62. */
  63. public function getBackends():array {
  64. $this->fetchBootstrapBackends();
  65. foreach ($this->backends as $backend) {
  66. if (isset($this->initializedBackends[$backend])) {
  67. continue;
  68. }
  69. /**
  70. * @todo fetch from the app container
  71. *
  72. * The backend might have services injected that can't be build from the
  73. * server container.
  74. */
  75. $this->initializedBackends[$backend] = $this->server->query($backend);
  76. }
  77. return array_values($this->initializedBackends);
  78. }
  79. /**
  80. * @param string $backendId
  81. * @throws \OCP\AppFramework\QueryException
  82. */
  83. public function getBackend($backendId): ?IBackend {
  84. $backends = $this->getBackends();
  85. foreach ($backends as $backend) {
  86. if ($backend->getBackendIdentifier() === $backendId) {
  87. return $backend;
  88. }
  89. }
  90. return null;
  91. }
  92. /**
  93. * removes all registered backend instances
  94. *
  95. * @since 14.0.0
  96. */
  97. public function clear(): void {
  98. $this->backends = [];
  99. $this->initializedBackends = [];
  100. }
  101. public function update(): void {
  102. $this->updater->updateRooms();
  103. }
  104. }