1
0

Application.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Trashbin\AppInfo;
  8. use OCA\DAV\Connector\Sabre\Principal;
  9. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  10. use OCA\Files_Trashbin\Capabilities;
  11. use OCA\Files_Trashbin\Events\BeforeNodeRestoredEvent;
  12. use OCA\Files_Trashbin\Expiration;
  13. use OCA\Files_Trashbin\Listener\EventListener;
  14. use OCA\Files_Trashbin\Listeners\LoadAdditionalScripts;
  15. use OCA\Files_Trashbin\Listeners\SyncLivePhotosListener;
  16. use OCA\Files_Trashbin\Trash\ITrashManager;
  17. use OCA\Files_Trashbin\Trash\TrashManager;
  18. use OCA\Files_Trashbin\Trashbin;
  19. use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
  20. use OCP\App\IAppManager;
  21. use OCP\AppFramework\App;
  22. use OCP\AppFramework\Bootstrap\IBootContext;
  23. use OCP\AppFramework\Bootstrap\IBootstrap;
  24. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  25. use OCP\Files\Events\BeforeFileSystemSetupEvent;
  26. use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
  27. use OCP\Files\Events\Node\NodeWrittenEvent;
  28. use OCP\User\Events\BeforeUserDeletedEvent;
  29. use Psr\Container\ContainerInterface;
  30. use Psr\Log\LoggerInterface;
  31. class Application extends App implements IBootstrap {
  32. public const APP_ID = 'files_trashbin';
  33. public function __construct(array $urlParams = []) {
  34. parent::__construct(self::APP_ID, $urlParams);
  35. }
  36. public function register(IRegistrationContext $context): void {
  37. $context->registerCapability(Capabilities::class);
  38. $context->registerServiceAlias('Expiration', Expiration::class);
  39. $context->registerServiceAlias(ITrashManager::class, TrashManager::class);
  40. /** Register $principalBackend for the DAV collection */
  41. $context->registerServiceAlias('principalBackend', Principal::class);
  42. $context->registerUserMigrator(TrashbinMigrator::class);
  43. $context->registerEventListener(
  44. LoadAdditionalScriptsEvent::class,
  45. LoadAdditionalScripts::class
  46. );
  47. $context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class);
  48. $context->registerEventListener(NodeWrittenEvent::class, EventListener::class);
  49. $context->registerEventListener(BeforeUserDeletedEvent::class, EventListener::class);
  50. $context->registerEventListener(BeforeFileSystemSetupEvent::class, EventListener::class);
  51. // pre and post-rename, disable trash logic for the copy+unlink case
  52. $context->registerEventListener(BeforeNodeDeletedEvent::class, Trashbin::class);
  53. }
  54. public function boot(IBootContext $context): void {
  55. $context->injectFn([$this, 'registerTrashBackends']);
  56. }
  57. public function registerTrashBackends(ContainerInterface $serverContainer, LoggerInterface $logger, IAppManager $appManager, ITrashManager $trashManager): void {
  58. foreach ($appManager->getInstalledApps() as $app) {
  59. $appInfo = $appManager->getAppInfo($app);
  60. if (isset($appInfo['trash'])) {
  61. $backends = $appInfo['trash'];
  62. foreach ($backends as $backend) {
  63. $class = $backend['@value'];
  64. $for = $backend['@attributes']['for'];
  65. try {
  66. $backendObject = $serverContainer->get($class);
  67. $trashManager->registerBackend($for, $backendObject);
  68. } catch (\Exception $e) {
  69. $logger->error($e->getMessage(), ['exception' => $e]);
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }