1
0

VersionManager.php 3.1 KB

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