StorageFactory.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Storage;
  26. use OCP\Files\Mount\IMountPoint;
  27. use OCP\Files\Storage\IStorageFactory;
  28. class StorageFactory implements IStorageFactory {
  29. /**
  30. * @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
  31. */
  32. private $storageWrappers = [];
  33. /**
  34. * allow modifier storage behaviour by adding wrappers around storages
  35. *
  36. * $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
  37. *
  38. * @param string $wrapperName name of the wrapper
  39. * @param callable $callback callback
  40. * @param int $priority wrappers with the lower priority are applied last (meaning they get called first)
  41. * @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to
  42. * @return bool true if the wrapper was added, false if there was already a wrapper with this
  43. * name registered
  44. */
  45. public function addStorageWrapper($wrapperName, $callback, $priority = 50, $existingMounts = []) {
  46. if (isset($this->storageWrappers[$wrapperName])) {
  47. return false;
  48. }
  49. // apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
  50. foreach ($existingMounts as $mount) {
  51. $mount->wrapStorage($callback);
  52. }
  53. $this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
  54. return true;
  55. }
  56. /**
  57. * Remove a storage wrapper by name.
  58. * Note: internal method only to be used for cleanup
  59. *
  60. * @param string $wrapperName name of the wrapper
  61. * @internal
  62. */
  63. public function removeStorageWrapper($wrapperName) {
  64. unset($this->storageWrappers[$wrapperName]);
  65. }
  66. /**
  67. * Create an instance of a storage and apply the registered storage wrappers
  68. *
  69. * @param \OCP\Files\Mount\IMountPoint $mountPoint
  70. * @param string $class
  71. * @param array $arguments
  72. * @return \OCP\Files\Storage
  73. */
  74. public function getInstance(IMountPoint $mountPoint, $class, $arguments) {
  75. return $this->wrap($mountPoint, new $class($arguments));
  76. }
  77. /**
  78. * @param \OCP\Files\Mount\IMountPoint $mountPoint
  79. * @param \OCP\Files\Storage $storage
  80. * @return \OCP\Files\Storage
  81. */
  82. public function wrap(IMountPoint $mountPoint, $storage) {
  83. $wrappers = array_values($this->storageWrappers);
  84. usort($wrappers, function ($a, $b) {
  85. return $b['priority'] - $a['priority'];
  86. });
  87. /** @var callable[] $wrappers */
  88. $wrappers = array_map(function ($wrapper) {
  89. return $wrapper['wrapper'];
  90. }, $wrappers);
  91. foreach ($wrappers as $wrapper) {
  92. $storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
  93. if (!($storage instanceof \OCP\Files\Storage)) {
  94. throw new \Exception('Invalid result from storage wrapper');
  95. }
  96. }
  97. return $storage;
  98. }
  99. }