Manager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Coordinator $bootstrapCoordinator;
  33. private IServerContainer $server;
  34. private bool $bootstrapBackendsLoaded = false;
  35. /**
  36. * @var string[] holds all registered resource backends
  37. * @psalm-var class-string<IBackend>[]
  38. */
  39. private array $backends = [];
  40. /** @var IBackend[] holds all backends that have been initialized already */
  41. private array $initializedBackends = [];
  42. public function __construct(Coordinator $bootstrapCoordinator,
  43. IServerContainer $server) {
  44. $this->bootstrapCoordinator = $bootstrapCoordinator;
  45. $this->server = $server;
  46. }
  47. /**
  48. * Registers a resource backend
  49. *
  50. * @param string $backendClass
  51. * @return void
  52. * @since 14.0.0
  53. */
  54. public function registerBackend(string $backendClass) {
  55. $this->backends[$backendClass] = $backendClass;
  56. }
  57. /**
  58. * Unregisters a resource backend
  59. *
  60. * @param string $backendClass
  61. * @return void
  62. * @since 14.0.0
  63. */
  64. public function unregisterBackend(string $backendClass) {
  65. unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
  66. }
  67. private function fetchBootstrapBackends(): void {
  68. if ($this->bootstrapBackendsLoaded) {
  69. return;
  70. }
  71. $context = $this->bootstrapCoordinator->getRegistrationContext();
  72. if ($context === null) {
  73. // Too soon
  74. return;
  75. }
  76. foreach ($context->getCalendarRoomBackendRegistrations() as $registration) {
  77. $this->backends[] = $registration->getService();
  78. }
  79. }
  80. /**
  81. * @return IBackend[]
  82. * @throws \OCP\AppFramework\QueryException
  83. * @since 14.0.0
  84. */
  85. public function getBackends():array {
  86. $this->fetchBootstrapBackends();
  87. foreach ($this->backends as $backend) {
  88. if (isset($this->initializedBackends[$backend])) {
  89. continue;
  90. }
  91. /**
  92. * @todo fetch from the app container
  93. *
  94. * The backend might have services injected that can't be build from the
  95. * server container.
  96. */
  97. $this->initializedBackends[$backend] = $this->server->query($backend);
  98. }
  99. return array_values($this->initializedBackends);
  100. }
  101. /**
  102. * @param string $backendId
  103. * @throws \OCP\AppFramework\QueryException
  104. * @return IBackend|null
  105. */
  106. public function getBackend($backendId) {
  107. $backends = $this->getBackends();
  108. foreach ($backends as $backend) {
  109. if ($backend->getBackendIdentifier() === $backendId) {
  110. return $backend;
  111. }
  112. }
  113. return null;
  114. }
  115. /**
  116. * removes all registered backend instances
  117. * @return void
  118. * @since 14.0.0
  119. */
  120. public function clear() {
  121. $this->backends = [];
  122. $this->initializedBackends = [];
  123. }
  124. }