Manager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Calendar\Room;
  27. use OC\AppFramework\Bootstrap\Coordinator;
  28. use OCP\Calendar\Room\IBackend;
  29. use OCP\Calendar\Room\IManager;
  30. use OCP\IServerContainer;
  31. class Manager implements IManager {
  32. private bool $bootstrapBackendsLoaded = false;
  33. /**
  34. * @var string[] holds all registered resource backends
  35. * @psalm-var class-string<IBackend>[]
  36. */
  37. private array $backends = [];
  38. /** @var IBackend[] holds all backends that have been initialized already */
  39. private array $initializedBackends = [];
  40. public function __construct(
  41. private Coordinator $bootstrapCoordinator,
  42. private IServerContainer $server,
  43. ) {
  44. }
  45. /**
  46. * Registers a resource backend
  47. *
  48. * @since 14.0.0
  49. */
  50. public function registerBackend(string $backendClass): void {
  51. $this->backends[$backendClass] = $backendClass;
  52. }
  53. /**
  54. * Unregisters a resource backend
  55. *
  56. * @param string $backendClass
  57. * @since 14.0.0
  58. */
  59. public function unregisterBackend(string $backendClass): void {
  60. unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
  61. }
  62. private function fetchBootstrapBackends(): void {
  63. if ($this->bootstrapBackendsLoaded) {
  64. return;
  65. }
  66. $context = $this->bootstrapCoordinator->getRegistrationContext();
  67. if ($context === null) {
  68. // Too soon
  69. return;
  70. }
  71. foreach ($context->getCalendarRoomBackendRegistrations() as $registration) {
  72. $this->backends[] = $registration->getService();
  73. }
  74. }
  75. /**
  76. * @return IBackend[]
  77. * @throws \OCP\AppFramework\QueryException
  78. * @since 14.0.0
  79. */
  80. public function getBackends():array {
  81. $this->fetchBootstrapBackends();
  82. foreach ($this->backends as $backend) {
  83. if (isset($this->initializedBackends[$backend])) {
  84. continue;
  85. }
  86. /**
  87. * @todo fetch from the app container
  88. *
  89. * The backend might have services injected that can't be build from the
  90. * server container.
  91. */
  92. $this->initializedBackends[$backend] = $this->server->query($backend);
  93. }
  94. return array_values($this->initializedBackends);
  95. }
  96. /**
  97. * @param string $backendId
  98. * @throws \OCP\AppFramework\QueryException
  99. */
  100. public function getBackend($backendId): ?IBackend {
  101. $backends = $this->getBackends();
  102. foreach ($backends as $backend) {
  103. if ($backend->getBackendIdentifier() === $backendId) {
  104. return $backend;
  105. }
  106. }
  107. return null;
  108. }
  109. /**
  110. * removes all registered backend instances
  111. *
  112. * @since 14.0.0
  113. */
  114. public function clear(): void {
  115. $this->backends = [];
  116. $this->initializedBackends = [];
  117. }
  118. }