StorageFactory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Storage;
  8. use OCP\Files\Mount\IMountPoint;
  9. use OCP\Files\Storage\IStorageFactory;
  10. class StorageFactory implements IStorageFactory {
  11. /**
  12. * @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
  13. */
  14. private $storageWrappers = [];
  15. /**
  16. * allow modifier storage behaviour by adding wrappers around storages
  17. *
  18. * $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
  19. *
  20. * @param string $wrapperName name of the wrapper
  21. * @param callable $callback callback
  22. * @param int $priority wrappers with the lower priority are applied last (meaning they get called first)
  23. * @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to
  24. * @return bool true if the wrapper was added, false if there was already a wrapper with this
  25. * name registered
  26. */
  27. public function addStorageWrapper($wrapperName, $callback, $priority = 50, $existingMounts = []) {
  28. if (isset($this->storageWrappers[$wrapperName])) {
  29. return false;
  30. }
  31. // apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
  32. foreach ($existingMounts as $mount) {
  33. $mount->wrapStorage($callback);
  34. }
  35. $this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
  36. return true;
  37. }
  38. /**
  39. * Remove a storage wrapper by name.
  40. * Note: internal method only to be used for cleanup
  41. *
  42. * @param string $wrapperName name of the wrapper
  43. * @internal
  44. */
  45. public function removeStorageWrapper($wrapperName) {
  46. unset($this->storageWrappers[$wrapperName]);
  47. }
  48. /**
  49. * Create an instance of a storage and apply the registered storage wrappers
  50. *
  51. * @param \OCP\Files\Mount\IMountPoint $mountPoint
  52. * @param string $class
  53. * @param array $arguments
  54. * @return \OCP\Files\Storage
  55. */
  56. public function getInstance(IMountPoint $mountPoint, $class, $arguments) {
  57. return $this->wrap($mountPoint, new $class($arguments));
  58. }
  59. /**
  60. * @param \OCP\Files\Mount\IMountPoint $mountPoint
  61. * @param \OCP\Files\Storage $storage
  62. * @return \OCP\Files\Storage
  63. */
  64. public function wrap(IMountPoint $mountPoint, $storage) {
  65. $wrappers = array_values($this->storageWrappers);
  66. usort($wrappers, function ($a, $b) {
  67. return $b['priority'] - $a['priority'];
  68. });
  69. /** @var callable[] $wrappers */
  70. $wrappers = array_map(function ($wrapper) {
  71. return $wrapper['wrapper'];
  72. }, $wrappers);
  73. foreach ($wrappers as $wrapper) {
  74. $storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
  75. if (!($storage instanceof \OCP\Files\Storage)) {
  76. throw new \Exception('Invalid result from storage wrapper');
  77. }
  78. }
  79. return $storage;
  80. }
  81. }