Manager.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Resource;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\Calendar\ResourcesRoomsUpdater;
  10. use OCP\Calendar\Resource\IBackend;
  11. use OCP\Calendar\Resource\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. * @since 14.0.0
  40. */
  41. public function unregisterBackend(string $backendClass): void {
  42. unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
  43. }
  44. private function fetchBootstrapBackends(): void {
  45. if ($this->bootstrapBackendsLoaded) {
  46. return;
  47. }
  48. $context = $this->bootstrapCoordinator->getRegistrationContext();
  49. if ($context === null) {
  50. // Too soon
  51. return;
  52. }
  53. foreach ($context->getCalendarResourceBackendRegistrations() as $registration) {
  54. $this->backends[] = $registration->getService();
  55. }
  56. }
  57. /**
  58. * @return IBackend[]
  59. * @throws \OCP\AppFramework\QueryException
  60. * @since 14.0.0
  61. */
  62. public function getBackends():array {
  63. $this->fetchBootstrapBackends();
  64. foreach ($this->backends as $backend) {
  65. if (isset($this->initializedBackends[$backend])) {
  66. continue;
  67. }
  68. $this->initializedBackends[$backend] = $this->server->query($backend);
  69. }
  70. return array_values($this->initializedBackends);
  71. }
  72. /**
  73. * @param string $backendId
  74. * @throws \OCP\AppFramework\QueryException
  75. */
  76. public function getBackend($backendId): ?IBackend {
  77. $backends = $this->getBackends();
  78. foreach ($backends as $backend) {
  79. if ($backend->getBackendIdentifier() === $backendId) {
  80. return $backend;
  81. }
  82. }
  83. return null;
  84. }
  85. /**
  86. * removes all registered backend instances
  87. *
  88. * @since 14.0.0
  89. */
  90. public function clear(): void {
  91. $this->backends = [];
  92. $this->initializedBackends = [];
  93. }
  94. public function update(): void {
  95. $this->updater->updateResources();
  96. }
  97. }