Application.php 6.0 KB

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