You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.8 KiB

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