1
0

Application.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Listener\FileEventsListener;
  36. use OCA\Files_Versions\Listener\LoadAdditionalListener;
  37. use OCA\Files_Versions\Listener\LoadSidebarListener;
  38. use OCA\Files_Versions\Listener\VersionAuthorListener;
  39. use OCA\Files_Versions\Listener\VersionStorageMoveListener;
  40. use OCA\Files_Versions\Versions\IVersionManager;
  41. use OCA\Files_Versions\Versions\VersionManager;
  42. use OCP\Accounts\IAccountManager;
  43. use OCP\App\IAppManager;
  44. use OCP\AppFramework\App;
  45. use OCP\AppFramework\Bootstrap\IBootContext;
  46. use OCP\AppFramework\Bootstrap\IBootstrap;
  47. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  48. use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
  49. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  50. use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
  51. use OCP\Files\Events\Node\BeforeNodeTouchedEvent;
  52. use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
  53. use OCP\Files\Events\Node\NodeCopiedEvent;
  54. use OCP\Files\Events\Node\NodeCreatedEvent;
  55. use OCP\Files\Events\Node\NodeDeletedEvent;
  56. use OCP\Files\Events\Node\NodeRenamedEvent;
  57. use OCP\Files\Events\Node\NodeTouchedEvent;
  58. use OCP\Files\Events\Node\NodeWrittenEvent;
  59. use OCP\IConfig;
  60. use OCP\IGroupManager;
  61. use OCP\IServerContainer;
  62. use OCP\IUserManager;
  63. use OCP\IUserSession;
  64. use OCP\L10N\IFactory;
  65. use OCP\Share\IManager as IShareManager;
  66. use Psr\Container\ContainerInterface;
  67. use Psr\Log\LoggerInterface;
  68. class Application extends App implements IBootstrap {
  69. public const APP_ID = 'files_versions';
  70. public function __construct(array $urlParams = []) {
  71. parent::__construct(self::APP_ID, $urlParams);
  72. }
  73. public function register(IRegistrationContext $context): void {
  74. /**
  75. * Register capabilities
  76. */
  77. $context->registerCapability(Capabilities::class);
  78. /**
  79. * Register $principalBackend for the DAV collection
  80. */
  81. $context->registerService('principalBackend', function (ContainerInterface $c) {
  82. /** @var IServerContainer $server */
  83. $server = $c->get(IServerContainer::class);
  84. return new Principal(
  85. $server->get(IUserManager::class),
  86. $server->get(IGroupManager::class),
  87. \OC::$server->get(IAccountManager::class),
  88. $server->get(IShareManager::class),
  89. $server->get(IUserSession::class),
  90. $server->get(IAppManager::class),
  91. $server->get(ProxyMapper::class),
  92. $server->get(KnownUserService::class),
  93. $server->get(IConfig::class),
  94. $server->get(IFactory::class),
  95. );
  96. });
  97. $context->registerService(IVersionManager::class, function () {
  98. return new VersionManager();
  99. });
  100. /**
  101. * Register Events
  102. */
  103. $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
  104. $context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
  105. $context->registerEventListener(BeforeNodeRenamedEvent::class, VersionStorageMoveListener::class);
  106. $context->registerEventListener(NodeRenamedEvent::class, VersionStorageMoveListener::class);
  107. $context->registerEventListener(BeforeNodeCopiedEvent::class, VersionStorageMoveListener::class);
  108. $context->registerEventListener(NodeCopiedEvent::class, VersionStorageMoveListener::class);
  109. $context->registerEventListener(NodeCreatedEvent::class, FileEventsListener::class);
  110. $context->registerEventListener(BeforeNodeTouchedEvent::class, FileEventsListener::class);
  111. $context->registerEventListener(NodeTouchedEvent::class, FileEventsListener::class);
  112. $context->registerEventListener(BeforeNodeWrittenEvent::class, FileEventsListener::class);
  113. $context->registerEventListener(NodeWrittenEvent::class, FileEventsListener::class);
  114. $context->registerEventListener(BeforeNodeDeletedEvent::class, FileEventsListener::class);
  115. $context->registerEventListener(NodeDeletedEvent::class, FileEventsListener::class);
  116. $context->registerEventListener(NodeRenamedEvent::class, FileEventsListener::class);
  117. $context->registerEventListener(NodeCopiedEvent::class, FileEventsListener::class);
  118. $context->registerEventListener(BeforeNodeRenamedEvent::class, FileEventsListener::class);
  119. $context->registerEventListener(BeforeNodeCopiedEvent::class, FileEventsListener::class);
  120. $context->registerEventListener(NodeWrittenEvent::class, VersionAuthorListener::class);
  121. }
  122. public function boot(IBootContext $context): void {
  123. $context->injectFn(\Closure::fromCallable([$this, 'registerVersionBackends']));
  124. }
  125. public function registerVersionBackends(ContainerInterface $container, IAppManager $appManager, LoggerInterface $logger): void {
  126. foreach ($appManager->getInstalledApps() as $app) {
  127. $appInfo = $appManager->getAppInfo($app);
  128. if (isset($appInfo['versions'])) {
  129. $backends = $appInfo['versions'];
  130. foreach ($backends as $backend) {
  131. if (isset($backend['@value'])) {
  132. $this->loadBackend($backend, $container, $logger);
  133. } else {
  134. foreach ($backend as $singleBackend) {
  135. $this->loadBackend($singleBackend, $container, $logger);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. private function loadBackend(array $backend, ContainerInterface $container, LoggerInterface $logger): void {
  143. /** @var IVersionManager $versionManager */
  144. $versionManager = $container->get(IVersionManager::class);
  145. $class = $backend['@value'];
  146. $for = $backend['@attributes']['for'];
  147. try {
  148. $backendObject = $container->get($class);
  149. $versionManager->registerBackend($for, $backendObject);
  150. } catch (\Exception $e) {
  151. $logger->error($e->getMessage(), ['exception' => $e]);
  152. }
  153. }
  154. }