Application.php 4.1 KB

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