VersionManager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Versions\Versions;
  25. use OCP\Files\File;
  26. use OCP\Files\FileInfo;
  27. use OCP\Files\Storage\IStorage;
  28. use OCP\IUser;
  29. class VersionManager implements IVersionManager {
  30. /** @var (IVersionBackend[])[] */
  31. private $backends = [];
  32. public function registerBackend(string $storageType, IVersionBackend $backend) {
  33. if (!isset($this->backends[$storageType])) {
  34. $this->backends[$storageType] = [];
  35. }
  36. $this->backends[$storageType][] = $backend;
  37. }
  38. /**
  39. * @return (IVersionBackend[])[]
  40. */
  41. private function getBackends(): array {
  42. return $this->backends;
  43. }
  44. /**
  45. * @param IStorage $storage
  46. * @return IVersionBackend
  47. * @throws BackendNotFoundException
  48. */
  49. public function getBackendForStorage(IStorage $storage): IVersionBackend {
  50. $fullType = get_class($storage);
  51. $backends = $this->getBackends();
  52. $foundType = '';
  53. $foundBackend = null;
  54. foreach ($backends as $type => $backendsForType) {
  55. if (
  56. $storage->instanceOfStorage($type) &&
  57. ($foundType === '' || is_subclass_of($type, $foundType))
  58. ) {
  59. foreach ($backendsForType as $backend) {
  60. /** @var IVersionBackend $backend */
  61. if ($backend->useBackendForStorage($storage)) {
  62. $foundBackend = $backend;
  63. $foundType = $type;
  64. }
  65. }
  66. }
  67. }
  68. if ($foundType === '' || $foundBackend === null) {
  69. throw new BackendNotFoundException("Version backend for $fullType not found");
  70. } else {
  71. return $foundBackend;
  72. }
  73. }
  74. public function getVersionsForFile(IUser $user, FileInfo $file): array {
  75. $backend = $this->getBackendForStorage($file->getStorage());
  76. return $backend->getVersionsForFile($user, $file);
  77. }
  78. public function createVersion(IUser $user, FileInfo $file) {
  79. $backend = $this->getBackendForStorage($file->getStorage());
  80. $backend->createVersion($user, $file);
  81. }
  82. public function rollback(IVersion $version) {
  83. $backend = $version->getBackend();
  84. return $backend->rollback($version);
  85. }
  86. public function read(IVersion $version) {
  87. $backend = $version->getBackend();
  88. return $backend->read($version);
  89. }
  90. public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): File {
  91. $backend = $this->getBackendForStorage($sourceFile->getStorage());
  92. return $backend->getVersionFile($user, $sourceFile, $revision);
  93. }
  94. public function useBackendForStorage(IStorage $storage): bool {
  95. return false;
  96. }
  97. }