Application.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Versions\AppInfo;
  8. use OC\KnownUser\KnownUserService;
  9. use OCA\DAV\CalDAV\Proxy\ProxyMapper;
  10. use OCA\DAV\Connector\Sabre\Principal;
  11. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  12. use OCA\Files\Event\LoadSidebar;
  13. use OCA\Files_Versions\Capabilities;
  14. use OCA\Files_Versions\Listener\FileEventsListener;
  15. use OCA\Files_Versions\Listener\LoadAdditionalListener;
  16. use OCA\Files_Versions\Listener\LoadSidebarListener;
  17. use OCA\Files_Versions\Listener\VersionAuthorListener;
  18. use OCA\Files_Versions\Listener\VersionStorageMoveListener;
  19. use OCA\Files_Versions\Versions\IVersionManager;
  20. use OCA\Files_Versions\Versions\VersionManager;
  21. use OCP\Accounts\IAccountManager;
  22. use OCP\App\IAppManager;
  23. use OCP\AppFramework\App;
  24. use OCP\AppFramework\Bootstrap\IBootContext;
  25. use OCP\AppFramework\Bootstrap\IBootstrap;
  26. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  27. use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
  28. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  29. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  30. use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
  31. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  32. use OCP\Files\Events\Node\NodeCopiedEvent;
  33. use OCP\Files\Events\Node\NodeCreatedEvent;
  34. use OCP\Files\Events\Node\NodeDeletedEvent;
  35. use OCP\Files\Events\Node\NodeRenamedEvent;
  36. use OCP\Files\Events\Node\NodeTouchedEvent;
  37. use OCP\Files\Events\Node\NodeWrittenEvent;
  38. use OCP\IConfig;
  39. use OCP\IGroupManager;
  40. use OCP\IServerContainer;
  41. use OCP\IUserManager;
  42. use OCP\IUserSession;
  43. use OCP\L10N\IFactory;
  44. use OCP\Share\IManager as IShareManager;
  45. use Psr\Container\ContainerInterface;
  46. use Psr\Log\LoggerInterface;
  47. class Application extends App implements IBootstrap {
  48. public const APP_ID = 'files_versions';
  49. public function __construct(array $urlParams = []) {
  50. parent::__construct(self::APP_ID, $urlParams);
  51. }
  52. public function register(IRegistrationContext $context): void {
  53. /**
  54. * Register capabilities
  55. */
  56. $context->registerCapability(Capabilities::class);
  57. /**
  58. * Register $principalBackend for the DAV collection
  59. */
  60. $context->registerService('principalBackend', function (ContainerInterface $c) {
  61. /** @var IServerContainer $server */
  62. $server = $c->get(IServerContainer::class);
  63. return new Principal(
  64. $server->get(IUserManager::class),
  65. $server->get(IGroupManager::class),
  66. \OC::$server->get(IAccountManager::class),
  67. $server->get(IShareManager::class),
  68. $server->get(IUserSession::class),
  69. $server->get(IAppManager::class),
  70. $server->get(ProxyMapper::class),
  71. $server->get(KnownUserService::class),
  72. $server->get(IConfig::class),
  73. $server->get(IFactory::class),
  74. );
  75. });
  76. $context->registerService(IVersionManager::class, function () {
  77. return new VersionManager();
  78. });
  79. /**
  80. * Register Events
  81. */
  82. $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  83. $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
  84. $context->registerEventListener(BeforeNodeRenamedEvent::class, VersionStorageMoveListener::class);
  85. $context->registerEventListener(NodeRenamedEvent::class, VersionStorageMoveListener::class);
  86. $context->registerEventListener(BeforeNodeCopiedEvent::class, VersionStorageMoveListener::class);
  87. $context->registerEventListener(NodeCopiedEvent::class, VersionStorageMoveListener::class);
  88. $context->registerEventListener(NodeCreatedEvent::class, FileEventsListener::class);
  89. $context->registerEventListener(BeforeNodeTouchedEvent::class, FileEventsListener::class);
  90. $context->registerEventListener(NodeTouchedEvent::class, FileEventsListener::class);
  91. $context->registerEventListener(BeforeNodeWrittenEvent::class, FileEventsListener::class);
  92. $context->registerEventListener(NodeWrittenEvent::class, FileEventsListener::class);
  93. $context->registerEventListener(BeforeNodeDeletedEvent::class, FileEventsListener::class);
  94. $context->registerEventListener(NodeDeletedEvent::class, FileEventsListener::class);
  95. $context->registerEventListener(NodeRenamedEvent::class, FileEventsListener::class);
  96. $context->registerEventListener(NodeCopiedEvent::class, FileEventsListener::class);
  97. $context->registerEventListener(BeforeNodeRenamedEvent::class, FileEventsListener::class);
  98. $context->registerEventListener(BeforeNodeCopiedEvent::class, FileEventsListener::class);
  99. $context->registerEventListener(NodeWrittenEvent::class, VersionAuthorListener::class);
  100. }
  101. public function boot(IBootContext $context): void {
  102. $context->injectFn(\Closure::fromCallable([$this, 'registerVersionBackends']));
  103. }
  104. public function registerVersionBackends(ContainerInterface $container, IAppManager $appManager, LoggerInterface $logger): void {
  105. foreach ($appManager->getInstalledApps() as $app) {
  106. $appInfo = $appManager->getAppInfo($app);
  107. if (isset($appInfo['versions'])) {
  108. $backends = $appInfo['versions'];
  109. foreach ($backends as $backend) {
  110. if (isset($backend['@value'])) {
  111. $this->loadBackend($backend, $container, $logger);
  112. } else {
  113. foreach ($backend as $singleBackend) {
  114. $this->loadBackend($singleBackend, $container, $logger);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. private function loadBackend(array $backend, ContainerInterface $container, LoggerInterface $logger): void {
  122. /** @var IVersionManager $versionManager */
  123. $versionManager = $container->get(IVersionManager::class);
  124. $class = $backend['@value'];
  125. $for = $backend['@attributes']['for'];
  126. try {
  127. $backendObject = $container->get($class);
  128. $versionManager->registerBackend($for, $backendObject);
  129. } catch (\Exception $e) {
  130. $logger->error($e->getMessage(), ['exception' => $e]);
  131. }
  132. }
  133. }