Manager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 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. * @since 14.0.0
  57. */
  58. public function unregisterBackend(string $backendClass): void {
  59. unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
  60. }
  61. private function fetchBootstrapBackends(): void {
  62. if ($this->bootstrapBackendsLoaded) {
  63. return;
  64. }
  65. $context = $this->bootstrapCoordinator->getRegistrationContext();
  66. if ($context === null) {
  67. // Too soon
  68. return;
  69. }
  70. foreach ($context->getCalendarResourceBackendRegistrations() as $registration) {
  71. $this->backends[] = $registration->getService();
  72. }
  73. }
  74. /**
  75. * @return IBackend[]
  76. * @throws \OCP\AppFramework\QueryException
  77. * @since 14.0.0
  78. */
  79. public function getBackends():array {
  80. $this->fetchBootstrapBackends();
  81. foreach ($this->backends as $backend) {
  82. if (isset($this->initializedBackends[$backend])) {
  83. continue;
  84. }
  85. $this->initializedBackends[$backend] = $this->server->query($backend);
  86. }
  87. return array_values($this->initializedBackends);
  88. }
  89. /**
  90. * @param string $backendId
  91. * @throws \OCP\AppFramework\QueryException
  92. */
  93. public function getBackend($backendId): ?IBackend {
  94. $backends = $this->getBackends();
  95. foreach ($backends as $backend) {
  96. if ($backend->getBackendIdentifier() === $backendId) {
  97. return $backend;
  98. }
  99. }
  100. return null;
  101. }
  102. /**
  103. * removes all registered backend instances
  104. *
  105. * @since 14.0.0
  106. */
  107. public function clear(): void {
  108. $this->backends = [];
  109. $this->initializedBackends = [];
  110. }
  111. }