2013-05-08 16:35:10 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-05-08 16:35:10 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-05-08 16:35:10 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Storage;
|
|
|
|
|
|
2015-03-05 06:28:17 -05:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2014-11-24 09:54:42 -05:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
|
|
|
|
|
|
class StorageFactory implements IStorageFactory {
|
2013-06-07 11:07:13 -04:00
|
|
|
/**
|
2015-03-26 14:24:49 -04:00
|
|
|
* @var array[] [$name=>['priority'=>$priority, 'wrapper'=>$callable] $storageWrappers
|
2013-06-07 11:07:13 -04:00
|
|
|
*/
|
2015-03-26 14:24:49 -04:00
|
|
|
private $storageWrappers = [];
|
2013-05-08 16:35:10 -04:00
|
|
|
|
2013-06-07 11:07:13 -04:00
|
|
|
/**
|
|
|
|
|
* allow modifier storage behaviour by adding wrappers around storages
|
|
|
|
|
*
|
|
|
|
|
* $callback should be a function of type (string $mountPoint, Storage $storage) => Storage
|
|
|
|
|
*
|
2015-01-21 15:54:43 -05:00
|
|
|
* @param string $wrapperName name of the wrapper
|
|
|
|
|
* @param callable $callback callback
|
2015-03-26 14:24:49 -04:00
|
|
|
* @param int $priority wrappers with the lower priority are applied last (meaning they get called first)
|
2015-01-23 07:48:35 -05:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint[] $existingMounts existing mount points to apply the wrapper to
|
2015-01-21 15:54:43 -05:00
|
|
|
* @return bool true if the wrapper was added, false if there was already a wrapper with this
|
2015-01-21 16:27:59 -05:00
|
|
|
* name registered
|
2013-06-07 11:07:13 -04:00
|
|
|
*/
|
2015-03-26 14:24:49 -04:00
|
|
|
public function addStorageWrapper($wrapperName, $callback, $priority = 50, $existingMounts = []) {
|
2015-01-21 16:27:59 -05:00
|
|
|
if (isset($this->storageWrappers[$wrapperName])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-01-23 07:48:35 -05:00
|
|
|
|
|
|
|
|
// apply to existing mounts before registering it to prevent applying it double in MountPoint::createStorage
|
|
|
|
|
foreach ($existingMounts as $mount) {
|
|
|
|
|
$mount->wrapStorage($callback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-26 14:24:49 -04:00
|
|
|
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
|
2015-01-21 16:27:59 -05:00
|
|
|
return true;
|
2013-06-07 11:07:13 -04:00
|
|
|
}
|
|
|
|
|
|
2015-01-21 15:54:43 -05:00
|
|
|
/**
|
|
|
|
|
* Remove a storage wrapper by name.
|
|
|
|
|
* Note: internal method only to be used for cleanup
|
|
|
|
|
*
|
|
|
|
|
* @param string $wrapperName name of the wrapper
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
|
|
|
|
public function removeStorageWrapper($wrapperName) {
|
|
|
|
|
unset($this->storageWrappers[$wrapperName]);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 10:30:58 -05:00
|
|
|
/**
|
2014-11-24 09:54:42 -05:00
|
|
|
* Create an instance of a storage and apply the registered storage wrappers
|
|
|
|
|
*
|
2015-03-05 06:28:17 -05:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-02-06 10:30:58 -05:00
|
|
|
* @param string $class
|
2014-11-24 09:54:42 -05:00
|
|
|
* @param array $arguments
|
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-06 10:30:58 -05:00
|
|
|
*/
|
2015-03-05 06:28:17 -05:00
|
|
|
public function getInstance(IMountPoint $mountPoint, $class, $arguments) {
|
2013-06-07 11:07:13 -04:00
|
|
|
return $this->wrap($mountPoint, new $class($arguments));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-19 03:31:54 -05:00
|
|
|
/**
|
2015-03-05 06:28:17 -05:00
|
|
|
* @param \OCP\Files\Mount\IMountPoint $mountPoint
|
2014-11-24 09:54:42 -05:00
|
|
|
* @param \OCP\Files\Storage $storage
|
|
|
|
|
* @return \OCP\Files\Storage
|
2014-02-19 03:31:54 -05:00
|
|
|
*/
|
2015-03-05 06:28:17 -05:00
|
|
|
public function wrap(IMountPoint $mountPoint, $storage) {
|
2015-03-26 14:24:49 -04:00
|
|
|
$wrappers = array_values($this->storageWrappers);
|
|
|
|
|
usort($wrappers, function ($a, $b) {
|
|
|
|
|
return $b['priority'] - $a['priority'];
|
|
|
|
|
});
|
|
|
|
|
/** @var callable[] $wrappers */
|
|
|
|
|
$wrappers = array_map(function ($wrapper) {
|
|
|
|
|
return $wrapper['wrapper'];
|
|
|
|
|
}, $wrappers);
|
|
|
|
|
foreach ($wrappers as $wrapper) {
|
2015-03-05 06:28:17 -05:00
|
|
|
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
|
2015-08-28 14:48:42 -04:00
|
|
|
if (!($storage instanceof \OCP\Files\Storage)) {
|
|
|
|
|
throw new \Exception('Invalid result from storage wrapper');
|
|
|
|
|
}
|
2013-06-07 11:07:13 -04:00
|
|
|
}
|
|
|
|
|
return $storage;
|
2013-05-08 16:35:10 -04:00
|
|
|
}
|
|
|
|
|
}
|