PluginManager.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud GmbH.
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\AppInfo;
  24. use OCP\App\IAppManager;
  25. use OC\ServerContainer;
  26. use OCP\AppFramework\QueryException;
  27. /**
  28. * Manager for DAV plugins from apps, used to register them
  29. * to the Sabre server.
  30. */
  31. class PluginManager {
  32. /**
  33. * @var ServerContainer
  34. */
  35. private $container;
  36. /**
  37. * @var IAppManager
  38. */
  39. private $appManager;
  40. /**
  41. * App plugins
  42. *
  43. * @var array
  44. */
  45. private $plugins = null;
  46. /**
  47. * App collections
  48. *
  49. * @var array
  50. */
  51. private $collections = null;
  52. /**
  53. * Contstruct a PluginManager
  54. *
  55. * @param ServerContainer $container server container for resolving plugin classes
  56. * @param IAppManager $appManager app manager to loading apps and their info
  57. */
  58. public function __construct(ServerContainer $container, IAppManager $appManager) {
  59. $this->container = $container;
  60. $this->appManager = $appManager;
  61. }
  62. /**
  63. * Returns an array of app-registered plugins
  64. *
  65. * @return array
  66. */
  67. public function getAppPlugins() {
  68. if (null === $this->plugins) {
  69. $this->populate();
  70. }
  71. return $this->plugins;
  72. }
  73. /**
  74. * Returns an array of app-registered collections
  75. *
  76. * @return array
  77. */
  78. public function getAppCollections() {
  79. if (null === $this->collections) {
  80. $this->populate();
  81. }
  82. return $this->collections;
  83. }
  84. /**
  85. * Retrieve plugin and collection list and populate attributes
  86. */
  87. private function populate() {
  88. $this->plugins = [];
  89. $this->collections = [];
  90. foreach ($this->appManager->getInstalledApps() as $app) {
  91. // load plugins and collections from info.xml
  92. $info = $this->appManager->getAppInfo($app);
  93. if (!isset($info['types']) || !in_array('dav', $info['types'], true)) {
  94. continue;
  95. }
  96. $this->loadSabrePluginsFromInfoXml($this->extractPluginList($info));
  97. $this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info));
  98. }
  99. }
  100. private function extractPluginList(array $array) {
  101. if (isset($array['sabre']) && is_array($array['sabre'])) {
  102. if (isset($array['sabre']['plugins']) && is_array($array['sabre']['plugins'])) {
  103. if (isset($array['sabre']['plugins']['plugin'])) {
  104. $items = $array['sabre']['plugins']['plugin'];
  105. if (!is_array($items)) {
  106. $items = [$items];
  107. }
  108. return $items;
  109. }
  110. }
  111. }
  112. return [];
  113. }
  114. private function extractCollectionList(array $array) {
  115. if (isset($array['sabre']) && is_array($array['sabre'])) {
  116. if (isset($array['sabre']['collections']) && is_array($array['sabre']['collections'])) {
  117. if (isset($array['sabre']['collections']['collection'])) {
  118. $items = $array['sabre']['collections']['collection'];
  119. if (!is_array($items)) {
  120. $items = [$items];
  121. }
  122. return $items;
  123. }
  124. }
  125. }
  126. return [];
  127. }
  128. private function loadSabrePluginsFromInfoXml(array $plugins) {
  129. foreach ($plugins as $plugin) {
  130. try {
  131. $this->plugins[] = $this->container->query($plugin);
  132. } catch (QueryException $e) {
  133. if (class_exists($plugin)) {
  134. $this->plugins[] = new $plugin();
  135. } else {
  136. throw new \Exception("Sabre plugin class '$plugin' is unknown and could not be loaded");
  137. }
  138. }
  139. }
  140. }
  141. private function loadSabreCollectionsFromInfoXml(array $collections) {
  142. foreach ($collections as $collection) {
  143. try {
  144. $this->collections[] = $this->container->query($collection);
  145. } catch (QueryException $e) {
  146. if (class_exists($collection)) {
  147. $this->collections[] = new $collection();
  148. } else {
  149. throw new \Exception("Sabre collection class '$collection' is unknown and could not be loaded");
  150. }
  151. }
  152. }
  153. }
  154. }