Manager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Resource;
  27. use OC\AppFramework\Bootstrap\Coordinator;
  28. use OCP\Calendar\Resource\IBackend;
  29. use OCP\Calendar\Resource\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 $backends = [];
  40. /** @var IBackend[] holds all backends that have been initialized already */
  41. private $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->getCalendarResourceBackendRegistrations() 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. $this->initializedBackends[$backend] = $this->server->query($backend);
  92. }
  93. return array_values($this->initializedBackends);
  94. }
  95. /**
  96. * @param string $backendId
  97. * @throws \OCP\AppFramework\QueryException
  98. * @return IBackend|null
  99. */
  100. public function getBackend($backendId) {
  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. * @return void
  112. * @since 14.0.0
  113. */
  114. public function clear() {
  115. $this->backends = [];
  116. $this->initializedBackends = [];
  117. }
  118. }