Application.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Versions\AppInfo;
  29. use OC\KnownUser\KnownUserService;
  30. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  31. use OCA\DAV\Connector\Sabre\Principal;
  32. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  33. use OCA\Files\Event\LoadSidebar;
  34. use OCA\Files_Versions\Capabilities;
  35. use OCA\Files_Versions\Hooks;
  36. use OCA\Files_Versions\Listener\LoadAdditionalListener;
  37. use OCA\Files_Versions\Listener\LoadSidebarListener;
  38. use OCA\Files_Versions\Versions\IVersionManager;
  39. use OCA\Files_Versions\Versions\VersionManager;
  40. use OCP\Accounts\IAccountManager;
  41. use OCP\App\IAppManager;
  42. use OCP\AppFramework\App;
  43. use OCP\AppFramework\Bootstrap\IBootContext;
  44. use OCP\AppFramework\Bootstrap\IBootstrap;
  45. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  46. use OCP\IConfig;
  47. use OCP\IGroupManager;
  48. use OCP\IServerContainer;
  49. use OCP\IUserManager;
  50. use OCP\IUserSession;
  51. use OCP\L10N\IFactory;
  52. use OCP\Share\IManager as IShareManager;
  53. use Psr\Container\ContainerInterface;
  54. use Psr\Log\LoggerInterface;
  55. class Application extends App implements IBootstrap {
  56. public const APP_ID = 'files_versions';
  57. public function __construct(array $urlParams = []) {
  58. parent::__construct(self::APP_ID, $urlParams);
  59. }
  60. public function register(IRegistrationContext $context): void {
  61. /**
  62. * Register capabilities
  63. */
  64. $context->registerCapability(Capabilities::class);
  65. /**
  66. * Register $principalBackend for the DAV collection
  67. */
  68. $context->registerService('principalBackend', function (ContainerInterface $c) {
  69. /** @var IServerContainer $server */
  70. $server = $c->get(IServerContainer::class);
  71. return new Principal(
  72. $server->get(IUserManager::class),
  73. $server->get(IGroupManager::class),
  74. \OC::$server->get(IAccountManager::class),
  75. $server->get(IShareManager::class),
  76. $server->get(IUserSession::class),
  77. $server->get(IAppManager::class),
  78. $server->get(ProxyMapper::class),
  79. $server->get(KnownUserService::class),
  80. $server->get(IConfig::class),
  81. $server->get(IFactory::class),
  82. );
  83. });
  84. $context->registerService(IVersionManager::class, function () {
  85. return new VersionManager();
  86. });
  87. /**
  88. * Register Events
  89. */
  90. $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  91. $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
  92. }
  93. public function boot(IBootContext $context): void {
  94. $context->injectFn(\Closure::fromCallable([$this, 'registerVersionBackends']));
  95. /**
  96. * Register hooks
  97. */
  98. Hooks::connectHooks();
  99. }
  100. public function registerVersionBackends(ContainerInterface $container, IAppManager $appManager, LoggerInterface $logger): void {
  101. foreach ($appManager->getInstalledApps() as $app) {
  102. $appInfo = $appManager->getAppInfo($app);
  103. if (isset($appInfo['versions'])) {
  104. $backends = $appInfo['versions'];
  105. foreach ($backends as $backend) {
  106. if (isset($backend['@value'])) {
  107. $this->loadBackend($backend, $container, $logger);
  108. } else {
  109. foreach ($backend as $singleBackend) {
  110. $this->loadBackend($singleBackend, $container, $logger);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. private function loadBackend(array $backend, ContainerInterface $container, LoggerInterface $logger): void {
  118. /** @var IVersionManager $versionManager */
  119. $versionManager = $container->get(IVersionManager::class);
  120. $class = $backend['@value'];
  121. $for = $backend['@attributes']['for'];
  122. try {
  123. $backendObject = $container->get($class);
  124. $versionManager->registerBackend($for, $backendObject);
  125. } catch (\Exception $e) {
  126. $logger->error($e->getMessage(), ['exception' => $e]);
  127. }
  128. }
  129. }