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.

PluginManager.php 4.3KB

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