Manager.php 2.7 KB

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